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

What will be the output of the program given below?

Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.

What will be the output of the program given below?

#include <stdio.h>

int main()
{
    int x = 5, y = 3, z = 6;
    
    x += y && z - 1 || (z = 5);
    
    printf("%d", x);
}

**MCQ

Choose one option.
Show answer & explanation
Answer: A. 6

The expression y && z - 1 || (z = 5) evaluates left-to-right with short-circuit logic. First, y && z - 1 evaluates: y is 3 (true), and z - 1 is 5 (true), so the AND result is 1 (true). Since the left side of OR is true, the right side (z = 5) is never evaluated due to short-circuit evaluation. Thus, x += 1 makes x = 6. The assignment (z = 5) does not execute, so z remains 6.

Step-by-step Derivation:
Step-by-step evaluation of x += y && z - 1 || (z = 5) with initial values x=5, y=3, z=6:

  1. Operator precedence: && has higher precedence than ||, so parse as (y && (z - 1)) || (z = 5)
  2. Evaluate y && (z - 1):
    • y = 3, which is non-zero (true in C)
    • z - 1 = 6 - 1 = 5, which is non-zero (true in C)
    • true && true = 1
  3. Evaluate 1 || (z = 5):
    • Left operand is 1 (true)
    • Due to short-circuit evaluation, the right operand (z = 5) is NOT evaluated
    • 1 || anything = 1 (result is 1)
  4. Execute x += 1:
    • x = x + 1 = 5 + 1 = 6
  5. printf("%d", x) outputs: 6