PrepGo

AP Computer Science A Practice Quiz: this Keyword

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

Within an instance method or a constructor, what does the keyword `this` represent?

All Questions (9)

Within an instance method or a constructor, what does the keyword `this` represent?

A) A reference to the class in which the method is defined.

B) A reference to the superclass of the current object.

C) A reference to the current object whose method or constructor is being called.

D) A reference to a new object that will be created.

Correct Answer: C

The provided content states that 'the keyword this acts as a special variable that holds a reference to the current object—the object whose method or constructor is being called.'

Consider the following `Car` class: ```java public class Car { private String model; public void setModel(String model) { // Line X } } ``` Which line of code at `Line X` correctly assigns the value of the parameter `model` to the instance variable `model`?

A) model = model;

B) this.model = model;

C) Car.model = model;

D) new Car().model = model;

Correct Answer: B

To distinguish between the instance variable `model` and the parameter `model`, the keyword `this` is used. `this.model` is a self-referencing expression that refers to the instance variable of the current `Car` object.

Consider the following classes: ```java public class EventLogger { public void log(EventSource source) { System.out.println("Event from: " + source); } } public class EventSource { private EventLogger logger = new EventLogger(); public void triggerEvent() { // How to pass the current EventSource object to the logger? logger.log(/* argument */); } } ``` Which argument should be used in the `logger.log()` method call to pass the current `EventSource` object?

A) new EventSource()

B) EventSource.class

C) this

D) logger

Correct Answer: C

The content states that `this` can be used to pass the current object as an argument in a method call. Inside the `triggerEvent` method, `this` refers to the current `EventSource` instance.

What is the result of executing the following code? ```java public class Point { private int x; public Point(int x) { this.x = x; } public Point move(int dx) { this.x += dx; return this; } public int getX() { return x; } } // In another method: Point p = new Point(10); p.move(5).move(3); System.out.println(p.getX()); ```

A) 10

B) 13

C) 15

D) 18

Correct Answer: D

The `move` method uses a self-referencing expression `return this;` to return the current `Point` object. This allows method calls to be chained. The first call, `p.move(5)`, changes `x` to 15 and returns `p`. The second call, `p.move(3)`, changes `x` to 18. The final output is 18.

In which of the following contexts is the use of the `this` keyword valid to refer to an object instance?

A) Within a static method.

B) Within an instance method.

C) Outside of any class definition.

D) Within a static initializer block.

Correct Answer: B

The provided content specifies that `this` can be used 'within an instance method or a constructor'. Static methods are associated with the class, not a specific object instance, so `this` has no meaning there.

Consider the following code: ```java public class Employee { private String name; public Employee(String name) { this.name = name; } public boolean isSamePerson(Employee other) { return this == other; } } // In another method: Employee e1 = new Employee("Alice"); Employee e2 = new Employee("Bob"); boolean result = e1.isSamePerson(e2); ``` When the line `e1.isSamePerson(e2)` is executed, what does `this` refer to inside the `isSamePerson` method?

A) The `Employee` class.

B) The `e1` object.

C) The `e2` object.

D) A null reference.

Correct Answer: B

The keyword `this` always refers to the object on which the method was invoked. In the expression `e1.isSamePerson(e2)`, the method is called on the `e1` object, so `this` is a reference to `e1`.

Consider the following classes: ```java public class Manager { public void assignTask(Task t) { t.setManager(this); } } public class Task { private Manager assignedManager; public void setManager(Manager m) { this.assignedManager = m; } public Manager getManager() { return this.assignedManager; } } // In another method: Manager m1 = new Manager(); Manager m2 = new Manager(); Task taskA = new Task(); m1.assignTask(taskA); ``` After this code executes, what is the result of `taskA.getManager() == m1`?

A) false

B) true

C) It causes a NullPointerException.

D) It fails to compile.

Correct Answer: B

In the call `m1.assignTask(taskA)`, the `this` keyword inside the `assignTask` method refers to the `m1` object. The line `t.setManager(this)` passes `m1` as an argument to the `setManager` method of `taskA`. This sets `taskA`'s `assignedManager` field to the `m1` object. Therefore, `taskA.getManager()` returns `m1`, and the comparison `== m1` is true.

Which of the following best describes a 'self-referencing expression' in the context of the `this` keyword?

A) A method that calls itself recursively.

B) An expression, such as `this.name`, used by an object to refer to its own instance variables or methods.

C) An object that is an instance of its own class.

D) A variable that holds a reference to a static copy of the object.

Correct Answer: B

A self-referencing expression is one where an object refers to itself. Using `this` to access its own members (e.g., `this.name` or `this.myMethod()`) is the primary way to do this.

An object `obj1` of class `A` needs to pass itself to a method of an object `obj2` of class `B`. The method signature in class `B` is `public void process(A anObject)`. Which method call inside a method of class `A` correctly accomplishes this?

A) obj2.process(A);

B) obj2.process(new A());

C) obj2.process(this);

D) this.process(obj2);

Correct Answer: C

The requirement is for `obj1` to pass *itself* as an argument. Inside an instance method of `obj1`, the keyword `this` is the reference to `obj1`. Therefore, `obj2.process(this)` correctly passes the current object (`obj1`) to `obj2`'s `process` method.