Bird
Raised Fist0

Which of the following code snippets correctly sets the initial balance to zero before processing transactions?

easy🧠 Conceptual Q3 of Q15
LLD - Design — Splitwise (Expense Sharing)
Which of the following code snippets correctly sets the initial balance to zero before processing transactions?
Abalance = null
Bbalance += 0
Cbalance = 0
Dbalance == 0
Step-by-Step Solution
Solution:
  1. Step 1: Understand initialization

    To start balance calculation, the balance variable must be explicitly set to zero.
  2. Step 2: Analyze options

    balance = 0 correctly assigns zero to balance. balance += 0 adds zero to an undefined variable, which may cause error. balance = null assigns null, which is not a numeric zero. balance == 0 is a comparison, not assignment.
  3. Final Answer:

    balance = 0 -> Option C
  4. Quick Check:

    Initialization requires assignment, not comparison or addition [OK]
Quick Trick: Use '=' to assign zero, not '==' or '+=' [OK]
Common Mistakes:
MISTAKES
  • Using '==' instead of '=' for assignment
  • Adding zero to an uninitialized variable
  • Assigning null instead of zero

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes