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 namedmax
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)
What is the correct syntax for defining a function in C++?
A. returnType functionName(parameters) { body; }
B. functionName(parameters) { returnType body; }
C. functionName(returnType, parameters) { body; }
D. returnType functionName(parameters, body) { }
What is the purpose of a function prototype in C++?
A. Determines the parameters of the function
B. Specifies the return type of the function
C. Provides the implementation of the function
D. Declares the function before it is defined
Which keyword is used to define a function in C++ that does not return any value?
A. return
B. null
C. void
D. None of the above
What is the correct way to call a function named "add" that takes two parameters in C++?
A. add(a, b);
B. add(int a, int b);
C. functionName = add(a, b);
D. add.functionName(a, b);
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.