PrepGo

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

Consider the following `LightBulb` class and `Switch` class. ```java public class LightBulb { private boolean isOn; public LightBulb(boolean startState) { this.isOn = startState; } public void toggle() { this.isOn = !this.isOn; } public boolean getState() { return this.isOn; } } public class Switch { public void flick(LightBulb bulb) { bulb.toggle(); } } ``` The following code segment appears in a method in another class. ```java LightBulb myBulb = new LightBulb(false); Switch mySwitch = new Switch(); mySwitch.flick(myBulb); System.out.println(myBulb.getState()); ``` What is printed as a result of executing the code segment?

All Questions (9)

Consider the following `LightBulb` class and `Switch` class. ```java public class LightBulb { private boolean isOn; public LightBulb(boolean startState) { this.isOn = startState; } public void toggle() { this.isOn = !this.isOn; } public boolean getState() { return this.isOn; } } public class Switch { public void flick(LightBulb bulb) { bulb.toggle(); } } ``` The following code segment appears in a method in another class. ```java LightBulb myBulb = new LightBulb(false); Switch mySwitch = new Switch(); mySwitch.flick(myBulb); System.out.println(myBulb.getState()); ``` What is printed as a result of executing the code segment?

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`.

Consider the following `Pen` class. ```java public class Pen { private String color; public Pen(String c) { this.color = c; } public String getColor() { return this.color; } } ``` Now consider the following method in another class. ```java public void attemptChange(Pen p) { p = new Pen("Blue"); } ``` The following code segment is executed. ```java Pen myPen = new Pen("Red"); attemptChange(myPen); System.out.println(myPen.getColor()); ``` What is printed as a result of executing the code segment?

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 method `modifyObject` receives a reference to a mutable object as a parameter. Which of the following accurately describes what happens inside the method?

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.

Consider the `Book` and `Library` classes. ```java public class Book { private String title; public Book(String t) { this.title = t; } public void setTitle(String t) { this.title = t; } public String getTitle() { return this.title; } } public class Library { private Book featuredBook; public Library(Book b) { this.featuredBook = b; } public Book getFeaturedBook() { return this.featuredBook; } } ``` The following code is executed. ```java Library myLibrary = new Library(new Book("Original Title")); Book retrievedBook = myLibrary.getFeaturedBook(); retrievedBook.setTitle("New Title"); System.out.println(myLibrary.getFeaturedBook().getTitle()); ``` What is printed?

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".

Consider the following `Counter` class. ```java public class Counter { private int value; public Counter(int v) { this.value = v; } public int getValue() { return this.value; } public void increment() { this.value++; } } ``` And the following method in another class. ```java public Counter process(Counter c) { c.increment(); return c; } ``` The following code segment is executed. ```java Counter c1 = new Counter(10); Counter c2 = process(c1); System.out.println(c1.getValue() + " " + (c1 == c2)); ``` What is printed as a result of executing the code segment?

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.

Consider the `Student` class and the `TestGrader` class. ```java public class Student { private String name; public Student(String n) { this.name = n; } public void setName(String n) { this.name = n; } public String getName() { return this.name; } } public class TestGrader { public void updateName(Student s) { s.setName("Updated"); } public void reassignStudent(Student s) { s = new Student("Reassigned"); s.setName("New Name"); } } ``` The following code is executed. ```java Student student = new Student("Original"); TestGrader grader = new TestGrader(); grader.updateName(student); grader.reassignStudent(student); System.out.println(student.getName()); ``` What is printed?

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 method signature is `public String getLabel()`. When this method is called and its result is assigned to a variable, what is being returned and assigned?

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.

Consider the following `Robot` class and `Factory` class. ```java public class Robot { private int id; public Robot(int id) { this.id = id; } } public class Factory { public Robot buildRobot(int id) { return new Robot(id); } } ``` The following code segment is executed. ```java Factory factory = new Factory(); Robot r1 = factory.buildRobot(101); Robot r2 = factory.buildRobot(101); System.out.println(r1 == r2); ``` What is printed?

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`.

Consider the following `Container` class. ```java public class Container { private String content; public Container(String c) { this.content = c; } public String getContent() { return this.content; } public void setContent(String c) { this.content = c; } public static void swap(Container c1, Container c2) { String temp = c1.getContent(); c1.setContent(c2.getContent()); c2.setContent(temp); } } ``` The following code is executed. ```java Container box1 = new Container("Apples"); Container box2 = new Container("Oranges"); Container.swap(box1, box2); System.out.println(box1.getContent()); ``` What is printed?

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".