Choosing how to earn college credit? CLEP vs AP guide
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 3: Class Creation
Start Practice TestPractice Test
About Exam
hard

Encapsulated Mutator Logic

Mark
< Prev
Next >

A class utilizes a mutator method with conditional logic to control the internal state of a private field.

What is printed when the following Java program is compiled and run?

class Bank {

    private int balance = 100;

    public void deposit(int amount) {
        if (amount > 0) {
            balance += amount;
        }
    }

    public int getBalance() {
        return balance;
    }
}

public class QuestionTrace {

    public static void main(String[] args) {
        Bank b = new Bank();
        b.deposit(-50);
        b.deposit(20);
        System.out.print(b.getBalance());
    }
}
A

50

B

120

C

70

D

100

Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Not enough data yet to show leaderboard.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Instagram: @_apfive_|Privacy Policy|