0%
0 / 15 answered
Object Creation and Storage (Instantiation) Practice Test
•15 QuestionsQuestion
1 / 15
Q1
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();
}
}
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();
}
}