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
.(num < 10) ? 100 : 200
.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:
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:
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.
Discussion & Comments