Integer Types(C++ Programming ) Questions and Answers

Question 1.


What will be output of this function?


1.
int main()
2.
{
3.
register int i = 1;
4.
int *ptr = &i;
5.
cout
  1.    0
  2.    1
  3.    Compiler error may be possible
  4.    Runtime error may be possible
Explanation:-
Answer: Option C. -> Compiler error may be possible

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.



Question 2.


What will be output of this program?


1.
#include
2.
using namespace std;
3.
int main()
4.
{
5.
int i = 3;
6.
int l = i / -2;
7.
int k = i % -2;
8.
cout
  1.    compile time error
  2.    -1 1
  3.    1 -1
  4.    implementation defined
Explanation:-
Answer: Option B. -> -1 1

Sign of result of mod operation on negative numbers is sign of the dividend.



Question 3.


What will be the output of this program?


1.
#include
2.
using namespace std;
3.
int main()
4.
{
5.
int a = 8;
6.
cout << "ANDing integer 'a' with 'true' :"" << a && true;
7.
return 0;
8.
}
  1.    integer 'a' with 'true' :8
  2.    ANDing integer 'a' with 'true' :0
  3.    ANDing integer 'a' with 'true' :1
  4.    None of the mentioned
Explanation:-
Answer: Option A. -> integer 'a' with 'true' :8

None.



Question 4.

0946, 786427373824, 'x' and 0X2f are _____, _____, ____ and _____ literals respectively


  1.    decimal, character,octal, hexadecimal
  2.    octal, hexadecimal, character, decimal
  3.    hexadecimal, octal, decimal, character
  4.    octal, decimal, character, hexadecimal
Explanation:-
Answer: Option D. -> octal, decimal, character, hexadecimal

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 ".



Question 5.

Which of these expressions will isolate the rightmost set bit?


  1.    x = x & (~x)
  2.    x = x ^ (~x)
  3.    x = x & (-x)
  4.    x = x ^ (-x)
Explanation:-
Answer: Option C. -> x = x & (-x)

None.



Question 6.

Which of these expressions will make the rightmost set bit zero in an input integer x?


  1.    x = x | (x-1)
  2.    x = x & (x-1)
  3.    x = x | (x+1)
  4.    x = x & (x+1)
Explanation:-
Answer: Option B. -> x = x & (x-1)

None.



Question 7.

Which of these expressions will return true if the input integer v is a power of two?


  1.    (v | (v + 1)) == 0;
  2.    (~v & (v - 1)) == 0;
  3.    (v | (v - 1)) == 0;
  4.    (v & (v - 1)) == 0;
Explanation:-
Answer: Option D. -> (v & (v - 1)) == 0;

Power of two integers have a single set bit followed by unset bits.



Question 8.

What is the value of the following 8-bit integer after all statements are executed?
int x = 1;
x = x << 7;
x = x >> 7;


  1.    1
  2.    -1
  3.    127
  4.    Implementation defined
Explanation:-
Answer: Option D. -> Implementation defined

 Right shift of signed integers is undefined, and has implementation-defined behaviour.



Question 9.

The size_t integer type in C++ is?


  1.    Unsigned integer of at least 64 bits
  2.    Signed integer of at least 16 bits
  3.    Unsigned integer of at least 16 bits
  4.    Signed integer of at least 64 bits
Explanation:-
Answer: Option C. -> Unsigned integer of at least 16 bits

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.



Question 10.


What is the output of the following program?


1.
#include
2.
using namespace std;
3.
int main()
4.
{
5.
int x = -1;
6.
unsigned int y = 2;
7.
8.
if(x > y) {
9.
cout
  1.    x is greater
  2.    y is greater
  3.    Implementation defined
  4.    Arbitrary
Explanation:-
Answer: Option A. -> x is greater

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