C++ Constants & Data Types | Set 3
21. What constant defined in <climits> header returns the number of bits in a char?
a) CHAR_SIZE
b) SIZE_CHAR
c) BIT_CHAR
d) CHAR_BIT
Answer: D
Explanation: CHAR_BIT is a macro constant defined in <climits> header file which expresses the number of bits in a character object in bytes.
22. 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.
23. 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: " ;
}
return 0;
}
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.
24. Which of these expressions will return true if the input integer v is a power of two?
a) (v | (v + 1)) == 0;
b) (~v & (v – 1)) == 0;
c) (v | (v – 1)) == 0;
d) (v & (v – 1)) == 0;
Answer: D
Explanation: Power of two integers have a single set bit followed by unset bits.
25. What is the value of the following 8-bit integer after all statements are executed?
1. int x = 1;
2. x = x << 7;
3. x = x >> 7;
a) 1
b) -1
c) 127
d) Implementation defined
Answer: D
Explanation: Right shift of signed integers is undefined, and has implementation-defined behaviour.
26. 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.
27. 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.
28. 0946, 786427373824, ‘x’ and 0X2f are _____ _____ ____ and _____ literals respectively.
a) decimal, character, octal, hexadecimal
b) octal, hexadecimal, character, decimal
c) hexadecimal, octal, decimal, character
d) octal, decimal, character, hexadecimal
Answer: D
Explanation: Literal integer constants that begin with 0x or 0X are interpreted as hexadecimal and the ones that begin with 0 as octal. The character literal are written within ”.
29. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main() {
int x = 8;
cout << "ANDing integer 'x' with 'true' : " << x && true;
return 0;
}
a) ANDing integer ‘x’ with ‘true’ :8
b) ANDing integer ‘x’ with ‘true’ :0
c) ANDing integer ‘x’ with ‘true’ :1
d) ANDing integer ‘x’ with ‘true’ :9
Answer: A
Explanation: The && operator in C++ uses short-circuit evaluation so that if bool1 evaluates to false it doesn’t bother evaluating bool2. So as here bool1 is 8 which is true as it is non-zero so C++ does not cares about the expression further and prints the answer of expression which is 8. If you write true && 8 then the output will be 1 because true is true and its integer equivalent is 1 so 1 will be printed.
30. 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.