AP Computer Science A • Unit 1.13

Object Creation and Storage

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

Q1B
Q2B
Q3C
Q4A
Q5B
Q6A
Q7B
Q8C
Q9A
Q10D
Q11B
Q12B
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 constructor. Which of the following correctly creates a Building object and stores its reference?

public Building(int no, String nm)
ABuilding b = Building(3, "E");
BBuilding b = new Building(3, "E");
Cnew b = Building(3, "E");
DBuilding(3, "E") = new b;
Correct answer: B. Only new Building(3, "E") calls the constructor and creates an object whose reference is stored in b.
Explanation

The keyword new calls a constructor and creates an object. The declaration on the left supplies the reference variable and its type, and the assignment stores the produced reference in that variable.

  • A is not correct: leaving out new does not create an object, so this is not a constructor call and there is no reference to store.
  • C is not correct: the keyword new is not a type, so it cannot begin a declaration; the class name Building gives the reference variable its type.
  • D is not correct: a constructor call cannot appear on the left of an assignment; the reference produced by new is stored into the declared variable.
Q2

Consider the following declaration. Which of the following best describes the result of this declaration?

Building b;
AA new Building object is constructed.
BA reference variable named b is declared.
CThe constructor of Building is called.
DThe variable b stores an int value.
Correct answer: B. A declaration creates only a reference variable named b; no constructor runs and no object exists yet.
Explanation

A declaration creates no object. It gives you a label and nothing to put it on. The object only exists when you say new, and a constructor then runs to set it up.

  • A is not correct: a declaration creates no object; the Building object exists only when new calls a constructor to build it.
  • C is not correct: no constructor call appears here. A constructor runs when new creates an object, and this line contains no new expression.
  • D is not correct: the type of the variable is the class Building, so b stores an object reference or null rather than an int.
Q3

Consider the following constructor header. Which of the following constructor calls matches this header?

public Building(int no, String nm)
Anew Building("South Wing", 3)
Bnew Building(3)
Cnew Building(3, "South Wing")
Dnew Building()
Correct answer: C. The call supplies an int then a String, matching the header in count, order, and types.
Explanation

A constructor call must match the constructor signature, with the same number of arguments, in the same order, and compatible types. Check the argument count first, then the argument order, then the argument types.

  • A is not correct: the arguments appear in the wrong order, since the header declares the int parameter first and the String parameter second.
  • B is not correct: one argument is too few for a header that declares two parameters, so the argument count already fails to match.
  • D is not correct: a call with no arguments cannot match a signature that lists two parameter types, so the argument count fails immediately.
Q4

Assume the Building constructor prints "C". Consider the following code segment. Which of the following is printed?

System.out.print("A");
Building b = new Building(1, "E");
System.out.print("B");
AACB
BABC
CCAB
DBAC
Correct answer: A. A constructor finishes before execution continues, so the constructor's marker falls between the other two, giving ACB.
Explanation

The first statement prints before any object exists. The new expression then calls the constructor, which prints its own output. A constructor finishes setting up the new object before Java continues after the completed new expression.

  • B is not correct: this order would require the constructor to run after the following statement, but a constructor finishes before execution continues past new.
  • C is not correct: nothing runs before the first print statement, because the constructor is called only when the later new expression is evaluated.
  • D is not correct: the statements run in the order they are written, so output from the last line cannot appear first.
Q5

Consider the following code segment. Which statement is true?

Building a = new Building(1, "E");
Building b = new Building(1, "E");
Aa and b refer to the same object.
Ba and b refer to two separate objects.
CThe second line does not compile because the arguments repeat.
DBoth variables store primitive values directly.
Correct answer: B. Each new expression creates a separate object, so a and b refer to two different objects.
Explanation

Identical arguments do not produce one shared object. Each call to new Building(...) creates its own object, and each object stores its own instance variable values, so the two variables locate separate objects.

  • A is not correct: identical argument values do not merge the two objects; each new expression builds its own object with its own stored values.
  • C is not correct: repeating the same argument values is legal, and the second call matches the constructor signature just as the first one does.
  • D is not correct: a reference variable stores an object reference or null; it does not contain a second copy of the object.
Q6

Which statement about a constructor is correct?

AA constructor has the same name as the class and is used when a new object is created.
BA constructor must always have return type void.
CA constructor is called only after an object is destroyed.
DA constructor stores primitive values only.
Correct answer: A. A constructor has the same name as the class and runs when a new object is created.
Explanation

A constructor is a special declaration that runs when a new object is created and sets that object up. It has the same name as the class, has no return type, and often initializes instance variables.

  • B is not correct: a constructor has no return type at all, not even void; writing void makes the declaration an ordinary method instead.
  • C is not correct: a constructor runs when a new object is created, not at some later point after the object is no longer reachable.
  • D is not correct: a constructor can also receive object references as arguments, as the School constructor does when it stores a Building reference.
Q7

A class provides Building(int, String) and Building(int, String, int, int). Which constructor is called by new Building(4, "Center")?

ABoth constructors
BBuilding(int, String)
CBuilding(int, String, int, int)
DNeither constructor
Correct answer: B. The call passes an int and a String, so Java selects the signature Building(int, String).
Explanation

The call supplies two arguments, an int and a String. Java matches that argument list against the parameter list of each constructor, and only Building(int, String) has a parameter list that matches.

  • A is not correct: only one constructor runs for one new expression, because Java selects the single signature whose parameter list matches the call.
  • C is not correct: that signature lists four parameter types, and the call supplies only two arguments, so the argument count does not match.
  • D is not correct: one signature does match, because two arguments given as an int then a String fit Building(int, String) exactly.
Q8

In the statement below, which expression creates the object?

Building b = new Building(8, "G");
ABuilding b
Bb =
Cnew Building(8, "G")
D"G"
Correct answer: C. The expression new Building(8, "G") calls the constructor and creates the object in this statement.
Explanation

A declaration creates a reference variable, and a constructor call with new creates an object. Both appear in this one statement, but only the part beginning with new produces the object represented in the heap.

  • A is not correct: that part is the declaration, which creates a reference variable and gives it a type, but creates no object.
  • B is not correct: the assignment only stores the reference produced by the expression on the right; it does not create the object being located.
  • D is not correct: that is one argument passed to the constructor, and an argument supplies a value for a parameter rather than creating the object.
Q9

What does public void Building(int no) declare inside class Building?

AA method named Building
BA constructor with return type void
CA reference variable
DAn overloaded class
Correct answer: A. A constructor has no return type, so a header written with void declares a method named Building.
Explanation

A constructor has the same name as the class and no return type, not even void. Adding a return type turns the declaration into an ordinary method that happens to share the class name.

  • B is not correct: no constructor has a return type, so a declaration written with void cannot be a constructor of any kind.
  • C is not correct: a reference variable is declared by writing a class type followed by a name, while this header declares a parameter list instead.
  • D is not correct: overloading applies to constructors whose signatures differ within one class, and a class itself is not something that is overloaded.
Q10

Which statement best describes this code?

Building temp = null;
AA new Building object is constructed.
Btemp stores the primitive value zero.
CThe declaration is invalid because every reference must immediately name an object.
Dtemp stores null, so it currently has no object reference.
Correct answer: D. A reference variable can store null, so temp currently holds no reference to any Building object.
Explanation

A variable whose type is a class stores an object reference or null. Nothing is created here, because an object exists only when new calls a constructor to build it.

  • A is not correct: no object is created, since the statement contains no new expression and therefore no constructor call runs.
  • B is not correct: the type of the variable is the class Building, so it stores an object reference or null rather than a numeric value.
  • C is not correct: a reference variable is allowed to hold null, so it does not have to locate an object at the moment it is declared.
Q11

Which pair cannot be overloaded in the same class?

ABuilding(int, String)
Building(String, int)
BBuilding(int floors, int rooms)
Building(int rooms, int floors)
CBuilding(int)
Building(double)
DBuilding(int)
Building(int, int)
Correct answer: B. Both headers share the signature Building(int, int), because parameter names do not determine a constructor signature.
Explanation

A constructor signature consists of the constructor name and the ordered list of parameter types. Two constructors can be overloaded only when their signatures differ, and changing only the parameter names does not change a signature.

  • A is not correct: the parameter types appear in a different order, so the two signatures differ and both constructors can exist in one class.
  • C is not correct: the single parameter type differs between the two headers, so the signatures differ and Java can select between them.
  • D is not correct: the two headers list different numbers of parameters, so their signatures differ and Java can select between them by argument count.
Q12

Consider the following code segment. What does the School constructor receive for its Building parameter?

Building mainBuilding = new Building(1, "North Hall", 3, 12);
School campusSchool = new School("Orchid High", mainBuilding);
AA complete copy of the Building object
BA copy of the reference stored in mainBuilding
CThe primitive value stored in buildingNo
DA newly constructed second Building object
Correct answer: B. Java passes arguments by value, so the constructor receives a copy of the reference stored in mainBuilding.
Explanation

A reference variable stores a reference that lets Java locate an object; it does not contain a second copy of that object. Passing it copies the reference, and the constructor stores that reference in the building instance variable.

  • A is not correct: a reference variable does not contain a second copy of the object, so passing it cannot hand over a whole copy.
  • C is not correct: the argument is the reference variable itself, not one of the instance variables such as buildingNo inside the object.
  • D is not correct: no second new expression appears here, and only a new expression creates another Building object.