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 as follows: a ^ a (XOR of a with itself) = 0, then 0 && (a ^ a) = 0, then 0 || a = 4 (truthy). Since the if condition is true, printf prints the value of a, which is 4.
Step-by-step Derivation:
Step-by-step evaluation:
- a = 4 (binary: 100)
- Evaluate condition:
a ^ a && (a ^ a) || a a ^ a= 4 ^ 4 = 0 (XOR cancels identical bits)0 && (a ^ a)= 0 && 0 = 0 (AND with 0 is always 0)0 || a= 0 || 4 = 4 (OR with non-zero gives non-zero)if (4)→ condition is TRUE (non-zero value)- printf("%d", a) executes and prints: 4