Bird
0
0

Consider the class below:

hard📝 Application Q9 of 15
Java - Classes and Objects
Consider the class below:
class Wallet {
  private int money = 0;
  public void addMoney(int amount) {
    if (amount > 0) {
      money += amount;
    }
  }
  public int getMoney() {
    return money;
  }
}
Which statement about the methods is correct?
ABoth methods are static and cannot access instance variables.
BaddMoney is static and getMoney is an instance method.
CaddMoney and getMoney are instance methods that operate on the object's money field.
DgetMoney modifies the money variable directly.
Step-by-Step Solution
Solution:
  1. Step 1: Identify method types

    Neither method is declared static, so both are instance methods.
  2. Step 2: Understand method behavior

    addMoney modifies the instance variable money; getMoney returns its value.
  3. Step 3: Evaluate options

    addMoney and getMoney are instance methods that operate on the object's money field. correctly describes both methods. Options B and C incorrectly label methods as static. getMoney modifies the money variable directly. is false because getMoney only returns money, not modifies it.
  4. Final Answer:

    addMoney and getMoney are instance methods that operate on the object's money field. -> Option C
  5. Quick Check:

    Instance methods access and modify instance variables. [OK]
Quick Trick: Instance methods access instance variables; static methods do not. [OK]
Common Mistakes:
  • Confusing static and instance methods
  • Assuming getters modify variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes