C++ Programming Questions and Answers – Booleans

This section on C++ language interview questions and answers focuses on “Booleans”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in 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++ language interview questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ language interview questions on “Booleans” along with answers, explanations and/or solutions:

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

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

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

Answer: b
Explanation: The given number is a double not an integer, so the function returns 0 which is boolean false.
Note: Join free Sanfoundry classes at Telegram or Youtube

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

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

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

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) ++ & —
View Answer

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?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int f(int p, int q)
  4.     {
  5.         if (p > q)
  6.             return p;
  7.         else
  8.             return q;
  9.     }
  10.     main()
  11.     {
  12.         int a = 5, b = 10;
  13.         int k;
  14.         bool x = true;
  15.         bool y = f(a, b);
  16.         k =((a * b) + (x + y));
  17.         cout << k;
  18.     }

a) 55
b) 62
c) 52
d) 75
View Answer

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?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int p;
  6.         bool a = true;
  7.         bool b = false;
  8.         int x = 10;
  9.         int y = 5;
  10.         p = ((x | y) + (a + b));
  11.         cout << p;
  12.         return 0;
  13.     }

a) 0
b) 16
c) 12
d) 2
View Answer

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

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.

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]

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.