Difficulty: Easy
Correct Answer: int a = 1, b = 2, c = 0; c = a < b ? a : 0;
Explanation:
Introduction / Context:This item asks for an equivalent one-liner using the conditional operator ?: that mirrors the behavior of setting c based on a < b with c initialized to 0.
Given Data / Assumptions:
Concept / Approach:With c initially 0, an equivalent expression is c = a < b ? a : 0; which assigns a when true, or 0 otherwise, preserving the initial default value when false.
Step-by-Step Solution:
Recognize the two outcomes: assign a when true; keep 0 when false. Write conditional expression: c = a < b ? a : 0; This matches the effect of the given snippet with c initially 0.Verification / Alternative check:If c had a nonzero initial value, the strict equivalence would be c = a < b ? a : c;. But for the provided initialization (c = 0), c = a < b ? a : 0; is behaviorally the same.
Why Other Options Are Wrong:
Common Pitfalls:Confusing a general equivalence (needing : c) with this specific snippet where c is known to be 0.
Final Answer:c = a < b ? a : 0;
Discussion & Comments