Evaluate the conditional (ternary) operator: what does this program print? #include<stdio.h> int main() { int k, num = 30; k = (num < 10) ? 100 : 200; printf("%d\n", num); return 0; }

Difficulty: Easy

Correct Answer: 30

Explanation:


Introduction / Context:
This item clarifies that the ternary operator result is stored in k, but the program prints the original variable num. It tests whether you are reading carefully rather than assuming the printed value equals the conditional result.


Given Data / Assumptions:

  • num = 30.
  • Ternary expression: (num < 10) ? 100 : 200.
  • Print statement: printf("%d\n", num); prints num, not k.


Concept / Approach:
Since num is 30, the condition num < 10 is false, so the expression yields 200 and assigns it to k. However, the program never prints k; it prints num, which remains unchanged at 30. Therefore, the output is 30 regardless of the value assigned to k.


Step-by-Step Solution:

Compute ternary: false → choose 200 → k = 200.No modification to num occurs.Print num → 30.


Verification / Alternative check:
Change the final line to printf("%d\n", k); to see 200 printed; or set num = 5 to observe k become 100 while num remains 5.


Why Other Options Are Wrong:

200/100: those are values assigned to k, not printed.500: not produced by any expression.Compilation error: the code is valid.


Common Pitfalls:
Assuming the printed variable is the same one assigned by the conditional expression; not tracking which identifier is passed to printf.


Final Answer:
30.

More Questions from Control Instructions

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion