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. YesExplanation
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. FalseExplanation
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::vectorExplanation
std::vectorQ 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 falseExplanation
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 functionExplanation
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. 1Explanation
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. 0Explanation
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?
- #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
Show Answer
Answer:-B. 16Explanation
| 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
Leave a Reply