Object Creation and Storage (Instantiation)
Help Questions
AP Computer Science A › Object Creation and Storage (Instantiation)
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("Mia");
Customer c = new Customer("Mia");
Customer c = new Customer();
Customer c = 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.
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.
}
}
Car.repaint("Corolla SE");
myCar.repaint("Corolla SE");
myCar.paint("Corolla SE");
myCar.repaint();
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);
}
}
BankAccount acct = new BankAccount("Eli", "200.0");
BankAccount acct = new BankAccount("Eli", 200.0);
BankAccount acct = new BankAccount(Eli, 200.0);
double acct = new BankAccount("Eli", 200.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).
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();
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' 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 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");
Car myCar = new car("Honda", "Civic");
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, 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.printReceipt;
order.receipt();
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.
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("Liam");
Order order = new Order(c);
Order order = Order(c);
Order order = new 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').
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.
}
}
acct.deposit(10.0);
acct.deposit();
acct.addMoney(10.0);
BankAccount.deposit(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, 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).
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 = BankAccount("Ava", 100.0);
BankAccount acct = new BankAccount("Ava");
BankAccount acct = new bankAccount("Ava", 100.0);
BankAccount acct = new BankAccount("Ava", 100.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();
}
}
Car myCar = new car("Toyota", "Corolla");
Car myCar = new Car("Toyota", "Corolla");
Car myCar = Car("Toyota", "Corolla");
Car myCar = new Car("Toyota");
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').