Skip to content
Sharp

Core Java · interview question

In an e-commerce checkout system, how would you structure nested conditions to check if an item is in stock and if the user has enough balance?

Multiple choiceMediumchapter-1-curatedpremiumcore-javaconditions-and-branching
java
boolean inStock = true;
double userBalance = 150.0, itemPrice = 100.0;
if (inStock) { if (userBalance >= itemPrice) { checkout(); } }
Choose one answer
Answer and explanation

Correct answer

D. If (inStock) { if (userBalance >= itemPrice) { checkout(); } }

Why

Checking if the item is in stock first is logical because it avoids unnecessary balance check if the item is unavailable. Nested ifs provide clear separation of conditions.

Test yourself on Core Java

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

Start free test
All Core Java questions →