PrepGo

AP Computer Science A Flashcards: Methods: Passing and Returning References of an Object

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Review key ideas with interactive flashcards. This set includes 10 cards to help you master important concepts.

Why is it important to know if an object is mutable when passing it to a method?
It is important because if the object is mutable, the method can permanently change the state of the original object, which may be an intended or unintended side effect.
Card 1 of 10

All Flashcards (10)

Why is it important to know if an object is mutable when passing it to a method?
It is important because if the object is mutable, the method can permanently change the state of the original object, which may be an intended or unintended side effect.
A method is defined as `void reassign(Dog d) { d = new Dog(); }`. If you pass a `Dog` object `myDog` to this method, will `myDog` refer to the new `Dog` after the method finishes?
No, the original `myDog` reference will be unaffected. The method only changes its local copy of the reference (`d`) to point to a new object.
What is initialized in a method's parameter when an object reference is passed as an argument?
The parameter is initialized with a copy of the object reference, not a new, independent copy of the object itself.
A method receives a reference to a `Student` object and calls a method on it, like `student.setGrade("A")`. Will the original `Student` object outside the method be affected?
Yes, because the parameter is a copy of the reference to the same object, any changes made to the object's state within the method will affect the original object.
How can a method or constructor alter the state of an object passed to it?
If the object is mutable, the method can use the passed reference (which points to the original object) to call methods that alter the object's state.
When a method returns an object, what is actually being returned?
The method returns the object reference itself, not a reference to a new copy of the object.
True or False: Passing an object to a method always creates a new, independent copy of that object for the method to use.
False. Passing an object to a method passes a copy of the reference, which points to the original object; it does not create a new copy of the object itself.
Explain the consequence of a method returning an object reference rather than a new copy.
Returning a reference means that the variable receiving the return value now points to the original object, allowing multiple references to access and modify the same object.
After `Car car2 = car1;` is executed, a method `paint(car2, "blue")` is called. If this method changes the color of the car object it receives, what is the color of `car1`?
The color of `car1` will also be blue, because both `car1` and `car2` are references pointing to the exact same `Car` object in memory.
What is meant by defining an object's behaviors through methods using object references?
This means writing methods in a class that take object references as parameters or return them, allowing objects to interact and modify each other's state.