| preferred AP College board partner for AP classes
AP Computer Science A/Unit 5: Writing Classes
Start Practice TestPractice Test
About Exam
medium Solved by 5 students
Private Member Access Within a Class
< Prev
Next >

Consider the following BankAccount class definition.

public class BankAccount {
    private double balance;
    private String accountNumber;
    
    public BankAccount(String num, double bal) {
        accountNumber = num;
        balance = bal;
    }
    
    public void transfer(double amount, BankAccount other) {
        if (amount > 0 && amount <= this.balance) {
            this.balance -= amount;
            other.balance += amount;
        }
    }
    
    public double getBalance() {
        return balance;
    }
}

Which statement best explains why the transfer method can directly access the private instance variable other.balance?

A

Methods can access private fields of any object of the same class type passed as a parameter

B

Private fields become accessible to all methods when objects are passed by reference

C

The transfer method has special privileges because it modifies multiple objects

D

The balance field is automatically made public when passed as a parameter

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

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Time
Score
#1kaisuki12 0m 00s 90
#2y.seong202701 3m 02s -192
#3bommasam00019 28m 05s -1,665
Items per page:
10
1 – 3 of 3
No comments yet. Be the first to comment!

AI Tutor

How can I help?

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