C++ Questions and Answers Part-10

1. Is bool a fundamental data type in C++?
a) Yes
b) No, it is a typedef of unsigned char
c) No, it is an enum of {false, true}
d) No, it is expanded from macros

Answer: a
Explanation: C++ has bool as a fundamental data type.

2. Find the odd one out.
a) std::vector<int>
b) std::vector<short>
c) std::vector<long>
d) std::vector<bool>

Answer: d
Explanation: std::vector<bool> is a specialized version of vector, which is used for elements of type bool and optimizes for space. It behaves like the unspecialized version of vector and the storage is not necessarily an array of bool values, but the library implementation may optimize storage so that each value is stored in a single bit.

3. What is the value of the bool?
bool is_int(789.54)
a) True
b) False
c) 1
d) 2

Answer: b
Explanation: The given number is a double not an integer, so the function returns 0 which is boolean false

4. What happens when a null pointer is converted into bool?
a) an error is flagged
b) bool value evaluates to true
c) bool value evaluates to false
d) the statement is ignored

Answer: c
Explanation: A pointer can be implicitly converted to a bool. A nonzero pointer converts to true and zero valued pointer converts to false.

5. Which of the following statements are false?
a) bool can have two values and can be used to express logical expressions
b) bool cannot be used as the type of the result of the function
c) bool can be converted into integers implicitly
d) a bool value can be used in arithmetic expressions

Answer: b
Explanation: Boolean can be used as a return value of a function.

6. For what values of the expression is an if-statement block not executed?
a) 0 and all negative values
b) 0 and -1
c) 0
d) 0, all negative values, all positive values except 1

Answer: c
Explanation: The if-statement block is only not executed when the expression evaluates to 0. its just syntactic sugar for a branch-if-zero instruction.

7. Which of the two operators ++ and — work for the bool data type in C++?
a) None
b) ++
c) —
d) ++ & —

Answer: b
Explanation: Due to the history of using integer values as booleans, if an integer is used as a boolean, then incrementing will mean that whatever its truth value before the operation, it will have a truth-value of true after it. However, it is not possible to predict the result of — given knowledge only of the truth value of x, as it could result in false

8. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int f(int p, int q)
if (p > q)
return p;
return q;
int a = 5, b = 10;
int k;
bool x = true;
bool y = f(a, b);
k =((a * b) + (x + y));
cout << k;
a) 55
b) 62
c) 52
d) 75

Answer: c
Explanation: In this question, value of x = true and value of y will be also true as f(a,b) will return a non-zero value. Now when adding these values with integers, the implicit type conversion takes place hence converting both x and y to 1(integer equivalent of bool true value). So expression (a*b) + (x+y) is evaluated to 52.

9. What is the value of p in the following C++ code?
#include <iostream>
using namespace std;
int main()
int p;
bool a = true;
bool b = false;
int x = 10;
int y = 5;
p = ((x | y) + (a + b));
cout << p;
return 0;
a) 0
b) 16
c) 12
d) 2

Answer: b
Explanation: | means bitwise OR operation so x | y (0101 | 1010) will be evaluated to 1111 which is integer 15 and as a is true and b is false so a+b(1 + 0) = 1. So final value of expression in line #10 will be 15 + 1 = 16.

10. Evaluate the following.
(false && true) || false || true
a) 0
b) 1
c) false
d) 2

Answer: b
Explanation: The given expression is equivalent to [( false AND True) OR false OR true] This is OR or three values so if any of them will be true then the whole exp will be true and as we have last value as true so the answer of expression will be TRUE.