PrepGo

AP Computer Science A Practice Quiz: Constructors

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Test your understanding with short quizzes. This quiz has 10 questions to check your progress.

Question 1 of 10

What is the primary role of a constructor in a class?

All Questions (10)

What is the primary role of a constructor in a class?

A) To return a value after performing a calculation.

B) To set the initial state of an object by initializing its instance variables.

C) To define the behavior and methods an object can perform.

D) To store a reference to the newly created object.

Correct Answer: B

Based on the provided content, "A constructor is used to set the initial state of an object, which should include initial values for all instance variables."

If an instance variable of type `int` is declared but not explicitly initialized in a constructor, what value will it hold by default?

A) null

B) 1

C) 0

D) It will cause a compilation error.

Correct Answer: C

The provided content states, "The default value for an attribute of type int is 0."

Consider the following class definition: ```java public class Student { private String name; private int studentID; public Student() { studentID = 12345; } } ``` After the statement `Student s = new Student();` is executed, what is the value of the `name` instance variable?

A) "" (an empty string)

B) "name"

C) 0

D) null

Correct Answer: D

`String` is a reference type. The constructor only initializes `studentID`. Since `name` is not initialized, it receives its default value. According to the content, "The default value of a reference type is null."

Consider the `Car` class: ```java public class Car { private String model; private int year; public Car(String carModel, int carYear) { model = carModel; year = carYear; } } ``` Which statement correctly creates a `Car` object and initializes its state according to the constructor?

A) Car myCar = new Car();

B) Car myCar = new Car("Sedan", 2023);

C) Car myCar = new Car; myCar.model = "Sedan"; myCar.year = 2023;

D) Car("Sedan", 2023);

Correct Answer: B

The content states, "Constructor parameters, if specified, provide data to initialize instance variables." The constructor for `Car` requires a `String` and an `int` as parameters. Option B correctly provides these arguments during object creation to initialize the instance variables.

A `Team` class stores a list of player names. Consider this constructor: ```java public class Team { private String[] players; public Team(String[] initialPlayers) { this.players = initialPlayers; } } ``` The following code is executed: ```java String[] starters = {"Alice", "Bob", "Charlie"}; Team varsity = new Team(starters); starters[0] = "Zoe"; ``` What is the state of the `players` instance variable inside the `varsity` object after this code runs?

A) The `players` array is `{"Alice", "Bob", "Charlie"}`.

B) The `players` array is `{"Zoe", "Bob", "Charlie"}`.

C) The `players` instance variable is `null`.

D) The code results in a runtime error.

Correct Answer: B

The constructor assigns the instance variable `players` to the same memory reference as the `initialPlayers` parameter. Because a copy was not made, the instance variable holds a reference to the original `starters` object. When the original `starters` array is modified, the change is reflected in the `varsity` object's `players` variable.

According to the provided content, how should a constructor be written to properly handle a mutable object parameter, such as an array, to prevent external modifications to the object's state?

A) The constructor should assign the parameter's reference directly to the instance variable.

B) The constructor should declare the instance variable as `final`.

C) The constructor should initialize the instance variable with a copy of the object passed as a parameter.

D) The constructor should refuse to accept mutable objects as parameters.

Correct Answer: C

The content explicitly states, "When a mutable object is a constructor parameter, the instance variable should be initialized with a copy of the referenced object. In this way, the instance variable does not hold a reference to the original object."

Consider the `Book` class: ```java public class Book { private String title; private int pageCount; private boolean isHardcover; public Book(String bookTitle) { title = bookTitle; } } ``` An object is created with `Book myBook = new Book("A Great Novel");`. What are the values of the `pageCount` and `isHardcover` instance variables for the `myBook` object?

A) `pageCount` is 0, `isHardcover` is false.

B) `pageCount` is 0, `isHardcover` is true.

C) Both are `null`.

D) The code will not compile because not all instance variables are initialized.

Correct Answer: A

The constructor only initializes the `title`. The other instance variables, `pageCount` and `isHardcover`, are not explicitly initialized, so they receive their default values. The content states the default for `int` is 0 and the default for `boolean` is `false`.

In the context of a class and its constructors, what is the purpose of declaring instance variables?

A) To serve as temporary variables only within the constructor.

B) To define the attributes that will be initialized in the constructor to represent an object's state.

C) To store the return values of the class's methods.

D) To count the number of objects created from the class.

Correct Answer: B

The content states to "declare instance variables for the attributes to be initialized in the body of the constructors of a class." This indicates that instance variables represent the attributes that define an object's state.

A `Product` class has an instance variable `private double price;`. If a `Product` object is created using a no-argument constructor that does not explicitly set a value for `price`, what will be the initial value of the `price` attribute?

A) 0

B) 0.0

C) null

D) 1.0

Correct Answer: B

According to the provided content, "The default value of an attribute of type double is 0.0."

Which of the following best describes what it means for a constructor to "set the initial state of an object"?

A) It runs all the methods in the class once.

B) It assigns memory for the object on the heap.

C) It provides initial values for all the instance variables, defining the object's starting properties.

D) It connects the object to other objects in the program.

Correct Answer: C

The content states, "A constructor is used to set the initial state of an object, which should include initial values for all instance variables." This directly corresponds to defining the object's starting properties by giving values to its attributes.