QUESTIONS WITH DETAILED SOLUTIONS
◉You are creating a class named Vessel. Which statement correctly
declares a constructor for this class?
a) public Vessel()
b) public void Vessel()
c) void Vessel()
d) new Vessel(). Answer: a
◉You are creating a class named Employee. Which statement
correctly declares a constructor for this class?
a) public Employee()
b) public void Employee()
c) void Employee()
d) new Employee(). Answer: a
◉Consider the following code snippet:
public class Vehicle{ private String type;
,private int numAxles;
public Vehicle(String vehicleType, int VehicleAxles)
{...}
}
Which of the following statements can be used to create an object of
type Vehicle?
a) Vehicle anAuto = new Vehicle();
b) Vehicle anAuto = new Vehicle(2, "SUV");
c) Vehicle anAuto = new Vehicle("SUV", "2");
d) Vehicle anAuto = new Vehicle("SUV", 2);. Answer: d
◉Consider the following code snippet:
public class Employee{ private String empID;
private boolean hourly;
public Employee(String employeeID, boolean isHourly) { . . . }
}
Which of the following statements can be used to create an object of
type Employee?
, a) Employee anAuto = new Employee();
b) Employee anAuto = new Employee(10548, true);
c) Employee anAuto = new Employee("10548", true);
d) Employee anAuto = new Employee(10548, "true");. Answer: c
◉Complete the following code snippet to create a constructor that
will initialize the class instance variables shown using data provided
by the program which is creating an object from this class.
public class Employee
{
private String empID;
private boolean hourly; _______________________
{...}
}
Which of the following statements can be used to create an object of
type Employee?