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);
}
Choose one option.
Show answer & explanation
Answer: B. 1

The expression y && z - 1 || (z = 5) evaluates left-to-right using operator precedence and short-circuit evaluation. First, y && z - 1 evaluates: y is 3 (truthy), and z - 1 is 5 (truthy), so 3 && 5 results in 1 (the logical AND of two non-zero values). Since the left side of || is already 1 (truthy), the right side (z = 5) is never evaluated due to short-circuit evaluation. Thus x is assigned 1.

Step-by-step Derivation:
Step-by-step evaluation:

  1. Initial values: x=5, y=3, z=6
  2. Evaluate: x = y && z - 1 || (z = 5)
  3. Operator precedence: - (subtraction) > && (logical AND) > || (logical OR)
  4. First evaluate z - 1 = 6 - 1 = 5
  5. Then y && 5 = 3 && 5:
    • In C, && returns 1 if both operands are non-zero (truthy)
    • 3 is non-zero (true), 5 is non-zero (true)
    • Result: 1
  6. Now evaluate: 1 || (z = 5)
    • Left side is 1 (true), so right side is never evaluated (short-circuit)
    • Result: 1
  7. x = 1
  8. printf("%d", x) outputs: 1