AP Computer Science A • Unit 3.9

The this Keyword

Detailed answer explanations for the Unit 3.9 multiple-choice practice questions.

Q1C
Q2A
Q3D
Q4B
Q5C
Q6C
Q7B
Q8B
Q9A
Q10B
Q11A
Q12C
Q13B
Q14D
How to use this page: First try each question without looking at the explanation. Then compare your answer with the key and read why the other choices are not correct.

Answer Explanations

Q1

Why is this.name = name; useful in a constructor?

AIt creates a class variable.
BIt returns a value from the constructor.
CIt distinguishes the instance variable from the same-name parameter.
DIt compares the two names.
Correct answer: C. this.name names the object's variable, while the bare name names the parameter that is hiding it.
Explanation

When a parameter shares a name with an instance variable, the parameter wins inside the method. this is what lets the code reach past the parameter to the object's own variable, which makes it possible to give both the same descriptive name.

  • A is not correct: a class variable is created by a declaration with static, not by an assignment.
  • B is not correct: a constructor has no return type and returns nothing.
  • D is not correct: a single = assigns; comparison would need == or a method call.
Q2

Which expression refers to the current object's instance variable named score?

Athis.score
Bscore.this
Cstatic.score
Dcurrent(score)
Correct answer: A. this is a reference to the current object, so a dot and the variable name reach that object's member.
Explanation

this follows the same shape as any other object reference: the reference comes first, then the dot, then the member. Reading it as "this object's score" gives the order directly.

  • B is not correct: the reference and the member are reversed.
  • C is not correct: static is a keyword used in declarations and cannot appear before a dot.
  • D is not correct: this is written as a method call, and no such method is involved.
Q3

Is this available in a class method?

AYes, because all class methods share one current object.
BYes, if the method receives an object parameter.
CNo, because this is allowed only in constructors.
DNo, because a class method is not tied to one current object.
Correct answer: D. A class method runs without any object, so there is nothing for this to refer to.
Explanation

this exists only where a current object exists. That is why it is available in instance methods and constructors but not in class methods, which are called through the class rather than on an object.

  • A is not correct: class methods share no object at all, which is precisely the reason this is unavailable.
  • B is not correct: a parameter gives the method an object to work with, but it does not make that object the current one.
  • C is not correct: this is available in instance methods too, not only in constructors.
Q4

During lamp.setBrightness(7), what does this refer to inside setBrightness?

AThe value 7
BThe object referenced by lamp
CThe Lamp class
DThe method parameter
Correct answer: B. this refers to whichever object the method was called on, which here is the one lamp names.
Explanation

The value of this is decided by the call, not by the method's text. The same method called on a different object would have this refer to that object instead.

  • A is not correct: 7 is the argument, which arrives through the parameter rather than through this.
  • C is not correct: this refers to an individual object, not to the class it was created from.
  • D is not correct: the parameter is a separate variable holding the argument.
Q5

Inside Parcel, the call depot.accept(this) passes the current parcel. Which header can receive it?

Apublic void accept(int item)
Bpublic void accept(String item)
Cpublic void accept(Parcel item)
Dpublic void accept(static item)
Correct answer: C. Inside Parcel the value of this is a Parcel, so the parameter must have that type.
Explanation

Passing this is how an object hands itself to another object's method. Because it is an ordinary reference, the usual matching rule applies: the parameter's type has to be the type of the value being passed.

  • A is not correct: an int parameter cannot receive an object reference.
  • B is not correct: a String parameter cannot receive a Parcel either.
  • D is not correct: static is a keyword rather than a type, so this is not a valid parameter declaration.
Q6

Which line is invalid inside public static void reset() because there is no current object?

Aint x = 5;
Breturn;
Cthis.score = 10;
DSystem.out.println(5);
Correct answer: C. this has no meaning inside a class method, so the line is rejected.
Explanation

This applies the earlier rule to an actual line of code. The other three statements are all ordinary work that needs no object, which is why a class method can contain them without difficulty.

  • A is not correct: a local variable belongs to the method, so declaring one needs no object.
  • B is not correct: a bare return is valid in a void method of either kind.
  • D is not correct: printing a literal involves no instance member at all.
Q7

During Book b = new Book("Dune");, what does this refer to inside the constructor?

AThe string "Dune"
BThe new Book object referenced by b
CEvery Book object
DThe variable b itself
Correct answer: B. Inside a constructor, this refers to the object currently being created.
Explanation

The object exists while the constructor runs, even though the variable has not received the reference yet. That is what allows the constructor to assign to this.title and have the value be there once the statement finishes.

  • A is not correct: the string is the argument, which arrives through the constructor's parameter.
  • C is not correct: this refers to one object, the one being created by this call.
  • D is not correct: this refers to the object, not to the variable that will hold a reference to it.
Q8

No local variable or parameter is named total. Which two expressions refer to the same instance variable?

Atotal and total.this
Btotal and this.total
Cthis and total
Dstatic.total and this.total
Correct answer: B. With nothing hiding the instance variable, the bare name and the this form mean the same thing.
Explanation

this is required only when a nearer declaration is in the way. When no parameter or local variable shares the name, writing it is optional, and some programmers include it anyway to make the instance variable obvious.

  • A is not correct: total.this reverses the reference and the member.
  • C is not correct: this alone refers to the whole object, not to one of its variables.
  • D is not correct: static cannot appear before a dot as though it named something.
Q9

Inside Badge, which call passes the current badge to verify?

Ascanner.verify(this);
Bscanner.verify(new);
Cthis = scanner.verify();
Dscanner.this.verify();
Correct answer: A. this is passed as an argument in the same way any other reference would be.
Explanation

The method is called on scanner, and the argument is the object doing the calling. This pattern is how one object gives another object access to it.

  • B is not correct: new is a keyword that begins the creation of an object and is not a value on its own.
  • C is not correct: this cannot be assigned to; it always refers to the current object.
  • D is not correct: this is not a member of scanner, so it cannot follow that reference.
Q10

An object starts with points equal to 4. After setPoints(9), what remains stored?

public void setPoints(int points) {
    points = points;
}
A9
B4
C0
DA compile-time error occurs.
Correct answer: B. Both names refer to the parameter, so the instance variable is never written and keeps its starting value.
Explanation

This is the mistake this exists to prevent. Because the parameter hides the instance variable, the statement assigns the parameter to itself. Writing this.points = points; makes the left side refer to the object's variable and repairs the method.

  • A is not correct: 9 is the argument, which stays in the parameter and is discarded when the method ends.
  • C is not correct: the object already held a value, and nothing here sets it to zero.
  • D is not correct: assigning a variable to itself is legal, so the method compiles and runs.
Q11

Assume that value is an instance variable and that value() returns it. What is printed?

public Counter(int start) {
    this.value = start;
}

Counter a = new Counter(2);
Counter b = new Counter(5);
System.out.println(a.value() + " " + b.value());
A2 5
B5 5
C2 2
DThe code does not compile.
Correct answer: A. Each construction sets this.value on its own new object, so the two objects keep separate values.
Explanation

this means a different object on each of the two constructor calls. Because value is an instance variable, each object has its own copy, and neither assignment disturbs the other.

  • B is not correct: this treats the second construction as overwriting the first, which would require a shared class variable.
  • C is not correct: this treats the second object as never receiving its own value.
  • D is not correct: using this inside a constructor is valid, so the code compiles.
Q12

Assume that the constructor and value() work as shown in Question 11. What is printed?

public void setValue(int value) {
    this.value = value;
}

Counter a = new Counter(2);
Counter b = new Counter(5);
a.setValue(8);
System.out.println(a.value() + " " + b.value());
A2 5
B8 8
C8 5
D2 8
Correct answer: C. The mutator was called on a, so this refers to that object and only its value changes.
Explanation

Compare this method with the one in Question 10. The parameter still shares the instance variable's name, but here this on the left side directs the assignment to the object rather than back into the parameter, so the mutator does what its name promises.

  • A is not correct: these are the starting values, which would remain only if the mutator had changed nothing.
  • B is not correct: this treats value as shared by both objects, but an instance variable is not.
  • D is not correct: this applies the change to the wrong object; the call was made on a.
Q13

Assume that Parcel(boolean) stores ready, isReady() returns it, and setReady(boolean) updates it. The first method below is in Parcel; the second is in Depot. What is printed?

public void submit(Depot d) {
    d.mark(this);
}

public void mark(Parcel item) {
    item.setReady(true);
}

Parcel p = new Parcel(false);
p.submit(new Depot());
System.out.println(p.isReady());
Afalse
Btrue
Cnull
DThe call creates a second Parcel object.
Correct answer: B. this hands the very object p names to mark, so the mutator changes that object.
Explanation

Follow the reference through both calls. Inside submit, this is the parcel p refers to; that reference becomes item inside mark; and calling a mutator on item reaches the same object once more. Passing this creates an alias just as passing any other reference would.

  • A is not correct: false is the starting value, which the mutator replaces.
  • C is not correct: the variable refers to a real object throughout, and the value being printed is a boolean.
  • D is not correct: passing this passes a reference to the existing object; no second parcel is created.
Q14

Examine the following method. What happens when the class is compiled?

private int value;

public static void clear() {
    this.value = 0;
}
AThe method sets every object's value to 0.
BThe method creates a current object automatically.
CThe method compiles but changes no object.
DA compile-time error occurs because a class method has no this.
Correct answer: D. static means the method runs without an object, so this has nothing to refer to.
Explanation

This is the same rule the earlier questions stated, shown here as a class that will not compile. Removing static would give the method a current object and make the line valid; keeping it would require the method to receive an object as a parameter instead.

  • A is not correct: no method reaches every object at once, and this one does not compile at all.
  • B is not correct: objects are created only by new, never supplied automatically.
  • C is not correct: the error is caught at compile time, so the method never runs.