C++ Questions and Answers Part-11

1. The size_t integer type in C++ is?
a) Unsigned integer of at least 64 bits
b) Signed integer of at least 16 bits
c) Unsigned integer of at least 16 bits
d) Signed integer of at least 64 bits

Answer: c
Explanation: The size_t type is used to represent the size of an object. Hence, it’s always unsigned. According to the language specification, it is at least 16 bits.

2. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int x = -1;
unsigned int y = 2;
if(x > y)
{
cout << "x is greater";
}
else
{
cout << "y is greater";
}
}
a) x is greater
b) y is greater
c) implementation defined
d) arbitrary

Answer: a
Explanation: x is promoted to unsigned int on comparison. On conversion x has all bits set, making it the bigger one.

3. How many characters are specified in the ASCII scheme?
a) 64
b) 128
c) 256
d) 24

Answer: b
Explanation: There are 128 characters defined in the C++ ASCII list.

4. Given the variables p, q are of char type and r, s, t are of int type. Select the right statement?
1. t = (r * s) / (r + s);
2. t = (p * q) / (r + s);
a) 1 is true but 2 is false
b) 1 is false and 2 is true
c) both 1 and 2 are true
d) both 1 and 2 are false

Answer: c
Explanation: Every character constant has an integer value. Also char belongs to the integral type hence arithmetic and logical operations can be performed on them.

5. Which of these expressions will make the rightmost set bit zero in an input integer x?
a) x = x | (x-1)
b) x = x & (x-1)
c) x = x | (x+1)
d) x = x & (x+2)

Answer: b
Explanation: If x is odd the last bit will be 1 and last bit of x-1 will become 0. If x is even then last bit of x will be 0 and last bit of x-1 will become 1. In both case AND operation of 1 and 0 will be 0. Hence last bit of final x will be 0.

6. Which of these expressions will isolate the rightmost set bit?
a) x = x & (~x)
b) x = x ^ (~x)
c) x = x & (-x)
d) x = x ^ (-x)

Answer: c
Explanation: Negative of a number is stores as 2;s complement in C++, so when you will take AND of x and (-x) the rightmost digit will be preserved.

7. Which of the following belongs to the set of character types?
a) char
b) wchar_t
c) only a
d) both wchar_t and char

Answer: d
Explanation: wchar_t and char are used to represent wide character and character.

8. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
char c = 74;
cout << c;
return 0;
}
a) A
b) N
c) J
d) I

Answer: c
Explanation: The literal value for 74 is J. So it will be printing J.

9. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int i = 3;
int l = i / -2;
int k = i % -2;
cout << l << k;
return 0;
}
a) compile time error
b) -1 1
c) 1 -1
d) implementation defined

Answer: b
Explanation: Sign of result of mod operation on negative numbers is sign of the dividend.

10. How do we represent a wide character of the form wchar_t?
a) L’a’
b) l’a’
c) L[a]
d) la

Answer: a
Explanation: A wide character is always indicated by immediately preceding the character literal by an L.