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:
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