Bird
0
0

Given a class Wallet with a private decimal field 'amount', which of the following methods correctly adds a positive deposit to 'amount' and ignores non-positive values?

hard🚀 Application Q8 of 15
C Sharp (C#) - Classes and Objects
Given a class Wallet with a private decimal field 'amount', which of the following methods correctly adds a positive deposit to 'amount' and ignores non-positive values?
Apublic void Deposit(decimal value) { if (value >= 0) amount = value; }
Bpublic void Deposit(decimal value) { amount += value; }
Cpublic void Deposit(decimal value) { if (value > 0) amount += value; }
Dpublic void Deposit(decimal value) { if (value < 0) amount += value; }
Step-by-Step Solution
Solution:
  1. Step 1: Understand deposit logic

    Only positive values should increase 'amount'.
  2. Step 2: Evaluate each option

    public void Deposit(decimal value) { if (value > 0) amount += value; } adds value only if positive. public void Deposit(decimal value) { amount += value; } adds all values, including negatives. public void Deposit(decimal value) { if (value >= 0) amount = value; } replaces amount if value is zero or positive, which is incorrect. public void Deposit(decimal value) { if (value < 0) amount += value; } adds only negative values, which is wrong.
  3. Final Answer:

    public void Deposit(decimal value) { if (value > 0) amount += value; } -> Option C
  4. Quick Check:

    Deposit only positive amounts [OK]
Quick Trick: Add only if value > 0 [OK]
Common Mistakes:
MISTAKES
  • Adding negative or zero values
  • Replacing amount instead of adding
  • Ignoring condition to check positivity

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes