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:
Step 1: Understand initialization
To start balance calculation, the balance variable must be explicitly set to zero.
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.
Final Answer:
balance = 0 -> Option C
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
Master "Design — Splitwise (Expense Sharing)" in LLD
9 interactive learning modes - each teaches the same concept differently