OA. free
Free
IBM Core Computer Science Core Computer Science Medium

Question 8 What would be the output of this method?

IBM technical mcq question, verified with a worked answer. Free to practise - no sign-up.

What would be the output of this method?

#include <stdio.h>

int main()
{
    int x= 10, y= 10;
    if (x=5)
        y--;
    ++x;
    printf("%d, %d", x--, y--);
}

Pick ONE option

Choose one option.
Show answer & explanation
Answer: C. 6, 9

The if condition uses assignment (x = 5) not comparison, so x becomes 5 and the condition evaluates to true (non-zero). This triggers y--, making y = 9. Then ++x increments x to 6. In printf, x-- outputs 6 (post-decrement uses current value before decrementing) and y-- outputs 9 (same logic), so the output is "6, 9".

Step-by-step Derivation:
Step-by-step execution:

  1. Initialize: x = 10, y = 10
  2. if (x = 5): Assignment operator assigns 5 to x; condition is true (5 ≠ 0)
    • After this: x = 5, y = 10
  3. y--: Post-decrement y
    • After this: x = 5, y = 9
  4. ++x: Pre-increment x
    • After this: x = 6, y = 9
  5. printf("%d, %d", x--, y--):
    • x--: prints current value of x (6), then decrements x to 5
    • y--: prints current value of y (9), then decrements y to 8
    • Output: "6, 9"