Vidyalelo
C++ Programming · Q32

Introduction to C++

Programming · C++ Programming · question 32

Q32

What is the output of the following code: cout << (5 < 2) ? "True" : "False"; in C++?

A.
FALSE
Answer
B.
TRUE
C.
5
D.
2

Answer: Option A

Solution

Answer: Option A
Solution:
The expression used in the code is: cout << (5 < 2) ? "True" : "False";

This line appears to use the ternary conditional operator, but due to the missing parentheses around the conditional expression, the actual precedence rules of C++ affect the output.

In C++, the insertion operator << has higher precedence than the ternary operator ?:.

So, the expression is interpreted as: (cout << (5 < 2)) ? "True" : "False";

Here, 5 < 2 evaluates to false (i.e., 0)

So, cout << 0 is executed, which prints 0

Then the ternary operator evaluates 0 ? "True" : "False", which returns "False", but the result is not used or printed

Thus, the final output is: 0

Since "0" is not among the options but option C says 5, this indicates the question or options may be inconsistent

However, assuming the intention was to demonstrate operator precedence, the correct output should be: 0, not 5.

But given the provided options, none match this expected behavior correctly

So, the correct answer should actually be: None of the above, but if we must choose, Option C is incorrect