OA. free
Free
Qualcomm Embedded Systems & Hardware Embedded Systems & Hardware Medium

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?

Choose one option.
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:

  1. 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 to 4, and a = 4.
  2. Line b = 5, 4; is a sequence of two statements: b = 5 (assignment) followed by , 4 (the constant 4 is evaluated and discarded). So b = 5.
  3. 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.