ExamVeda
Login
Home
Functions and Procedures in C plus plus
?

What will be the output of the following code: int max(int a, int b) { return (a > b) ? a : b; } cout << max(5, 3); in C++?

Answer & Solution
Correct Answer: Option A
The given code defines a function named max which takes two integers a and b as input and returns the maximum of the two. In the main function, max(5, 3) is called, but it is directly printed to the output without any calculation. Therefore, the output will be max(5, 3), representing the function call.
Examveda
Question posted by Examveda
Community

Join the Discussion

2 Comments
Saad Mansoor
Saad Mansoor 8 months ago
The given answer (Option A: max(5, 3)) is incorrect.

The function:
int max(int a, int b) {
return (a > b) ? a : b;
}

uses the ternary operator to return the greater of the two values.

When calling:
cout 3 is true

So the function returns 5

Therefore, the output will be: 5
Since 5 is not listed in the options, none of the given options are correct.
Nashaib Akbar
Nashaib Akbar 2 years ago

The given code snippet defines a function max that takes two integer arguments a and b and returns the maximum of the two values using the ternary conditional operator (a > b) ? a : b.

Then, max(5, 3) is called in the cout statement.

Let's evaluate max(5, 3):

The first argument is 5, and the second argument is 3.
Since 5 > 3 is true, the expression (a > b) ? a : b evaluates to a, which is 5.
So, the output of the code will be: 5 Option B is wrong correct it by writing 5 as correct answer.