C++ Programming Questions and Answers – Integer Types

This section on 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 (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. 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.

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

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?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.     	int x = -1;
  6.         unsigned int y = 2;
  7.  
  8.         if(x > y) 
  9.         {
  10.         	cout << "x is greater";
  11.     	}
  12.         else 
  13.         {
  14.     		cout << "y is greater";
  15.     	}      
  16.     }

a) x is greater
b) y is greater
c) implementation defined
d) arbitrary
View Answer

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

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

Answer: d
Explanation: Power of two integers have a single set bit followed by unset bits.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

4. 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
View Answer

Answer: d
Explanation: Right shift of signed integers is undefined, and has implementation-defined behaviour.
advertisement

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

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

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

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

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

8. What will be the output of the following C++ code?

  1.     #include <iostream>
  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.     }

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

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.

9. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int i = 3;
  6.         int l = i / -2;
  7.         int k = i % -2;
  8.         cout << l << k;
  9.         return 0;
  10.     }

a) compile time error
b) -1 1
c) 1 -1
d) implementation defined
View Answer

Answer: b
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?

  1.     int main()
  2.     {
  3.         register int i = 1;
  4.         int *ptr = &i;
  5.         cout << *ptr;
  6. 	return 0;
  7.     }

a) 0
b) 1
c) Compiler error may be possible
d) Runtime error may be possible
View Answer

Answer: c
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]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.