Quiz 6 - Inheritance

¡Supera tus tareas y exámenes ahora con Quizwiz!

public class Vehicle { private String manufacturer; . . . public void setVehicleClass(double numberAxles) { . . . } } If a Motorcycle class is created as a subclass of the Vehicle class, which of the following statements is correct? A Motorcycle object inherits but cannot directly use either the instance variable manufacturer or the method setVehicleClass. A Motorcycle object inherits and can directly use both the instance variable manufacturer and the method setVehicleClass. A Motorcycle object inherits and can directly use the method setVehicleClass but cannot directly use the instance variable manufacturer. A Motorcycle object inherits and can directly use the instance variable manufacturer but not the method setVehicleClass.

A Motorcycle object inherits and can directly use the method setVehicleClass but cannot directly use the instance variable manufacturer.

Which of the following is true regarding subclasses? A subclass that inherits methods from its superclass may not override the methods. A subclass may inherit methods and instance variables from its superclass, and may also implement its own methods and declare its own instance variables. A subclass may inherit methods or instance variables from its superclass but not both. A subclass that inherits instance variables from its superclass may not declare additional instance variables.

A subclass may inherit methods and instance variables from its superclass, and may also implement its own methods and declare its own instance variables.

public class Motorcycle extends Vehicle { . . . public Motorcycle(int numberAxles) { super(numberAxles); } } What does this code do? It invokes the constructor of the Motorcycle class from within the constructor of the Vehicle class. It invokes a private method of the Vehicle class from within a method of the Motorcycle class. It invokes the constructor of the Vehicle class from within the constructor of the Motorcycle class. This code will not compile.

It invokes the constructor of the Vehicle class from within the constructor of the Motorcycle class.

public class Vehicle { private String type; public Vehicle(String type) { this.type = type; } public String getType() { return type; } } public class LandVehicle extends Vehicle { public LandVehicle(String type) { . . . } } public class Auto extends LandVehicle { public Auto(String type) { . . . } } Which of the following code fragments is NOT valid in Java? Vehicle myAuto = new Auto("sedan"); Auto myAuto = new Auto("sedan"); LandVehicle myAuto = new Vehicle("sedan"); LandVehicle myAuto = new Auto("sedan");

LandVehicle myAuto = new Vehicle("sedan");

A class that represents a more specific entity in an inheritance hierarchy is called a/an ____. Inheritance class. Superclass Default class Subclass.

Subclass.

A class that represents the most general entity in an inheritance hierarchy is called a/an ____. Inheritance class. Subclass. Default class. Superclass.

Superclass.

public class Employee { . . . public void setDepartment(String deptName) { . . . } } public class Programmer extends Employee { . . . public void setProjectName(String projName) { . . . } public void setDepartment(String deptName) { . . . } } Which of the following statements is NOT correct? The Programmer class can call the setDepartment method of the Employee class. The Programmer class overrides the setDepartment method. The Employee class can call the setProjectName method. The Programmer class can call the setDepartment method of the Programmer class.

The Employee class can call the setProjectName method.

public class Vehicle { . . . public void setVehicleClass(double numberAxles) { . . . } } public class Auto extends Vehicle { . . . public void setVehicleClass(int numberAxles) { . . . } } Which of the following statements is correct? The Auto class overrides the setVehicleClass method. The Vehicle class overrides the setVehicleClass method. The Vehicle class overloads the setVehicleClass method. The Auto class overloads the setVehicleClass method.

The Vehicle class overloads the setVehicleClass method.

public class Vessel { . . . public void setVesselClass(double numberAxles) { . . . } } public class Speedboat extends Vessel { . . . public void setVesselClass(double numberAxles) { . . . } } Which of the following statements is correct? The Speedboat class overrides the setVesselClass method. The Vessel class overloads the setVesselClass method. The Vessel class overrides the setVesselClass method. The Speedboat class overloads the setVesselClass method.

The Vessel class overrides the setVesselClass method.

Employee anEmployee = new Programmer(); anEmployee.increaseSalary(2500); If the Programmer class inherits from the Employee class, and both classes have an implementation of the increaseSalary method with the same set of parameters and the same return type, which statement is correct? It is not possible to determine which class's method is called. The increaseSalary method of the Employee class will be executed. You must specify in the code which class's increaseSalary method is to be used. The increaseSalary method of the Programmer class will be executed.

The increaseSalary method of the Programmer class will be executed.

Vehicle aVehicle = new Auto(); aVehicle.moveForward(200); If the Auto class inherits from the Vehicle class, and both classes have an implementation of the moveForward method with the same set of parameters and the same return type, which statement is correct? It is not possible to determine which class's method is called. The moveForward method of the Vehicle class will be executed. The moveForward method of the Auto class will be executed. You must specify in the code which class's moveForward method is to be used.

The moveForward method of the Auto class will be executed.

public class Vehicle { . . . public void setVehicleAtrributes() { . . . } } public class Auto extends Vehicle { . . . public void setVehicleAtrributes() { . . . } } Which of the following statements is correct? This code will not compile. The subclass is shadowing a superclass method. The subclass is overloading a superclass method. The subclass is overriding a superclass method.

The subclass is overriding a superclass method.

What must a subclass do to modify a private superclass instance variable? The subclass must declare its own instance variable with the same name as the superclass instance variable. The subclass must use a public method of the superclass (if it exists) to update the superclass's private instance variable. The subclass must simply use the name of the superclass instance variable. The subclass must have its own public method to update the superclass's private instance variable.

The subclass must use a public method of the superclass (if it exists) to update the superclass's private instance variable.

public void deposit(double amount) { transactionCount ++; super.deposit(amount); } Which of the following statements is true? This method will call itself. This method calls a private method in its superclass This method calls a public method in its subclass. This method calls a public method in its superclass.

This method calls a public method in its superclass.

Which of the following statements about inheritance is correct? A superclass inherits data and behavior from a subclass. You can always use a subclass object in place of a superclass object. You can always use a superclass object in place of a subclass object. A superclass inherits only behavior from a subclass.

You can always use a subclass object in place of a superclass object.

Vehicle aVehicle = new Auto(); aVehicle.moveForward(200); Assume that the Auto class inherits from the Vehicle class, and both classes have an implementation of the moveForward method with the same set of parameters and the same return type. The process for determining which class's moveForward method to execute is called ____. dynamic inheritance. inheritance disambiguation. dynamic lookup. inheritance hierarchy.

dynamic lookup.

To create a subclass, use the ____ keyword. interface inherits implements extends

extends

If a subclass contains a method with the same name as a method in its superclass, but with different parameter types, the subclass method is said to ____ the method of the superclass. implement. inherit. override. overload.

overload.

Which of the following indicates that a class named Class1 is a subclass of a class named Class2? public class Class1 implements Class2 public class Class2 implements Class1 public class Class2 extends Class1 public class Class1 extends Class2

public class Class1 extends Class2

Which of the following indicates that a class named ClassA class is a superclass of the ClassB class? public class ClassA extends ClassB public class ClassB extends ClassA public class ClassA implements ClassB public class ClassB implements ClassA

public class ClassB extends ClassA

You are creating a Motorcycle class that is supposed to be a subclass of the Vehicle class. Which of the following class declaration statements will accomplish this? public class Vehicle inherits Motorcycle public class Vehicle extends Motorcycle public class Motorcycle inherits Vehicle public class Motorcycle extends Vehicle

public class Motorcycle extends Vehicle

You are creating a Motorcycle class which is supposed to inherit from the Vehicle class. Which of the following class declaration statements will accomplish this? public class Motorcycle extends Vehicle public class Motorcycle implements Vehicle public class Motorcycle inherits Vehicle public class Motorcycle interfaces Vehicle

public class Motorcycle extends Vehicle

public class Vehicle { . . . public void setVehicleClass(double numberAxles) { . . . } } public class Motorcycle extends Vehicle { . . . public Motorcycle() { _______________; } } this.setVehicleClass(2.0); Motorcyle.setVehicleClass(2.0); setVehicleClass(2.0); Vehicle.setVehicleClass(2.0);

setVehicleClass(2.0);

Which reserved word must be used to call a method of a superclass? super this my parent

super

public class Auto extends LandVehicle { public Auto(String type) { _________; } } Complete the code in the Auto class constructor to store the type data. super(super(type)); super.super(type); This cannot be done unless the Auto declares an instance variable named type. super(type);

super(type);

Employee anEmployee = new Programmer(); anEmployee.increaseSalary(2500); Assume that the Programmer class inherits from the Employee class, and both classes have an implementation of the increaseSalary method with the same set of parameters and the same return type. Which class's increaseSalary method is to be executed is determined by ____. the hierarchy of the classes. the variable's type. the actual object type. it is not possible to determine which method is executed.

the actual object type.

Vehicle aVehicle = new Auto(); aVehicle.moveForward(200); Assume that the Auto class inherits from the Vehicle class, and both classes have an implementation of the moveForward method with the same set of parameters and the same return type. Which class's moveForward method is to be executed is determined by ____. the variable's type. the hierarchy of the classes. the actual object type. it is not possible to determine which method is executed.

the actual object type.

If a subclass uses the same method name but different parameter types for a method that appears in its superclass, ____. a compiler error will occur. the subclass has overridden its superclass's method. the subclass has implemented its superclass's method. the subclass has overloaded its superclass's method.

the subclass has overloaded its superclass's method.

If a subclass defines the same method name and the same parameter types for a method that appears in its superclass, ____. the subclass has implemented its superclass's method. the subclass has overloaded its superclass's method. the subclass has overridden its superclass's method. a compiler error will occur.

the subclass has overridden its superclass's method.


Conjuntos de estudio relacionados

NCLEX-RN - MED-SURG: GASTROINTESTINAL

View Set