C++ Programming Quizs Question and Answers – Booleans

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

C++ language interview questions on “Booleans” along with answers, explanations and/or solutions:

Q 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

Show Answer Answer:-A. Yes
Explanation C++ has bool as a fundamental data type.

Q 2. What is the value of the bool?

bool is_int(789.54)

A. True
B. False
C. 1
D. 2

Show Answer Answer:-B. False
Explanation The given number is a double not an integer, so the function returns 0 which is boolean false.

Q 3. Find the odd one out.
A. std::vector<int>
B. std::vector<bool>
C. std::vector<long>
D. std::vector<short>

Show Answer Answer:-B. std::vector
Explanation std::vector 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.

Q 4. What happens when a null pointer is converted into bool?
A. an error is flagged
B. bool value evaluates to true
C. the statement is ignored
D. bool value evaluates to false

Show Answer Answer:-D. bool value evaluates to false
Explanation A pointer can be implicitly converted to a bool. A nonzero pointer converts to true and zero valued pointer converts to false.

Q 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

Show Answer Answer:-B. bool cannot be used as the type of the result of the function
Explanation Boolean can be used as a return value of a function.

Q 6. Evaluate the following.

   (false && true) || false || true

A. 0
B. 1
C. false
D. 2

Show Answer Answer:-B. 1
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.

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

Show Answer Answer:-C. 0
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.

Q 8. Which of the two operators ++ and — work for the bool data type in C++?
A. None
B. ++
C. —
D. ++ & —

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

Q 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

Show Answer Answer:-B. 16
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.

Q 10. 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;

else

return q;

}

main()

{

int a = 5, b = 10;

int k;

bool x = true;

bool y = f(a, b);

k =((a * b) + (x + y));

cout << k;

}

A. 52
B. 62
C. 72
D. 75

Show Answer Answer:-A. 52
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.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page