What this quiz covers
This quiz focuses on Object Creation And Storage Instantiation, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science a.
Consider the following class; what method would you call to achieve starting the engine?
public class Car {
private String make;
private String model;
// Constructor creates a Car object with identifying information.
public Car(String make, String model) {
this.make = make;
this.model = model;
}
// startEngine prints a message using stored fields.
public void startEngine() {
System.out.println("Engine started: " + make + " " + model);
}
public static void main(String[] args) {
Car myCar = new Car("Ford", "Focus");
// The object is used via its reference.
}
}
AP Computer Science a Quiz
Practice Object Creation And Storage Instantiation in AP Computer Science a with focused quiz questions that help you check what you know, review explanations, and build confidence with test-style prompts.
This quiz focuses on Object Creation And Storage Instantiation, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science a.
Try each quiz question before looking at the correct answer. Use the explanations to review missed ideas, then come back to similar questions until the pattern feels familiar.
Consider the following class; what method would you call to achieve starting the engine?
public class Car {
private String make;
private String model;
// Constructor creates a Car object with identifying information.
public Car(String make, String model) {
this.make = make;
this.model = model;
}
// startEngine prints a message using stored fields.
public void startEngine() {
System.out.println("Engine started: " + make + " " + model);
}
public static void main(String[] args) {
Car myCar = new Car("Ford", "Focus");
// The object is used via its reference.
}
}
Explanation: This question tests AP Computer Science A skills in object creation and storage (instantiation). Object instantiation in Java involves using the 'new' keyword followed by a constructor call for the class, which allocates memory and initializes the object. In the provided code snippet, a Car object named 'myCar' has been created, and the task is to call the startEngine method on this instance. Choice B is correct because it uses proper dot notation 'myCar.startEngine()' to invoke the instance method on the object reference. Choice A is incorrect because it attempts to call startEngine as a static method on the Car class rather than on the instance, and Choice C is incorrect because 'engineStart' is not a method defined in the Car class. To help students: Emphasize the difference between static and instance method calls, and stress that method names must match exactly as defined in the class. Practice using object references to invoke instance methods rather than trying to pass objects as parameters (Choice D).
Given the code below, which line of code correctly creates an object of ArrayList?
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
// ArrayList stores a dynamic list of scores.
// The list reference stores the object, then add manipulates its contents.
// Example use: scores.add(100);
}
}
```
Explanation: This question tests AP Computer Science A skills in object creation and storage (instantiation). Object instantiation in Java involves using the 'new' keyword followed by a constructor call for the class, which allocates memory and initializes the object. In the provided code snippet, the task is to create an ArrayList that will store Integer objects, which requires understanding Java's generics and wrapper classes. Choice B is correct because it follows the correct syntax 'new ArrayList()' using the Integer wrapper class since ArrayLists cannot store primitive types like int. Choice A is incorrect because it attempts to use the primitive type 'int' in the generic declaration, which is not allowed in Java generics - wrapper classes must be used instead. To help students: Emphasize that collections like ArrayList require wrapper classes (Integer, Double, Boolean) rather than primitives (int, double, boolean). Practice declaring and instantiating generic collections with proper type parameters.
Consider the following class; what method would you call to achieve withdrawing money?
public class BankAccount {
private double balance;
// Constructor initializes the balance
public BankAccount(double startingBalance) {
balance = startingBalance;
}
// Method subtracts money from the account
public void withdraw(double amount) {
balance -= amount;
}
public static void main(String[] args) {
// Object is instantiated, then withdraw() changes its stored balance
BankAccount acct = new BankAccount(200.0);
// Invoke the method here
}
}
Explanation: This question tests AP Computer Science A skills in object creation and storage (instantiation). Object instantiation in Java involves using the 'new' keyword followed by a constructor call for the class, which allocates memory and initializes the object. In the provided code snippet, a BankAccount object named acct is already created, and the task is to call the withdraw method with a parameter. Choice B is correct because it uses proper dot notation (acct.withdraw(25.0)) to invoke the instance method on the created object with the required double parameter. Choice D is incorrect because it uses 'withDraw' with incorrect capitalization - Java method names are case-sensitive and the method is defined as 'withdraw' with lowercase 'd'. To help students: Emphasize that Java is case-sensitive for all identifiers including method names. Practice careful attention to exact spelling and capitalization when calling methods.
Consider the following class and main method; how do you create an instance of BankAccount?
public class BankAccount {
private String owner;
private double balance;
// Constructor sets up a new account with an initial balance.
public BankAccount(String owner, double initialBalance) {
this.owner = owner;
balance = initialBalance;
}
// deposit adds money to the account.
public void deposit(double amount) {
balance += amount;
}
public static void main(String[] args) {
// The account object is created and then manipulated.
// Example method invocation on an instantiated object:
// acct.deposit(50.0);
}
}
Explanation: This question tests AP Computer Science A skills in object creation and storage (instantiation). Object instantiation in Java involves using the 'new' keyword followed by a constructor call for the class, which allocates memory and initializes the object. In the provided code snippet, the class BankAccount is instantiated with a constructor requiring a String owner and a double initialBalance. Choice A is correct because it uses proper syntax 'new BankAccount("Ava", 100.0)' with both required parameters in the correct order and types. Choice B is incorrect because it only provides one parameter when two are required, and Choice C is incorrect because it omits the essential 'new' keyword. To help students: Stress the importance of the 'new' keyword for object creation and remind them that Java is case-sensitive (Choice D incorrectly uses 'bankAccount' with lowercase 'b'). Encourage students to always check constructor parameter requirements before instantiation.
Given the code below, which line of code correctly creates an object of Car?
public class Car {
private String make;
private String model;
// Constructor initializes a specific car.
public Car(String make, String model) {
this.make = make;
this.model = model;
}
// startEngine simulates starting the car.
public void startEngine() {
System.out.println("Engine started: " + make + " " + model);
}
public static void main(String[] args) {
// The Car object is created and then used to start the engine.
// Example method invocation on an instantiated object:
// myCar.startEngine();
}
}
Explanation: This question tests AP Computer Science A skills in object creation and storage (instantiation). Object instantiation in Java involves using the 'new' keyword followed by a constructor call for the class, which allocates memory and initializes the object. In the provided code snippet, the class Car is instantiated with a constructor that requires two String parameters: make and model. Choice B is correct because it follows the correct syntax 'new Car("Toyota", "Corolla")' and provides both required parameters matching the constructor definition. Choice A is incorrect because it only provides one parameter when the constructor requires two, and Choice C is incorrect because it's missing the 'new' keyword which is essential for object instantiation. To help students: Emphasize matching constructor parameters exactly with their definitions in both number and type. Practice identifying constructor signatures and understanding that Java is case-sensitive (Choice D uses lowercase 'car' instead of 'Car').
Given the code below, what method would you call to achieve printing the receipt?
public class Customer {
private String name;
// Constructor sets up a Customer object.
public Customer(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class Order {
private Customer customer;
// Constructor stores an existing Customer reference.
public Order(Customer customer) {
this.customer = customer;
}
// printReceipt outputs a simple receipt.
public void printReceipt() {
System.out.println("Customer: " + customer.getName());
}
public static void main(String[] args) {
Customer c = new Customer("Zoe");
Order order = new Order(c);
// The Order object is manipulated after instantiation.
}
}
Explanation: This question tests AP Computer Science A skills in object creation and storage (instantiation). Object instantiation in Java involves using the 'new' keyword followed by a constructor call for the class, which allocates memory and initializes the object. In the provided code snippet, an Order object named 'order' has been created, and the task is to call the printReceipt method on this instance. Choice A is correct because it uses proper dot notation 'order.printReceipt()' to invoke the instance method on the object reference with the required parentheses for a no-parameter method. Choice B is incorrect because it attempts to call printReceipt as a static method on the class rather than on an instance, and Choice C is incorrect because 'receipt' is not a method defined in the Order class. To help students: Emphasize that method calls always require parentheses even when there are no parameters (Choice D omits parentheses). Practice distinguishing between static and instance method invocations.
Given the code below, which line of code correctly creates an object of Car?
public class Car {
private String make;
private String model;
// Constructor stores make and model for later use.
public Car(String make, String model) {
this.make = make;
this.model = model;
}
// repaint changes the car's model label (simulated).
public void repaint(String newModelLabel) {
model = newModelLabel;
}
public static void main(String[] args) {
// A Car reference points to a new object in memory.
// Example method invocation on an instantiated object:
// myCar.repaint("Corolla SE");
}
}
Explanation: This question tests AP Computer Science A skills in object creation and storage (instantiation). Object instantiation in Java involves using the 'new' keyword followed by a constructor call for the class, which allocates memory and initializes the object. In the provided code snippet, the Car class constructor requires two String parameters: make and model. Choice B is correct because it uses proper syntax 'new Car("Honda", "Civic");' with both required parameters and ends with a semicolon as required in Java statements. Choice A is incorrect because it's missing the semicolon at the end of the statement, and Choice C is incorrect because it only provides one parameter when the constructor requires two. To help students: Emphasize the importance of proper Java syntax including semicolons at the end of statements. Remind students that Java is case-sensitive (Choice D uses lowercase 'car' instead of 'Car') and that constructor calls must match the defined parameter list exactly.
Given the code below, which line of code correctly creates an object of Customer?
public class Customer {
private String name;
// Constructor creates a customer with a stored name.
public Customer(String name) {
this.name = name;
}
// getName returns the customer's name.
public String getName() {
return name;
}
}
class Order {
private Customer customer;
// Constructor stores a reference to a Customer object.
public Order(Customer customer) {
this.customer = customer;
}
// printReceipt uses the nested object's method.
public void printReceipt() {
System.out.println("Customer: " + customer.getName());
}
public static void main(String[] args) {
// Objects are instantiated and then used together.
// Example method invocation on an instantiated object:
// order.printReceipt();
}
}
Explanation: This question tests AP Computer Science A skills in object creation and storage (instantiation). Object instantiation in Java involves using the 'new' keyword followed by a constructor call for the class, which allocates memory and initializes the object. In the provided code snippet, the class Customer is instantiated with a constructor that requires one String parameter for the customer's name. Choice A is correct because it follows the correct syntax 'new Customer("Mia")' with the required String parameter. Choice B is incorrect because it attempts to use a no-argument constructor which doesn't exist in the Customer class, and Choice C is incorrect because it's missing the 'new' keyword required for object instantiation. To help students: Emphasize that constructors must be called with the exact parameters they define, and remind them that Java is case-sensitive (Choice D uses lowercase 'customer' instead of 'Customer'). Practice identifying available constructors by examining class definitions.
Consider the following class; which line of code correctly creates an object of Order?
public class Customer {
private String name;
// Constructor creates a customer object.
public Customer(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class Order {
private Customer customer;
// Constructor stores a Customer reference inside the Order.
public Order(Customer customer) {
this.customer = customer;
}
// printReceipt uses the stored Customer object.
public void printReceipt() {
System.out.println("Customer: " + customer.getName());
}
public static void main(String[] args) {
Customer c = new Customer("Liam");
// The Order object is created and then used.
// Example method invocation on an instantiated object:
// order.printReceipt();
}
}
Explanation: This question tests AP Computer Science A skills in object creation and storage (instantiation). Object instantiation in Java involves using the 'new' keyword followed by a constructor call for the class, which allocates memory and initializes the object. In the provided code snippet, the Order class constructor requires a Customer object as its parameter, and a Customer object 'c' has already been created. Choice B is correct because it uses proper syntax 'new Order(c)' passing the existing Customer object reference to the Order constructor. Choice A is incorrect because it attempts to pass a String "Liam" when the constructor expects a Customer object, and Choice C is incorrect because it's missing the essential 'new' keyword. To help students: Emphasize understanding parameter types in constructors and that object references can be passed as parameters. Remind students about Java's case sensitivity (Choice D uses lowercase 'order' instead of 'Order').
Consider the following class; what method would you call to achieve starting the engine?
public class Car {
private String make;
private String model;
private boolean engineOn;
// Constructor initializes a new car with engine off
public Car(String make, String model) {
this.make = make;
this.model = model;
engineOn = false;
}
// Method turns the engine on
public void startEngine() {
engineOn = true;
}
public static void main(String[] args) {
// Object is instantiated, then a method is invoked to change its state
Car myCar = new Car("Honda", "Civic");
// Invoke the method here
}
}
Explanation: This question tests AP Computer Science A skills in object creation and storage (instantiation). Object instantiation in Java involves using the 'new' keyword followed by a constructor call for the class, which allocates memory and initializes the object. In the provided code snippet, a Car object named myCar is already instantiated, and the task is to call the startEngine() method on this object. Choice B is correct because it uses the proper dot notation (myCar.startEngine()) to invoke an instance method on the created object. Choice A is incorrect because it attempts to call startEngine() as a static method on the class rather than on the instance. To help students: Emphasize the difference between static methods (called on the class) and instance methods (called on objects). Practice using dot notation to access object methods and reinforce that instance methods require an object reference.
Given the code below, what method would you call to achieve adding a name to the list?
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
// names stores a dynamic set of student names.
ArrayList<String> names = new ArrayList<String>();
// Example invocation on an instantiated object is needed here.
}
}
```
Explanation: This question tests AP Computer Science A skills in object creation and storage (instantiation). Object instantiation in Java involves using the 'new' keyword followed by a constructor call for the class, which allocates memory and initializes the object. In the provided code snippet, an ArrayList object has been created and stored in the reference variable 'names', and the task is to add a String element to this list. Choice A is correct because it uses the proper syntax 'names.add("Kai")' to call the add method on the instantiated ArrayList object with a String parameter. Choice C is incorrect because ArrayList uses the method name 'add' not 'append' - append is commonly used in other languages like Python but not in Java's ArrayList class. To help students: Emphasize learning the specific method names for Java collections like ArrayList. Practice using ArrayList methods and understanding that different programming languages may use different method names for similar operations.
Consider the following class; which line of code correctly creates an object of Playlist?
public class Playlist {
private String name;
private int songCount;
// Constructor stores the playlist name and starting song count
public Playlist(String name, int songCount) {
this.name = name;
this.songCount = songCount;
}
// Method adds one song to the playlist
public void addSong() {
songCount++;
}
public static void main(String[] args) {
// Playlist object is created, then addSong() updates its stored count
// p.addSong();
}
}
Explanation: This question tests AP Computer Science A skills in object creation and storage (instantiation). Object instantiation in Java involves using the 'new' keyword followed by a constructor call for the class, which allocates memory and initializes the object. In the provided code snippet, the Playlist class constructor requires a String name and an int songCount, as defined by Playlist(String name, int songCount). Choice A is correct because it follows the complete correct syntax: using the 'new' keyword, providing both required parameters, and ending with a semicolon. Choice C is incorrect because it's missing the semicolon at the end of the statement, which is a required syntax element in Java. To help students: Emphasize that every Java statement must end with a semicolon and that missing semicolons are common syntax errors. Practice proofreading code for complete statements including proper punctuation.
Given the code below, what method would you call to achieve changing the car's model label?
public class Car {
private String make;
private String model;
// Constructor creates a Car object stored in a reference.
public Car(String make, String model) {
this.make = make;
this.model = model;
}
// repaint updates the model field.
public void repaint(String newModelLabel) {
model = newModelLabel;
}
public static void main(String[] args) {
Car myCar = new Car("Toyota", "Corolla");
// The object is manipulated after instantiation.
}
}
Explanation: This question tests AP Computer Science A skills in object creation and storage (instantiation). Object instantiation in Java involves using the 'new' keyword followed by a constructor call for the class, which allocates memory and initializes the object. In the provided code snippet, a Car object named 'myCar' has been created, and the task is to call the repaint method which requires a String parameter. Choice A is correct because it uses proper dot notation 'myCar.repaint("Corolla SE")' to invoke the instance method with the required String parameter. Choice B is incorrect because it attempts to call repaint as a static method on the Car class rather than on the instance, and Choice C is incorrect because 'paint' is not a method defined in the Car class. To help students: Emphasize checking method signatures for required parameters (Choice D omits the required String parameter). Practice distinguishing between similar method names and understanding that methods must be called exactly as defined.
Given the code below, which line of code correctly creates an object of BankAccount?
public class BankAccount {
private String owner;
private double balance;
// Constructor allocates a new account object and stores initial state.
public BankAccount(String owner, double initialBalance) {
this.owner = owner;
balance = initialBalance;
}
// withdraw removes money if available.
public void withdraw(double amount) {
balance -= amount;
}
public static void main(String[] args) {
// The object is instantiated and then withdraw can be invoked.
// Example method invocation on an instantiated object:
// acct.withdraw(10.0);
}
}
Explanation: This question tests AP Computer Science A skills in object creation and storage (instantiation). Object instantiation in Java involves using the 'new' keyword followed by a constructor call for the class, which allocates memory and initializes the object. In the provided code snippet, the BankAccount constructor requires a String owner and a double initialBalance. Choice B is correct because it uses proper syntax 'new BankAccount("Eli", 200.0)' with the correct reference type BankAccount and both required parameters in the correct types. Choice A is incorrect because it declares the reference variable as type 'double' instead of 'BankAccount', and Choice C is incorrect because it passes "200.0" as a String when the constructor expects a double. To help students: Emphasize that the reference variable type must match the class being instantiated. Remind students that String literals require quotes while numeric literals do not (Choice D incorrectly omits quotes around the name Eli).
Given the code below, what method would you call to achieve adding money to the account?
public class BankAccount {
private String owner;
private double balance;
// Constructor creates an account object stored in a reference variable.
public BankAccount(String owner, double initialBalance) {
this.owner = owner;
balance = initialBalance;
}
// deposit increases the balance.
public void deposit(double amount) {
balance += amount;
}
public static void main(String[] args) {
BankAccount acct = new BankAccount("Noah", 20.0);
// The object is used after instantiation.
}
}
Explanation: This question tests AP Computer Science A skills in object creation and storage (instantiation). Object instantiation in Java involves using the 'new' keyword followed by a constructor call for the class, which allocates memory and initializes the object. In the provided code snippet, a BankAccount object named 'acct' has already been created, and the task is to call the deposit method on this instance. Choice C is correct because it uses the proper dot notation 'acct.deposit(10.0)' to invoke the instance method on the object reference with the required double parameter. Choice A is incorrect because 'addMoney' is not a method defined in the BankAccount class, and Choice B is incorrect because it attempts to call deposit as a static method on the class rather than on an instance. To help students: Emphasize the difference between static and instance methods, and practice using dot notation to access instance methods. Remind students that method calls must include all required parameters (Choice D omits the amount parameter).
Given the code below, which line of code correctly creates an object of Order?
public class Customer {
private String name;
// Constructor stores the customer's name
public Customer(String name) {
this.name = name;
}
}
public class Order {
private Customer customer;
private int itemCount;
// Constructor stores a customer reference and initial item count
public Order(Customer customer, int itemCount) {
this.customer = customer;
this.itemCount = itemCount;
}
// Method increases the number of items in the order
public void addItems(int amount) {
itemCount += amount;
}
public static void main(String[] args) {
// An Order object is created using an existing Customer, then addItems() updates it
Customer cust = new Customer("Noah");
// Create the Order here
}
}
Explanation: This question tests AP Computer Science A skills in object creation and storage (instantiation). Object instantiation in Java involves using the 'new' keyword followed by a constructor call for the class, which allocates memory and initializes the object. In the provided code snippet, the Order class constructor requires a Customer object and an int, as defined by Order(Customer customer, int itemCount). Choice B is correct because it uses the already-created Customer object 'cust' as the first parameter and provides the integer 2 as the second parameter. Choice A is incorrect because it attempts to pass a String "Noah" instead of a Customer object, which doesn't match the constructor's parameter type. To help students: Emphasize the importance of understanding parameter types and that object references (like 'cust') must be used when a constructor expects an object parameter. Practice distinguishing between primitive types, Strings, and object references in constructor calls.
Consider the following class; how do you create an instance of BankAccount?
public class BankAccount {
private String owner;
private double balance;
// BankAccount represents a simple account that tracks an owner's balance.
public BankAccount(String owner, double startingBalance) {
this.owner = owner;
balance = startingBalance;
}
// deposit increases the balance.
public void deposit(double amount) {
balance += amount;
}
}
class Main {
public static void main(String[] args) {
// The account reference stores the new object, then deposit manipulates its state.
// Example use: acct.deposit(50.0);
}
}
```
Explanation: This question tests AP Computer Science A skills in object creation and storage (instantiation). Object instantiation in Java involves using the 'new' keyword followed by a constructor call for the class, which allocates memory and initializes the object. In the provided code snippet, the class BankAccount is instantiated with a constructor that takes a String parameter for owner and a double parameter for startingBalance. Choice A is correct because it follows the correct syntax 'new BankAccount("Ava", 100.0)' with parameters in the correct order and types as defined in the constructor. Choice C is incorrect because it reverses the parameter order, attempting to pass the double first when the constructor expects the String first. To help students: Emphasize the importance of parameter order in constructor calls and matching parameter types exactly. Encourage students to carefully read constructor definitions and trace through the parameter list when instantiating objects.
Consider the following class; what method would you call to achieve starting the car?
public class Car {
private String make;
private String model;
// Car represents a vehicle identified by make and model.
public Car(String make, String model) {
this.make = make;
this.model = model;
}
// startEngine prints a message indicating the car starts.
public void startEngine() {
System.out.println("Engine started");
}
}
class Main {
public static void main(String[] args) {
// The reference stores the created object, then an instance method is invoked.
Car myCar = new Car("Honda", "Civic");
}
}
```
Explanation: This question tests AP Computer Science A skills in object creation and storage (instantiation). Object instantiation in Java involves using the 'new' keyword followed by a constructor call for the class, which allocates memory and initializes the object. In the provided code snippet, a Car object has been created and stored in the reference variable 'myCar', and the task is to call the startEngine method on this instantiated object. Choice B is correct because it uses the proper syntax 'myCar.startEngine()' to call an instance method on the object referenced by myCar. Choice A is incorrect because it attempts to call startEngine as a static method on the class Car rather than on an instance, which would result in a compilation error since startEngine is an instance method. To help students: Emphasize the distinction between static methods (called on the class) and instance methods (called on objects). Practice using dot notation to access instance methods through object references.
Given the code below, what method would you call to achieve increasing grade level by 1?
public class Student {
private String name;
private int gradeLevel;
// Student represents a student whose grade level can change.
public Student(String name, int gradeLevel) {
this.name = name;
this.gradeLevel = gradeLevel;
}
// promote updates the student's grade level.
public void promote() {
gradeLevel++;
}
}
class Main {
public static void main(String[] args) {
// The reference s stores the created Student, then an instance method modifies it.
Student s = new Student("Ethan", 9);
}
}
```
Explanation: This question tests AP Computer Science A skills in object creation and storage (instantiation). Object instantiation in Java involves using the 'new' keyword followed by a constructor call for the class, which allocates memory and initializes the object. In the provided code snippet, a Student object has been created and stored in the reference variable 's', and the task is to call the promote method which takes no parameters and increases the grade level. Choice B is correct because it uses the proper syntax 's.promote()' to call the parameterless instance method on the object referenced by s. Choice C is incorrect because it attempts to pass a parameter (1) to the promote method when the method signature shows it takes no parameters - the increment is handled internally by the method. To help students: Emphasize reading method signatures carefully to determine if parameters are needed. Practice distinguishing between methods that take parameters versus those that perform operations internally without parameters.