ExamVeda
Login
Home
Introduction to C plus plus
?

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

Answer & Solution
Correct Answer: Option A
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
Examveda
Question posted by Examveda
Community

Join the Discussion

1 Comment
Gopal Ambore
Gopal Ambore 1 year ago
the output of the following code: cout