AP Computer Science A • Unit 1.14

Calling Instance Methods

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

Q1C
Q2A
Q3C
Q4B
Q5C
Q6A
Q7A
Q8B
Q9D
Q10B
Q11D
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

Consider the following code segment. Which of the following best describes what happens?

Building b = null;
System.out.println(b.label());
AThe code prints null.
BThe code prints an empty string.
CThe code throws a NullPointerException when the method is called.
DThe code creates a new Building object automatically.
Correct answer: C. A null reference refers to no object, so calling label through it throws a NullPointerException at run time.
Explanation

A reference variable that stores null does not refer to any object. The code compiles, but at run time there is no object for the method to run on, so the program stops with a NullPointerException.

  • A is not correct: printing happens only if the call succeeds, and the receiver refers to no object, so the program stops before any value reaches the output.
  • B is not correct: the method never runs, so no text of any kind is produced; the failure occurs at the moment the call is made.
  • D is not correct: new is what builds an object, and this segment never uses it, so a null reference is not replaced automatically.
Q2

Consider the following code segment. Assume that totalRooms returns floorCount * roomsPerFloor, and label returns the building number followed by the building name. Which statement describes the output?

Building b = new Building(4, "C", 2, 9);
System.out.println(b.totalRooms());
b.rename("CE");
System.out.println(b.label());
AThe first print statement displays 18; the second uses the updated name.
BThe first print statement displays 11; the second uses the original name.
CThe code does not compile because rename is void.
DThe code throws a NullPointerException.
Correct answer: A. totalRooms() returns 2 times 9, which is 18, and the later label() call sees the renamed object.
Explanation

Trace the calls in order and keep track of the object's state. The first call returns the product of the floor count and the rooms per floor. The call to rename then changes the name, so the later label() call reports the new one.

  • B is not correct: the returned total is the product of the two values, which is 18 rather than 11, and the rename runs before the second call.
  • C is not correct: storing the result of a void method is what fails to compile, and here rename is called as its own statement, which is the usual form.
  • D is not correct: the receiver refers to an object built with new, so there is no null receiver anywhere in this segment.
Q3

Consider the following code segment. Which of the following best describes the output?

Building b = new Building(7, "W", 3, 5);
String before = b.label();
b.rename("WA");
String after = b.label();
System.out.println(before);
System.out.println(after);
ABoth print statements use the original name.
BBoth print statements use the updated name.
CThe first print statement uses the original name; the second uses the updated name.
DThe code does not compile because an object cannot change state.
Correct answer: C. The first label is stored ahead of the rename, so it keeps the original name while the second call reads the updated name.
Explanation

A non-void method returns a value that can be stored in a variable. Once that value is stored, changing the object afterwards does not change the stored value, so the two printed lines differ.

  • A is not correct: the second call to label() happens after rename changes the object's name, so that call reports the updated name.
  • B is not correct: the first value was assigned before the rename, and a stored String value does not change when the object changes later.
  • D is not correct: a void method such as rename exists in order to change an object's state, and this segment compiles.
Q4

Consider the following code segment. Assume totalRooms() returns floorCount * roomsPerFloor. What is printed?

Building north = new Building(1, "N", 3, 12);
Building c = new Building(2, "C", 1, 8);
System.out.println(c.totalRooms());
A36
B8
C44
DThe code throws a NullPointerException.
Correct answer: B. The receiver is c, whose own values are 1 and 8, so 8 is printed.
Explanation

Each object stores its own instance variable values. The receiver written before the dot decides whose data the method uses, so the same method call on two different objects can produce two different results.

  • A is not correct: 36 is the total for the object referenced by north, but the call in the segment uses c, which stores different values.
  • C is not correct: each object keeps its own instance variable values, so the totals for the two objects are never combined into a single number.
  • D is not correct: both variables were assigned objects built with new, so the receiver is not null when the method is called.
Q5

Assume that b refers to a valid Building object. Which of the following is a correct instance method call?

ABuilding.label();
Blabel.b();
Cb.label();
Dnew label(b);
Correct answer: C. An instance method call names the object reference first, then the dot, then the method name with its parentheses.
Explanation

The general pattern is objectReference.methodName(arguments). The object reference comes first, the dot connects it to a member of that object, and the method name with its parentheses follows.

  • A is not correct: calling an instance method with a class name does not compile, because the method needs a particular object to run on.
  • B is not correct: the receiver belongs before the dot and the method name after it, and this choice reverses those two parts of the call.
  • D is not correct: new builds an object from a class, so it is not the way to ask an object that already exists for its label.
Q6

Assume that rename is a void instance method of the Building class. Which of the following is a valid use of the method?

Ab.rename("A");
BString result = b.rename("A");
CSystem.out.println(b.rename("A"));
Dint result = b.rename("A");
Correct answer: A. A void method returns no value, so a call to rename appears as its own statement.
Explanation

A void method performs an action but returns no value. A call to such a method usually appears as its own statement, and any attempt to store or print its result does not compile.

  • B is not correct: a void method produces nothing to store, so assigning its result to a String variable does not compile.
  • C is not correct: an output statement needs a value to display, and a void method returns none, so there is nothing to print.
  • D is not correct: the same problem applies with int, because no value comes back from a void method to be assigned.
Q7

Assume b refers to a valid Building object and totalRooms() returns an int. Which statement correctly stores the returned value?

Aint rooms = b.totalRooms();
BString rooms = b.totalRooms();
Cvoid rooms = b.totalRooms();
Db.totalRooms;
Correct answer: A. totalRooms() returns an int, so the variable that receives the returned value is declared int.
Explanation

A non-void method returns a value to the code that called it. Ask where that returned value goes: it may be assigned to a variable whose type matches what the method returns.

  • B is not correct: the returned value is an int rather than text, so a String variable cannot hold what this method returns.
  • C is not correct: void describes a method that returns no value, so it is not a type that a variable can be declared with.
  • D is not correct: a method call needs parentheses after the method name, so this line does not call totalRooms at all.
Q8

Assume rename has the method header void rename(String newName). Which call is valid?

Ab.rename();
Bb.rename("Center East");
Cb.rename(4);
DBuilding.rename("Center East");
Correct answer: B. The header shows that rename needs one String argument, and this call passes one through an object reference.
Explanation

A method header states what a call must supply. This header names one parameter of type String, so a valid call provides exactly one String argument, and it is made on an object reference.

  • A is not correct: the header requires one String argument, and this call supplies none, so it does not match the header.
  • C is not correct: 4 is not a String, so the argument does not match the parameter type named in the method header.
  • D is not correct: an instance method must be called on an object reference, and using the class name before the dot does not compile.
Q9

Consider the following code segment. Assume totalRooms() returns floorCount * roomsPerFloor. What is printed?

Building b = new Building(3, "S", 2, 6);
System.out.println(b.totalRooms() + 3);
A5
B9
C12
D15
Correct answer: D. totalRooms() returns 2 times 6, which is 12, and adding 3 prints 15.
Explanation

A returned value can be used directly inside an expression. The call is evaluated first, producing the product of the floor count and the rooms per floor, and that result is then used in the addition.

  • A is not correct: 5 leaves out the rooms per floor entirely and adds 3 to the floor count alone.
  • B is not correct: 9 adds 3 to the rooms per floor and ignores the floor count, so the total is not the one returned.
  • C is not correct: 12 is the value the method returns, but the expression still adds 3 to it before printing.
Q10

In the call b.totalRooms(), which expression is the receiver?

AtotalRooms
Bb
Cint
DBuilding
Correct answer: B. The receiver is the object reference written before the dot, which in this call is b.
Explanation

The receiver is the object being asked to run the method. Reading an instance method call from left to right gives the receiver first, then the dot, then the method being asked for.

  • A is not correct: totalRooms is written after the dot, so it names what is being asked for rather than who is being asked.
  • C is not correct: int is a type and does not appear in the call at all, while the receiver is the reference standing before the dot.
  • D is not correct: Building is the class name and does not appear in this call, and the receiver here is an object reference.
Q11

Assume label() is an instance method of Building. What happens when Java compiles Building.label();?

AThe call creates a new Building object.
BThe call automatically chooses the most recent Building object.
CThe call returns null.
DThe code does not compile because an instance method must be called on an object.
Correct answer: D. An instance method needs an object to run on, so calling label through the class name does not compile.
Explanation

An instance method belongs to an object and is called using an object reference. A static method is the kind called with a class name, so this line is rejected before the program ever runs.

  • A is not correct: only new builds an object, and writing a class name before the dot constructs nothing at all.
  • B is not correct: a receiver is never chosen automatically, so the code itself must name the object reference the method should run on.
  • C is not correct: the line never runs, because the problem appears when the code is compiled rather than while a value is returned.