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 y = 4, x;
x = y >> 1;
if ( x == 0)
printf("Pass");
else if (x == 2)
printf("Fail");
else
printf("Not decided");
return 0;
}
Show answer & explanation
Answer: B. Fail
The right shift operator (>>) shifts the bits of y (4) one position to the right. In binary, 4 is 100, and shifting right by 1 gives 010 (which is 2 in decimal). So x = 2, which matches the second condition (x == 2), printing "Fail". The code compiles without error and does not print "Pass" or "Not decided".
Step-by-step Derivation:
Step-by-step execution:
- y = 4 (binary: 0100)
- x = y >> 1 performs a right bit shift by 1 position
- 0100 >> 1 = 0010 (binary), which equals 2 (decimal)
- x = 2
- Check first condition: if (x == 0) → if (2 == 0) → false, skip
- Check second condition: else if (x == 2) → else if (2 == 2) → true, execute this branch
- printf("Fail") is executed
- Output: Fail