AP Computer Science A Practice Quiz: Methods: Passing and Returning References of an Object
Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026
Test your understanding with short quizzes. This quiz has 9 questions to check your progress.
Question 1 of 9
All Questions (9)
A) true
B) false
C) null
D) The code will not compile.
Correct Answer: A
The `myBulb` object reference is passed to the `flick` method. The parameter `bulb` in the `flick` method becomes a copy of this reference, pointing to the same `LightBulb` object. The method then calls `bulb.toggle()`, which alters the state of the original `LightBulb` object. Therefore, the `isOn` instance variable changes from `false` to `true`.
A) Red
B) Blue
C) null
D) A NullPointerException is thrown.
Correct Answer: A
When `myPen` is passed to `attemptChange`, the parameter `p` is initialized with a copy of the reference to the "Red" `Pen` object. Inside the method, the line `p = new Pen("Blue")` reassigns the local parameter `p` to a new `Pen` object. This does not affect the original `myPen` reference in the calling code, which still points to the original "Red" `Pen` object. Therefore, "Red" is printed.
A) The method receives a deep copy of the original object, so any changes are isolated.
B) The method receives a copy of the reference, allowing it to alter the state of the original object.
C) The method receives the original reference itself, allowing the method to make the original variable point to a new object.
D) The object's state cannot be altered because the reference is final.
Correct Answer: B
Java passes arguments by value. For object types, the "value" being passed is the reference to the object. Therefore, the method's parameter is initialized with a copy of the reference. Since both the original reference and the parameter's copy point to the same object in memory, the method can use its reference to alter the state of that one object.
A) Original Title
B) New Title
C) null
D) The code causes a compilation error.
Correct Answer: B
The method `getFeaturedBook` returns the reference stored in the `featuredBook` instance variable. It does not return a copy of the object. Therefore, both `retrievedBook` and `myLibrary.featuredBook` point to the exact same `Book` object. When `retrievedBook.setTitle("New Title")` is called, it modifies the state of this single object. The final print statement retrieves the title from that same modified object, printing "New Title".
A) 10 false
B) 10 true
C) 11 false
D) 11 true
Correct Answer: D
The reference `c1` is passed to the `process` method. Inside the method, `c.increment()` modifies the state of the object that `c1` points to, changing its value from 10 to 11. The method then returns the reference `c`. The variable `c2` is assigned this returned reference. Since `c` was a copy of the reference `c1`, both `c1` and `c2` now point to the same, modified object. Thus, `c1.getValue()` is 11, and the comparison `c1 == c2` is true.
A) Original
B) Updated
C) Reassigned
D) New Name
Correct Answer: B
1. `grader.updateName(student)` is called. The method receives a reference to the `student` object and changes its name to "Updated". The state of the original object is now changed. 2. `grader.reassignStudent(student)` is called. The parameter `s` inside this method initially points to the same object. However, the line `s = new Student("Reassigned")` makes the local parameter `s` point to a completely new `Student` object. Any subsequent changes to `s` affect this new object, not the original one. The `student` variable in the calling scope is unaffected by this reassignment. Therefore, the name of the original `student` object remains "Updated".
A) A new copy of the `String` object from within the method.
B) A reference to a `String` object.
C) The raw sequence of characters, which is a primitive type.
D) The memory address of the `String` object as a primitive `long`.
Correct Answer: B
In Java, methods that have an object as a return type (like `String`, which is an object) always return a reference to that object, not a copy of the object itself. This reference is then assigned to the variable in the calling code.
A) true
B) false
C) The output is unpredictable.
D) The code fails to compile.
Correct Answer: B
Each call to `factory.buildRobot(101)` executes the line `return new Robot(id);`. The `new` keyword always creates a new, distinct object in memory and returns a reference to it. Even though the two `Robot` objects have the same `id` value, they are separate objects at different memory locations. The `==` operator, when used with object references, checks if the references point to the same object. Since `r1` and `r2` point to different objects, the expression `r1 == r2` evaluates to `false`.
A) Apples
B) Oranges
C) null
D) A NullPointerException is thrown.
Correct Answer: B
The `swap` method takes two `Container` references, `c1` and `c2`. When called with `swap(box1, box2)`, `c1` points to the same object as `box1` and `c2` points to the same object as `box2`. The method then accesses and modifies the state (the `content` field) of these two objects directly via their references. It successfully swaps the `content` strings between the two objects. Therefore, after the call, the object referenced by `box1` now contains "Oranges".