What is the output of the value() function?
Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.
int fun(int m)
{
m *= m;
return (m /= m);
}
int value()
{
int a, b;
a = (3, 4);
b = 5, 4;
return (a + b);
}
What is the output of the value() function?
Show answer & explanation
Answer: C. 9
The comma operator evaluates left-to-right and returns the rightmost value. In a = (3, 4), the expression evaluates to 4, so a = 4. In b = 5, 4, this is a sequence statement where b = 5 is executed, then 4 is discarded; b = 5. Therefore, a + b = 4 + 5 = 9.
Step-by-step Derivation:
Step-by-step execution:
- Line
a = (3, 4);uses the comma operator. The comma operator evaluates both expressions left-to-right and returns the value of the rightmost expression. So(3, 4)evaluates to4, anda = 4. - Line
b = 5, 4;is a sequence of two statements:b = 5(assignment) followed by, 4(the constant 4 is evaluated and discarded). Sob = 5. - Return
a + b = 4 + 5 = 9.
Note: The fun() function is defined but never called in value(), so it has no effect on the output.