PrepGo

AP Computer Science A Practice Quiz: Object Creation and Storage (Instantiation)

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

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

Question 1 of 16

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

All Questions (16)

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

A) To perform a calculation and return a value.

B) To create objects of the class.

C) To declare variables for the class.

D) To define the class's name.

Correct Answer: B

The provided content states, 'A class contains constructors that are called to create objects.' Their primary purpose is instantiation.

According to standard Java conventions, how is a constructor identified in a class definition?

A) It has the keyword `constructor` before its name.

B) It has the same name as the class.

C) It always has the name `init` or `create`.

D) It has no name, only parameters.

Correct Answer: B

The provided content explicitly states that constructors 'have the same name as the class.'

Which keyword is typically used to create an instance of an object?

A) create

B) object

C) new

D) instance

Correct Answer: C

The provided content mentions, 'An object is typically created using the keyword new followed by a call to one of the class's constructors.'

Consider a class named `Robot`. Which of the following correctly declares a variable `myRobot` that can hold a reference to a `Robot` object?

A) new Robot myRobot;

B) Robot myRobot;

C) object Robot myRobot;

D) myRobot = Robot;

Correct Answer: B

This demonstrates the requirement to 'develop code to declare variables of the correct types to hold object references.' The correct syntax is `ClassName variableName;`.

After the statement `Student s1 = new Student();` is executed, what does the variable `s1` contain?

A) The Student object itself.

B) A copy of the Student object.

C) The value null.

D) A reference to the Student object.

Correct Answer: D

The provided content states, 'A variable of a reference type holds an object reference'. The variable does not hold the object itself, but rather its location in memory (a reference).

Consider the following line of code: `Dog myDog;` If no object has been assigned to `myDog`, what value does it hold?

A) An empty Dog object

B) The integer 0

C) null

D) An empty string ""

Correct Answer: C

The provided content states, 'A variable of a reference type holds an object reference or, if there is no object, null.' Since the variable is declared but not initialized with a `new` object, it holds `null`.

Which line of code correctly declares a variable `myBook` of type `Book` and creates a new `Book` object for it to reference, assuming a no-argument constructor exists?

A) Book myBook = new Book();

B) new Book() = myBook;

C) Book myBook = Book();

D) myBook = new Book;

Correct Answer: A

This fulfills the requirement to 'develop code to create an object by calling a constructor.' The correct syntax combines declaration (`Book myBook`) with instantiation (`new Book()`) using the assignment operator and the `new` keyword.

Given a class `Car` with the following constructors: public class Car { public Car() { /* ... */ } public Car(String modelName) { /* ... */ } public Car(String modelName, int modelYear) { /* ... */ } } Which constructor is called by the statement `Car myCar = new Car("Sedan");`?

A) public Car()

B) public Car(String modelName)

C) public Car(String modelName, int modelYear)

D) No constructor is called; this is a syntax error.

Correct Answer: B

This question tests the ability to 'Identify, using its signature, the correct constructor being called.' The call `new Car("Sedan")` provides one String argument, which matches the signature `public Car(String modelName)`.

Assume a class `Computer` with a constructor `public Computer(int ram)`. Which of the following attempts to create a `Computer` object contains a syntax error?

A) Computer myComp = new Computer(16);

B) new Computer(8);

C) Computer myComp; myComp = new Computer(32);

D) Computer myComp = Computer(16);

Correct Answer: D

Object creation requires the `new` keyword. Option D is missing the `new` keyword, making it an invalid call to a constructor for the purpose of instantiation.

A class `Song` has a constructor that takes a `String` for the title. Which code snippet correctly declares a `Song` variable named `favorite` and initializes it to a song titled "Happy Day"?

A) Song favorite = new Song("Happy Day");

B) String favorite = new Song("Happy Day");

C) Song("Happy Day") = favorite;

D) Song favorite = "Happy Day";

Correct Answer: A

This question requires developing code to declare a variable of the correct type (`Song favorite`) and to create an object by calling the appropriate constructor (`new Song("Happy Day")`). Option A correctly combines both steps.

Consider the following code segment: // Assume a class named 'Game' exists. Game game1; Game game2 = new Game(); Which of the following statements is true?

A) `game1` holds a reference to a `Game` object, and `game2` holds `null`.

B) `game1` holds `null`, and `game2` holds a reference to a `Game` object.

C) Both `game1` and `game2` hold references to distinct `Game` objects.

D) Both `game1` and `game2` hold the value `null`.

Correct Answer: B

`game1` is only declared, so its value is `null`. `game2` is declared and instantiated with a new `Game` object, so it holds a reference to that object. This combines the concepts of declaration, instantiation, and the `null` value.

Given a class `Box` with the following constructors: public class Box { public Box(int size) { /* ... */ } public Box(String label) { /* ... */ } public Box(int w, int h) { /* ... */ } } Which of the following lines of code will cause a compilation error?

A) Box b1 = new Box(10);

B) Box b2 = new Box("Fragile");

C) Box b3 = new Box(5, 10);

D) Box b4 = new Box(10.5);

Correct Answer: D

The call `new Box(10.5)` passes a double-precision floating-point number as an argument. There is no constructor in the `Box` class that has a signature matching a single `double` parameter. The compiler cannot find a matching constructor to call.

What does it mean when an object reference variable has the value `null`?

A) The object it refers to has no data.

B) It does not currently refer to any object.

C) It refers to a special 'null object' that exists in memory.

D) The variable has not been declared.

Correct Answer: B

The provided content states that a reference variable holds `null` 'if there is no object.' It signifies the absence of an object.

If a class is defined as `public class Bicycle`, what must be the name of its constructors?

A) newBicycle

B) createBicycle

C) Bicycle

D) It can be any valid method name.

Correct Answer: C

The content specifies that constructors 'have the same name as the class.' Therefore, any constructor for the `Bicycle` class must be named `Bicycle`.

What are the two essential components, in order, that follow the `new` keyword when creating an object?

A) The variable name and a semicolon.

B) The class name and a call to one of its constructors.

C) The data type and the variable name.

D) A call to a constructor and the object's memory address.

Correct Answer: B

The content states, 'An object is typically created using the keyword new followed by a call to one of the class's constructors.' A constructor call consists of the class name followed by parentheses with any necessary arguments.

Consider the following code segment: // Assume a class named 'Pen' exists. Pen p1 = new Pen(); Pen p2 = p1; p1 = null; After this code is executed, what is the state of the variable `p2`?

A) It is `null`.

B) It refers to the same `Pen` object that `p1` originally referred to.

C) It refers to a new, distinct `Pen` object.

D) The code causes a runtime error.

Correct Answer: B

The line `Pen p2 = p1;` copies the reference from `p1` into `p2`. Both variables now point to the same object. The line `p1 = null;` changes only the `p1` variable to no longer refer to the object. The `p2` variable is unaffected and still holds the reference to the original `Pen` object.