C++ interview questions and answers focuses on “Integer Types”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.
C++ interview questions on “Integer Types” along with answers, explanations and/or solutions:
Q 1. The size_t integer type in C++ is?
A. Unsigned integer of at least 64 bits
B. Unsigned integer of at least 16 bits
C. Signed integer of at least 56 bits
D. Signed integer of at least 64 bits
Show Answer
Answer:-B.Unsigned integer of at least 16 bitsExplanation
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.Q 2. 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;
Show Answer
Answer:-D. (v & (v – 1)) == 0;Explanation
Power of two integers have a single set bit followed by unset bits.Q 3. 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
Show Answer
Answer:-A. x is greaterExplanation
x is promoted to unsigned int on comparison. On conversion x has all bits set, making it the bigger one.Q 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
Show Answer
Answer:-D. Implementation definedExplanation
Right shift of signed integers is undefined, and has implementation-defined behaviour.Q 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)
Show Answer
Answer:-C. x = x & (x-1)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.Q 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)
Show Answer
Answer:-C. x = x & (-x)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.Q 7. 0946, 786427373824, ‘x’ and 0X2f are _____ _____ ____ and _____ literals respectively.
A. decimal, character, octal, hexadecimal
B. octal, decimal, character, hexadecimal
C. hexadecimal, octal, decimal, character
D. octal, hexadecimal, character, decimal
Show Answer
Answer:-B. octal, decimal, character, hexadecimalExplanation
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 ”.Q 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’ :1
B. ANDing integer ‘a’ with ‘true’ :0
C. ANDing integer ‘a’ with ‘true’ :8
D. ANDing integer ‘a’ with ‘true’ :9
Show Answer
Answer:-A. ANDing integer ‘a’ with ‘true’ :1Explanation
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.Q 9. 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
Show Answer
Answer:-C. Compiler error may be possibleExplanation
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.Q 10. 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
Leave a Reply