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 a = 4;
    
    if ( a ^ a && (a ^ a) || a)
        printf("%d", a);
    
    return 0;
}
Choose one option.
Show answer & explanation
Answer: D. 4

The condition evaluates to true because of the || (OR) operator. The expression breaks down as: (a ^ a && (a ^ a)) || a. The left side (a ^ a && (a ^ a)) is 0 && 0 = 0 (XOR of equal values is 0), but the right side a is 4 (non-zero, hence true). Since 0 || 4 evaluates to true, printf prints the value of a, which is 4.

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

  1. a = 4
  2. a ^ a = 4 ^ 4 = 0 (XOR: any number XOR itself equals 0)
  3. (a ^ a) = 0
  4. 0 && 0 = 0 (AND of two zeros is 0)
  5. 0 || a = 0 || 4
  6. Since a = 4 is non-zero (true in C), the OR operation returns true
  7. if condition is true, so printf("%d", a) executes
  8. Output: 4