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;
}
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:
- a = 4
- a ^ a = 4 ^ 4 = 0 (XOR: any number XOR itself equals 0)
- (a ^ a) = 0
- 0 && 0 = 0 (AND of two zeros is 0)
- 0 || a = 0 || 4
- Since a = 4 is non-zero (true in C), the OR operation returns true
- if condition is true, so printf("%d", a) executes
- Output: 4