Methods: Passing and Returning References
Help Questions
AP Computer Science A › Methods: Passing and Returning References
Based on the code snippet above, which describes the state of acct after depositAndReturn executes?
public class BankAccount {
private String accountNumber;
private double balance;
public BankAccount(String accountNumber, double balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}
// Adds funds to this account
public void deposit(double amount) {
balance += amount;
}
// Deposits then returns this same account reference
public BankAccount depositAndReturn(double amount) {
deposit(amount);
return this;
}
public double getBalance() {
return balance;
}
public static void main(String[] args) {
BankAccount acct = new BankAccount("A200", 100.0);
BankAccount ref = acct.depositAndReturn(40.0);
ref.deposit(10.0);
System.out.println(acct.getBalance());
}
}
acct becomes null after returning this from depositAndReturn.
acct balance is 140.0 because only depositAndReturn changes it.
acct balance is 100.0 because ref modifies only its own copy.
acct balance is 150.0 because acct and ref reference the same object.
Explanation
This question tests AP Computer Science A skills: understanding methods passing and returning object references. In Java, when a method returns 'this', it returns a reference to the same object, meaning multiple variables can reference the same object in memory. In this scenario, acct starts with balance 100.0, depositAndReturn adds 40.0 (making it 140.0) and returns 'this' to ref, then ref.deposit(10.0) adds another 10.0 to the same object, resulting in a final balance of 150.0. Choice C is correct because both acct and ref reference the same BankAccount object in memory - any modifications through either reference affect the same object, so the balance becomes 150.0 after both deposits. Choice B is incorrect because it fails to account for the second deposit through ref, missing that both variables point to the same object. To help students: Use box-and-arrow diagrams to show how multiple references can point to the same object. Trace through the code step-by-step, updating the object's state to show cumulative effects.
In the provided class example, what will be the output after calling the method depositAndReturn?
public class BankAccount {
private String accountNumber;
private double balance;
public BankAccount(String accountNumber, double balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}
// Adds funds to this account
public void deposit(double amount) {
balance += amount;
}
// Deposits then returns this same account reference
public BankAccount depositAndReturn(double amount) {
deposit(amount);
return this;
}
public double getBalance() {
return balance;
}
public static void main(String[] args) {
BankAccount acct = new BankAccount("A600", 5.0);
BankAccount ref = acct.depositAndReturn(2.5);
ref.deposit(1.5);
System.out.println(acct.getBalance());
}
}
A compilation error occurs due to returning this.
5.0
9.0
7.5
Explanation
This question tests AP Computer Science A skills: understanding methods passing and returning object references. In Java, when a method returns 'this', it enables multiple operations on the same object, and all references to that object see the cumulative changes. In this scenario, acct starts with balance 5.0, depositAndReturn adds 2.5 (making it 7.5) and assigns the returned reference to ref, then ref.deposit(1.5) adds another 1.5 to the same object, resulting in a final balance of 9.0. Choice C is correct because both acct and ref reference the same BankAccount object - the first deposit increases the balance to 7.5, and the second deposit through ref increases it further to 9.0, which is displayed when accessing the balance through acct. Choice B is incorrect because it only accounts for the first deposit, missing that the second deposit also affects the same object since ref and acct point to the same instance. To help students: Trace the execution step-by-step, showing the balance after each operation. Emphasize that returning 'this' doesn't create a new object but provides another way to reference the same object.
Based on the code snippet above, which describes the state of acct after tryReassign executes?
public class BankAccount {
private String accountNumber;
private double balance;
public BankAccount(String accountNumber, double balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}
// Adds funds to this account
public void deposit(double amount) {
balance += amount;
}
// Attempts to reassign the parameter to a new object
public static void tryReassign(BankAccount target) {
target = new BankAccount("NEW", 999.0);
target.deposit(1.0);
}
public double getBalance() {
return balance;
}
public static void main(String[] args) {
BankAccount acct = new BankAccount("A500", 10.0);
tryReassign(acct);
System.out.println(acct.getBalance());
}
}
acct balance becomes 11.0 because deposit always affects the caller object.
acct balance remains 10.0 because only the parameter target was reassigned.
acct becomes null because target is set to a new object.
acct balance becomes 1000.0 because target reassigns the original reference.
Explanation
This question tests AP Computer Science A skills: understanding methods passing and returning object references. In Java, reassigning a parameter reference inside a method only affects the local parameter variable, not the original reference passed from the caller. In this scenario, tryReassign receives a copy of the reference to acct, but when it reassigns target to a new BankAccount object, only the local parameter target is changed - the original acct reference remains unchanged and still points to the original object with balance 10.0. Choice B is correct because reassigning the parameter target doesn't affect the caller's reference acct - the reassignment is local to the method, so acct still references the original object with balance 10.0. Choice A is incorrect because it assumes reassigning a parameter can change the caller's reference, which is impossible in Java since references are passed by value. To help students: Emphasize that Java is always pass-by-value, including for references - the value being passed is the reference itself. Use diagrams to show how reassigning a parameter creates a new arrow from the parameter to a different object, leaving the original reference unchanged.
Based on the code snippet above, what is returned by the method deposit? Describe its significance.
public class BankAccount {
private String accountNumber;
private double balance;
public BankAccount(String accountNumber, double balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}
// Adds funds to this account
public void deposit(double amount) {
balance += amount;
}
// Deposits then returns this same account reference
public BankAccount depositAndReturn(double amount) {
deposit(amount);
return this;
}
public double getBalance() {
return balance;
}
public static void main(String[] args) {
BankAccount acct = new BankAccount("A100", 50.0);
BankAccount ref = acct.depositAndReturn(25.0);
System.out.println(acct.getBalance());
System.out.println(ref.getBalance());
}
}
null, because depositAndReturn has no explicit return value.
A reference to the same modified BankAccount object (this).
A new BankAccount object with the updated balance.
A reference to a temporary local parameter that becomes invalid.
Explanation
This question tests AP Computer Science A skills: understanding methods passing and returning object references. In Java, the 'this' keyword refers to the current object instance, and returning 'this' from a method returns a reference to that same object, enabling method chaining. In this scenario, the depositAndReturn method first calls deposit to add funds, then returns 'this' - a reference to the current BankAccount object that was just modified. Choice C is correct because it identifies that the method returns a reference to the same modified BankAccount object, allowing the caller to continue working with the updated object through the returned reference. Choice A is incorrect because it suggests a new object is created and returned, which doesn't happen - the same object reference is returned after modification. To help students: Demonstrate method chaining with visual representations of how 'this' points to the current object. Show examples where returning 'this' enables fluent interfaces like builder patterns.
Based on the code snippet above, which describes the state of a and b after transferTo executes?
public class BankAccount {
private String accountNumber;
private double balance;
public BankAccount(String accountNumber, double balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}
// Moves money from this account into another account
public BankAccount transferTo(BankAccount other, double amount) {
this.balance -= amount;
other.balance += amount;
return other;
}
public double getBalance() {
return balance;
}
public static void main(String[] args) {
BankAccount a = new BankAccount("A", 50.0);
BankAccount b = new BankAccount("B", 10.0);
a.transferTo(b, 15.0);
System.out.println(a.getBalance() + "," + b.getBalance());
}
}
a is 35.0 and b is 10.0 because only this.balance can change.
a is 35.0 and b is 25.0 because both referenced objects were mutated.
a is 50.0 and b is 10.0 because objects are passed by value.
a is 65.0 and b is -5.0 because amount is added to the sender.
Explanation
This question tests AP Computer Science A skills: understanding methods passing and returning object references. In Java, methods can modify multiple objects when given references to them, and changes to object state through any valid reference are permanent. In this scenario, transferTo is called on object a, which decreases a's balance by 15.0 (from 50.0 to 35.0) and increases b's balance by 15.0 (from 10.0 to 25.0), effectively transferring funds between the two accounts. Choice B is correct because both referenced objects were mutated - the method modifies this.balance (object a) by subtracting the amount and other.balance (object b) by adding the amount, resulting in balances of 35.0 and 25.0 respectively. Choice A is incorrect because it claims objects are passed by value in a way that prevents modification, but Java passes references by value, allowing methods to modify the objects those references point to. To help students: Step through the code showing how 'this' refers to object a and 'other' refers to object b. Use visual representations of bank accounts with arrows showing money movement to make the transfer concept concrete.
In the provided class example, what is returned by the method transferTo? Describe its significance.
public class BankAccount {
private String accountNumber;
private double balance;
public BankAccount(String accountNumber, double balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}
// Moves money from this account into another account
public BankAccount transferTo(BankAccount other, double amount) {
this.balance -= amount;
other.balance += amount;
return other;
}
public double getBalance() {
return balance;
}
public static void main(String[] args) {
BankAccount a = new BankAccount("A", 80.0);
BankAccount b = new BankAccount("B", 20.0);
BankAccount result = a.transferTo(b, 30.0);
System.out.println(result.getBalance());
}
}
null, since returning an object after field updates is not allowed.
A new BankAccount containing the combined balances.
A reference to other, enabling chained operations on the recipient account.
A reference to this, because methods always return the caller object.
Explanation
This question tests AP Computer Science A skills: understanding methods passing and returning object references. In Java, methods can modify multiple objects through their references and return any reference, enabling flexible object interactions and method chaining. In this scenario, transferTo modifies both the calling object (this) by decreasing its balance and the parameter object (other) by increasing its balance, then returns the reference to other, which now has balance 50.0 (20.0 + 30.0). Choice A is correct because the method returns a reference to the other account after transferring funds to it, enabling continued operations on the recipient account - this design allows for chaining operations on the account that received the transfer. Choice B is incorrect because methods don't automatically return the caller object - the explicit 'return other' statement determines what reference is returned. To help students: Trace through the method showing how both objects are modified and how the return value is determined. Demonstrate practical uses of returning specific object references for method chaining and fluent interfaces.
Based on the code snippet above, how does the method deposit affect the object passed as target?
public class BankAccount {
private String accountNumber;
private double balance;
public BankAccount(String accountNumber, double balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}
// Adds funds to this account
public void deposit(double amount) {
balance += amount;
}
// Helper method: passes an object reference into a method
public static void applyDeposit(BankAccount target, double amount) {
target.deposit(amount);
}
public double getBalance() {
return balance;
}
public static void main(String[] args) {
BankAccount acct = new BankAccount("A400", 60.0);
applyDeposit(acct, 15.0);
System.out.println(acct.getBalance());
}
}
It throws a syntax error because methods cannot accept object references.
It reassigns target, making acct reference a different BankAccount.
It changes only a copied balance value; acct remains 60.0.
It changes target’s balance, which updates acct since they reference the same object.
Explanation
This question tests AP Computer Science A skills: understanding methods passing and returning object references. In Java, when an object reference is passed as a parameter, the method receives a copy of the reference value, but this copy still points to the same object in memory. In this scenario, applyDeposit receives a reference to the same BankAccount object that acct references, and calling target.deposit(amount) modifies the balance of that shared object from 60.0 to 75.0. Choice A is correct because the method changes the balance field of the object that both target and acct reference - since they point to the same object in memory, the change is visible through the acct reference. Choice B is incorrect because it suggests only a copy is modified, misunderstanding that while the reference is copied, both references point to the same mutable object. To help students: Draw diagrams showing how parameter passing creates a new reference variable that points to the same object. Demonstrate that modifications through any reference to an object are visible through all references to that object.