Here is a listing of C++ interview questions on “Integer Types” along with answers, explanations and/or solutions:
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
View Answer
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
View Answer
Explanation: x is promoted to unsigned int on comparison. On conversion x has all bits set, making it the bigger one.
3. 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;
View Answer
Explanation: Power of two integers have a single set bit followed by unset bits.
4. What is the value of the following 8-bit integer after all statements are executed?
int x = 1;
x = x << 7;
x = x >> 7;
a) 1
b) -1
c) 127
d) Implementation defined
View Answer
Explanation: Right shift of signed integers is undefined, and has implementation-defined behaviour.
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)
View Answer
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)
View Answer
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. 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
View Answer
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 ”.
8. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int a = 8;
cout << "ANDing integer 'a' with 'true' :" << (a && true);
return 0;
}
a) ANDing integer ‘a’ with ‘true’ :8
b) ANDing integer ‘a’ with ‘true’ :0
c) ANDing integer ‘a’ with ‘true’ :1
d) ANDing integer ‘a’ with ‘true’ :9
View Answer
Explanation: The && operator in C++ is a logical operator. Since the value of ‘a’ is 8 (non-zero), the && operation result will be true and hence the final value will be 1.
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
View Answer
Explanation: Sign of result of mod operation on negative numbers is sign of the dividend.
10. What will be the output of the following C++ function?
int main()
{
register int i = 1;
int *ptr = &i;
cout << *ptr;
return 0;
}
a) 0
b) 1
c) Compiler error may be possible
d) Runtime error may be possible
View Answer
Explanation: Using & on a register variable may be invalid, since the compiler may store the variable in a register, and finding the address of it is illegal.
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]
- Practice Programming MCQs
- Apply for Computer Science Internship
- Check C++ Books
- Practice Computer Science MCQs
- Check Programming Books