Examveda

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++?

A. max(5, 3)

B. 3

C. Compilation error due to missing function definition

D. max(3, 5)

Answer: Option A

Solution (By Examveda Team)

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.

Join The Discussion

Comments (1)

  1. Nashaib Akbar
    Nashaib Akbar:
    1 year 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.

Related Questions on Functions and Procedures in C plus plus