AP Computer Science A • Unit 3.4

Constructors

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

Q1B
Q2C
Q3A
Q4C
Q5B
Q6D
Q7B
Q8D
Q9D
Q10A
Q11B
Q12A
Q13C
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

What is the main purpose of a constructor?

ATo allocate memory without establishing object state
BTo initialize a new object
CTo make every instance variable publicly accessible
DTo return the new object reference explicitly
Correct answer: B. A constructor puts a newly created object's instance variables into their starting state.
Explanation

A constructor runs once, at the moment an object comes into existence, and its job is to leave that object ready to use. Everything else about constructors follows from this purpose.

  • A is not correct: establishing the object's state is precisely what a constructor is for.
  • C is not correct: the visibility of an instance variable is set by its own declaration, and the usual choice is private.
  • D is not correct: a constructor has no return type and returns nothing explicitly.
Q2

Which statement about constructors is true?

AA constructor is called using dot notation after an object exists.
BA constructor must return void.
CA constructor has the same name as the class and no return type.
DA constructor cannot have parameters.
Correct answer: C. These two signs together are what identify a constructor and distinguish it from a method.
Explanation

Both features have to be present. A member with the class's name but a return type written in front of it is an ordinary method that merely happens to share the name.

  • A is not correct: a constructor runs while the object is being created, so there is no existing object to call it on.
  • B is not correct: void is a return type, and a constructor has none at all.
  • D is not correct: parameters are how an object receives its starting values, so constructors commonly have them.
Q3

Which header could be a constructor for the Book class?

Apublic Book(String t)
Bpublic book(String t)
Cpublic void Book(String t)
Dpublic String Book(String t)
Correct answer: A. The name matches the class exactly and no return type appears before it.
Explanation

This question applies the previous rule to four near-misses. Each wrong option fails on exactly one of the two requirements, which makes it a useful check of whether both are being tested.

  • B is not correct: Java is case-sensitive, so a lowercase name does not match the class.
  • C is not correct: void is a return type, so this is a method that shares the class's name.
  • D is not correct: the same problem with a different return type.
Q4

In new Sensor(18.5), what happens to 18.5?

AIt is assigned automatically to every double instance variable.
BIt becomes the constructor's return value.
CIt is passed as an argument to the matching constructor parameter.
DIt determines the name of the constructor.
Correct answer: C. The value travels into the constructor through its parameter, exactly as an argument does for a method.
Explanation

Nothing about the value reaches an instance variable on its own. The constructor body has to contain an assignment that stores it, which is why the next question asks about the direction of that assignment.

  • A is not correct: matching is by position in the parameter list, not by type, and no automatic assignment takes place.
  • B is not correct: a constructor has no return type, so it produces no return value.
  • D is not correct: the constructor's name is fixed by the class name and cannot depend on an argument.
Q5

A class declares public Student(String n). Which statement is true?

AThe constructor must return a Student object explicitly.
BThe constructor must be called with one String argument.
CThe constructor can be called with no arguments.
DThe class automatically also has a default no-argument constructor.
Correct answer: B. A call must match the declared parameter list in number and type.
Explanation

Declaring any constructor also has a second consequence that catches people out: Java stops supplying the no-argument constructor. From that point every creation of the object must pass the arguments this constructor declares.

  • A is not correct: a constructor returns nothing explicitly; the object comes back from the new expression.
  • C is not correct: the declared parameter is not optional, so a call with no argument does not match.
  • D is not correct: the default constructor is supplied only when the class declares none of its own.
Q6

A constructor has a parameter myName. Which statement inside the constructor correctly initializes the instance variable name?

Areturn name;
BmyName = name;
CString name = name;
Dname = myName;
Correct answer: D. Assignment moves right to left, so the instance variable goes on the left and the parameter on the right.
Explanation

The direction is what the question tests. Reading the statement as "the instance variable becomes the parameter's value" makes the correct order easy to check, and the reversed version easy to spot.

  • A is not correct: a constructor returns nothing, and returning would not store the value anyway.
  • B is not correct: this copies the instance variable into the parameter, leaving the object unchanged.
  • C is not correct: this declares a new local variable that hides the instance variable, so the object is again left unchanged.
Q7

When Java supplies a default no-argument constructor, what must be true?

AAll instance variables are declared private.
BThe class declares no constructors.
CThe class declares only a constructor with parameters.
DA declared constructor has an empty body.
Correct answer: B. The default constructor appears only when the class writes none of its own.
Explanation

The rule is all or nothing. Writing a single constructor, whatever its parameters, means Java supplies no others, which is why adding a constructor with parameters can break code that previously created the object with no arguments.

  • A is not correct: the access designation of the instance variables has no bearing on this rule.
  • C is not correct: declaring a constructor with parameters is exactly the case where no default is supplied.
  • D is not correct: a declared constructor counts as a constructor whether or not its body is empty.
Q8

A constructor receives a mutable Timer t parameter. Which statement gives the instance variable timer a defensive copy?

Atimer = t;
Bt = timer;
CTimer timer = t;
Dtimer = new Timer(t.getSeconds());
Correct answer: D. Only this option creates a separate object, so the caller's Timer and the stored one are no longer the same object.
Explanation

Assigning a reference copies the arrow, not the object it points at. When the parameter's object is mutable, that leaves the caller holding a reference that can still change the object the constructor stored. Using new is what breaks that link.

  • A is not correct: this stores the same reference, so both the caller and the object share one Timer.
  • B is not correct: the assignment runs in the wrong direction and leaves the instance variable unset.
  • C is not correct: this declares a local variable that hides the instance variable, so nothing is stored in the object.
Q9

A class with no constructor declares double rate;, boolean open;, and String note;. What are their initial values?

A0, false, and ""
B0.0, true, and null
Cnull, false, and null
D0.0, false, and null
Correct answer: D. Numeric types start at zero, boolean starts at false, and reference types start at null.
Explanation

Instance variables always receive a starting value even when nothing assigns one, and that value depends on the type. Knowing these defaults is what makes it possible to predict an object's state when no constructor has set it.

  • A is not correct: a String starts at null rather than at an empty string, which is a different value.
  • B is not correct: a boolean starts at false, not true.
  • C is not correct: double is a primitive type, so it starts at zero rather than at null.
Q10

Which statement correctly calls public Book(String title, int pages)?

ABook b = new Book("Java", 250);
BBook b = new Book(250, "Java");
CBook b = Book("Java", 250);
Dnew Book b = ("Java", 250);
Correct answer: A. The new keyword precedes the class name, and the arguments appear in the order the parameters are declared.
Explanation

Creating an object combines two things in one statement: the new expression that builds the object and the declaration that stores the reference to it. Both parts have a fixed shape.

  • B is not correct: the arguments are in the wrong order, so neither one matches its parameter's type.
  • C is not correct: without new this reads as a method call rather than the creation of an object.
  • D is not correct: the variable name and the class name are in the wrong places and the class name is missing from the expression.
Q11

Examine the following declarations and statement. Which values make up the initial state of seat?

private String section;
private int row;

public Ticket(String s, int r) {
    section = s;
    row = r;
}

Ticket seat = new Ticket("A", 4);
Asection is "A" and row is 0.
Bsection is "A" and row is 4.
Csection is null and row is 4.
Dsection is null and row is 0.
Correct answer: B. Both assignments run in the correct direction, so each argument reaches its instance variable.
Explanation

This is the constructor working as intended, which is worth tracing carefully because the next two questions show what happens when one piece is missing. Arguments match parameters by position, and each parameter is then copied into the instance variable named on the left of the assignment.

  • A is not correct: 0 is the default for an int, but the second assignment replaces it.
  • C is not correct: null is the default for a String, but the first assignment replaces it.
  • D is not correct: these are the defaults for both types, which would remain only if the constructor assigned nothing.
Q12

Examine the following declarations and statement. What value remains stored in count's instance variable value?

private int value;

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

Counter count = new Counter(7);
A0
B7
CThe code does not compile.
Dnull
Correct answer: A. The assignment runs backwards, so the instance variable is never written and keeps its default value.
Explanation

This is the reversed assignment from the earlier question, shown in a program that compiles and runs. The parameter receives the instance variable's default and is then discarded when the constructor ends, so the object is left exactly as it started.

  • B is not correct: 7 is the argument, which is overwritten inside the constructor before it is ever stored.
  • C is not correct: both sides of the assignment are int, so the code compiles without complaint.
  • D is not correct: null is the default for a reference type, and value is a primitive.
Q13

Examine the following class and statement. What is the initial state of award?

public class Badge {
    private String label;
    private int points;
}

Badge award = new Badge();
Alabel is "" and points is 0.
Blabel is null and points is null.
Clabel is null and points is 0.
DThe statement does not compile because no constructor is written.
Correct answer: C. The class declares no constructor, so Java supplies the default one and both variables keep the defaults for their types.
Explanation

Two rules combine here. Because the class writes no constructor, the no-argument default is available, which is why the creation statement is legal. Because nothing assigns the instance variables, each one holds the default value for its own type.

  • A is not correct: a String defaults to null, which is not the same as an empty string.
  • B is not correct: null is a value for reference types only; an int cannot hold it.
  • D is not correct: the default constructor exists precisely because the class declares none.
Q14

The provided Counter class is mutable. Examine the constructor below. Why does the constructor create a new Counter?

public class ScoreCard {
    private Counter score;

    public ScoreCard(Counter c) {
        score = new Counter(c.getValue());
    }
}
ATo make Counter immutable
BTo provide a return value for the constructor
CTo assign the default value null to score
DTo keep score from referring to the original mutable object
Correct answer: D. The copy gives the ScoreCard its own object, so the caller's reference can no longer reach the stored one.
Explanation

This is the defensive copy from the earlier question shown inside a complete class. The parameter's object stays with the caller; the object stored in score is a separate one that starts with the same value. From then on, changes made through either reference leave the other untouched.

  • A is not correct: a class is immutable or not because of how it is written, and copying an object cannot change that.
  • B is not correct: a constructor has no return type, so it produces no return value.
  • C is not correct: the statement assigns a newly created object, which is the opposite of leaving the variable at null.