# 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

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

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;

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

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)

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)

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.

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

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?

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

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

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]