Skip to content
Marlbridge

AP® Computer Science A · Unit 3 · Topic 3.9

this Keyword: Practice Questions (Computer Science A 3.9)

Nine original Marlbridge practice questions on this: disambiguating variables, passing this as an argument, comparing objects with this, and static-context errors, with suggested rubrics.

These are original Marlbridge practice questions, not past exam questions. All scenarios are fictional. Mark points are a suggested Marlbridge rubric, not an official scoring guideline. No calculator is allowed. The classes Pet, Player, Team and Chain are printed in the study guide. All code was compiled and run.

Question 1 (multiple choice · foundation)

Inside an instance method, what does the keyword this hold?

  • (A) The name of the class
  • (B) A reference to the current object
  • (C) The value of the first parameter
  • (D) A reference to the class’s first object ever created
Answer and explanation

Answer: (B). this is a reference to the object whose method or constructor is running.

  • (A) is not a value this holds.
  • (C) is unrelated to parameters.
  • (D) is wrong: this refers to the current object, whichever it is.

Question 2 (multiple choice · foundation)

A constructor is public Pet(String name, int age). Which statement correctly stores the parameter name in the instance variable name?

  • (A) name = name;
  • (B) this.name = name;
  • (C) name = this.name;
  • (D) this = name;
Answer and explanation

Answer: (B). this.name is the instance variable and name alone is the parameter; assignment copies right to left.

  • (A) assigns the parameter to itself.
  • (C) copies the instance variable into the parameter, the wrong direction.
  • (D) does not compile: this cannot be assigned.

Question 3 (multiple choice · core)

Using the Pet class from the study guide (the constructor uses this.name = name;), what is printed?

Pet a = new Pet("Rex", 4);
Pet b = new Pet("Tia", 7);
Pet c = a;
System.out.println(a.sameObjectAs(c) + " " + a.sameObjectAs(b));

where sameObjectAs(Pet other) returns this == other.

  • (A) true true
  • (B) false false
  • (C) true false
  • (D) false true
Answer and explanation

Answer: (C). c is another name for the same object as a, so this == other is true. b is a different object, so it is false. The compiled program printed true false.

  • (A) treats two different objects as the same.
  • (B) forgets that c is an alias of a.
  • (D) reverses the results.

Question 4 (multiple choice · core)

olderThan(Pet other) returns this.age > other.age. Assume Pet a = new Pet("Rex", 4); Pet b = new Pet("Tia", 7);. What is printed by System.out.println(a.olderThan(b) + " " + b.olderThan(a));?

  • (A) true false
  • (B) false true
  • (C) true true
  • (D) false false
Answer and explanation

Answer: (B). In a.olderThan(b), this is a (age 4): 4 > 7 is false. In b.olderThan(a), this is b (age 7): 7 > 4 is true. The program printed false true.

  • (A) reads the ages the wrong way round.
  • (C) and (D) forget that this changes with the object before the dot.

Question 5 (multiple choice · core)

public class Bad
{
    private int value = 3;

    public static int twice()
    {
        return this.value * 2;
    }
}

What happens when this class is compiled?

  • (A) It compiles and twice() returns 6.
  • (B) It compiles but throws an exception when twice() is called.
  • (C) It does not compile, because a class method has no this reference.
  • (D) It does not compile, because value is private.
Answer and explanation

Answer: (C). Class methods have no this. The compiler reports non-static variable this cannot be referenced from a static context.

  • (A) and (B) assume the code compiles.
  • (D) is wrong: the method is inside the class, so private access is not the problem.

Question 6 (multiple choice · core)

Team team = new Team();
Player p = new Player("Maya");
p.becomeCaptainOf(team);
System.out.println(team.getCaptain() == p);

where becomeCaptainOf(Team t) contains t.setCaptain(this); and setCaptain stores its parameter in the instance variable captain. What is printed?

  • (A) true
  • (B) false
  • (C) null
  • (D) Nothing: the code does not compile.
Answer and explanation

Answer: (A). this inside becomeCaptainOf is the same object as p. The team stores that reference, so getCaptain() returns the object that p refers to, and == is true. The compiled program printed true.

  • (B) would need the team to store a copy.
  • (C) is not a possible output of ==.
  • (D) is wrong: the code compiles.

Question 7 (constructed response · core)

Write a class Badge with instance variables owner (a String) and level (an int).

(a) Write a constructor Badge(String owner, int level) that uses this so that the parameters and instance variables keep the same names.

(b) Write a method boolean outranks(Badge other) that returns whether this badge’s level is greater than the other badge’s level.

(c) State the output of new Badge("Ola", 5).outranks(new Badge("Sam", 3)).

Answer and explanation
public class Badge
{
    private String owner;
    private int level;

    public Badge(String owner, int level)
    {
        this.owner = owner;
        this.level = level;
    }

    public boolean outranks(Badge other)
    {
        return this.level > other.level;
    }
}

(c) true, because 5 > 3.

Suggested Marlbridge rubric (5 marks).

Mark Requirement
1 Two private instance variables with the right types
1 Public constructor with no return type and parameters with the same names
1 this.owner = owner; and this.level = level; in the correct direction
1 outranks is a public boolean method comparing the levels
1 Part (c): true

Question 8 (constructed response · core)

A Chain class has the method

public Chain add(int n)
{
    total += n;
    return this;
}

and getTotal() returns total (initially 0).

(a) State the output of the code below.

Chain c = new Chain();
Chain d = c.add(2).add(3);
System.out.println(c.getTotal() + " " + d.getTotal() + " " + (c == d));

(b) Explain why c == d has that value.

(c) Explain what this refers to in the second call to add.

Answer and explanation

(a) 5 5 true. This was confirmed by running the code.

(b) add returns this, a reference to the object it was called on. Both calls therefore act on the same object, and d is assigned a reference to that object, so c and d refer to the same object.

(c) The object returned by the first call, which is the same object as c.

Suggested Marlbridge rubric (4 marks).

Mark Requirement
1 Part (a): 5 5 true
1 Part (b): add returns a reference to the current object
1 Part (b): c and d are aliases of one object
1 Part (c): this is the object returned by the first call (the same object as c)

Question 9 (constructed response · stretch)

A programmer writes this constructor and finds that every Pet has name equal to null.

public Pet(String name, int age)
{
    name = name;
    this.age = age;
}

(a) Explain what is wrong and say which instance variable is set correctly.

(b) Fix it using this.

(c) Explain what this refers to when the constructor runs for new Pet("Rex", 4).

Answer and explanation

(a) name = name; assigns the parameter to itself because the parameter hides the instance variable, so the instance variable name keeps its default null. this.age = age; is correct, so age is set to 4.

(b) Change the first line to this.name = name;.

(c) The new Pet object that is being constructed.

Suggested Marlbridge rubric (4 marks).

Mark Requirement
1 Part (a): the parameter hides the instance variable, so it is assigned to itself
1 Part (a): age is set correctly (it already uses this)
1 Part (b): this.name = name;
1 Part (c): the object under construction

How did you do?

AP® and Advanced Placement® are trademarks registered by the College Board, which is not affiliated with, and does not endorse, this website. Marlbridge is an independent tuition provider. It is not an authorized AP course provider, an AP school or an exam centre, and these resources are not official College Board materials. Reading them or doing simulations does not meet the laboratory requirement of an AP science course.