Skip to content
Sharp

Core Java · interview question

When recalculating a discount in a checkout system, what is a potential pitfall of reusing the same variable name within nested blocks?

Multiple choiceHardchapter-1-curatedpremiumcore-javascope-and-reassignment
Choose one answer
Answer and explanation

Correct answer

C. The inner variable may overshadow the outer variable, leading to incorrect calculations.

Why

Reusing the same variable name in nested blocks can lead to overshadowing, where the inner variable hides the outer one. This can cause logical errors, as calculations may use the wrong variable.

Test yourself on Core Java

A free test with instant, explained results. No account needed.

Start free test
All Core Java questions →