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 y = 4, x;
    
    x = y >> 1;
    
    if ( x == 0)
        printf("Pass");
    else if (x == 2)
        printf("Fail");
    else
        printf("Not decided");
    
    return 0;
}
Choose one option.
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:

  1. y = 4 (binary: 0100)
  2. x = y >> 1 performs a right bit shift by 1 position
  3. 0100 >> 1 = 0010 (binary), which equals 2 (decimal)
  4. x = 2
  5. Check first condition: if (x == 0) → if (2 == 0) → false, skip
  6. Check second condition: else if (x == 2) → else if (2 == 2) → true, execute this branch
  7. printf("Fail") is executed
  8. Output: Fail