Difficulty: Easy
Correct Answer: 0
Explanation:
Introduction / Context:
This question spotlights a classic C/C++ bug: using assignment (=
) instead of comparison (==
) inside an if
condition. It also asks you to trace how that bug changes the path and, consequently, the computed result in a simple area calculation scaffold.
Given Data / Assumptions:
AreaFinder(10, 10, 20)
so h=10
, l=10
, b=20
.Display
, the line if (l = 0)
assigns 0
to l
and yields false
.else
block executes with l
already changed to 0
.
Concept / Approach:
In C++, assignments in boolean contexts evaluate to the assigned value. Here, l = 0
makes l
zero and the condition evaluates to false, forcing execution into the else
branch. Since l
was just set to zero, result = l * b
becomes 0
regardless of b
.
Step-by-Step Solution:
Verification / Alternative check:
If the code had been if (l == 0)
, the if
would be false and the else would compute 10 * 20 = 200
. If the intention was circle area, the branch should check a flag parameter instead of clobbering l
.
Why Other Options Are Wrong:
l=10
before multiplication.
Common Pitfalls:
Overlooking side effects from assignment in conditions and assuming the variable value remains unchanged when the if
is false.
Final Answer:
0
Discussion & Comments