Object Creation and Storage (Instantiation)
Help Questions
AP Computer Science A › Object Creation and Storage (Instantiation)
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.
}
}
myCar.engineStart();
startEngine(myCar);
Car.startEngine();
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, 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);
}
}
```
ArrayList
ArrayList
ArrayList
ArrayList
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
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
}
}
acct.withdraw(25.0);
acct.withDraw(25.0);
BankAccount.withdraw(25.0);
acct.withdraw();
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);
}
}
BankAccount acct = new BankAccount("Ava", 100.0);
BankAccount acct = new bankAccount("Ava", 100.0);
BankAccount acct = BankAccount("Ava", 100.0);
BankAccount acct = new BankAccount("Ava");
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();
}
}
Car myCar = new Car("Toyota");
Car myCar = new Car("Toyota", "Corolla");
Car myCar = Car("Toyota", "Corolla");
Car myCar = new car("Toyota", "Corolla");
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.
}
}
order.printReceipt();
Order.printReceipt();
order.receipt();
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, 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");
}
}
Car myCar = new Car("Honda", "Civic")
Car myCar = new Car("Honda");
Car myCar = new Car("Honda", "Civic");
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, 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();
}
}
Customer c = new Customer();
Customer c = new Customer("Mia");
Customer c = Customer("Mia");
Customer c = new customer("Mia");
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();
}
}
Order order = new Order(c);
Order order = new Order("Liam");
Order order = new order(c);
Order order = Order(c);
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
}
}
myCar.start();
Car.startEngine();
myCar.startEngine();
startEngine(myCar);
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.