Deck 9: Inheritance and Interfaces
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/99
Play
Full screen (f)
Deck 9: Inheritance and Interfaces
1
To create a subclass, use the ____ keyword.
A) inherits
B) implements
C) interface
D) extends
A) inherits
B) implements
C) interface
D) extends
D
2
Which of the following is true regarding subclasses?
A) A subclass has access to private instance variables of its superclass.
B) A subclass does not have access to public instance variables of its superclass.
C) A subclass must specify the implicit parameter to use methods inherited from its superclass.
D) A subclass has no access to private instance variables of its superclass.
A) A subclass has access to private instance variables of its superclass.
B) A subclass does not have access to public instance variables of its superclass.
C) A subclass must specify the implicit parameter to use methods inherited from its superclass.
D) A subclass has no access to private instance variables of its superclass.
D
3
You are creating a Motorcycle class which is supposed to be a subclass of the Vehicle class. Which of the following class declaration statements will accomplish this?
A) public class Motorcycle extends Vehicle
B) public class Motorcycle implements Vehicle
C) public class Motorcycle interfaces Vehicle
D) public class Motorcycle inherits Vehicle
A) public class Motorcycle extends Vehicle
B) public class Motorcycle implements Vehicle
C) public class Motorcycle interfaces Vehicle
D) public class Motorcycle inherits Vehicle
A
4
Which of the following statements about inheritance is correct?
A) You can always use a superclass object in place of a subclass object.
B) You can always use a subclass object in place of a superclass object.
C) A superclass inherits data and behavior from a subclass.
D) A superclass inherits only behavior from a subclass.
A) You can always use a superclass object in place of a subclass object.
B) You can always use a subclass object in place of a superclass object.
C) A superclass inherits data and behavior from a subclass.
D) A superclass inherits only behavior from a subclass.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
5
Which of the following indicates that a class named ClassA class is a superclass of the ClassB class?
A) public class ClassB extends ClassA
B) public class ClassB implements ClassA
C) public class ClassA extends ClassB
D) public class ClassA implements ClassB
A) public class ClassB extends ClassA
B) public class ClassB implements ClassA
C) public class ClassA extends ClassB
D) public class ClassA implements ClassB
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
6
A class that represents the most general entity in an inheritance hierarchy is called a/an ____.
A) Default class.
B) Superclass.
C) Subclass.
D) Inheritance class.
A) Default class.
B) Superclass.
C) Subclass.
D) Inheritance class.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
7
What must a subclass do to modify a private superclass instance variable?
A) The subclass must simply use the name of the superclass instance variable.
B) The subclass must declare its own instance variable with the same name as the superclass instance variable.
C) The subclass must use a public method of the superclass (if it exists) to update the superclass's private instance variable.
D) The subclass must have its own public method to update the superclass's private instance variable.
A) The subclass must simply use the name of the superclass instance variable.
B) The subclass must declare its own instance variable with the same name as the superclass instance variable.
C) The subclass must use a public method of the superclass (if it exists) to update the superclass's private instance variable.
D) The subclass must have its own public method to update the superclass's private instance variable.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
8
Insert the missing code in the following code fragment. This fragment is intended to call the Vehicle class's method.
Public class Vehicle
{
) . .
Public void setVehicleClass(double numberAxles)
{
) . .
}
}
Public class Motorcycle extends Vehicle
{
) . .
Public Motorcycle()
{
_______________;
}
}
A) Motorcyle.setVehicleClass(2.0);
B) Vehicle.setVehicleClass(2.0);
C) this.setVehicleClass(2.0);
D) setVehicleClass(2.0);
Public class Vehicle
{
) . .
Public void setVehicleClass(double numberAxles)
{
) . .
}
}
Public class Motorcycle extends Vehicle
{
) . .
Public Motorcycle()
{
_______________;
}
}
A) Motorcyle.setVehicleClass(2.0);
B) Vehicle.setVehicleClass(2.0);
C) this.setVehicleClass(2.0);
D) setVehicleClass(2.0);
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
9
A class that represents a more specific entity in an inheritance hierarchy is called a/an ____.
A) Default class
B) Superclass
C) Subclass.
D) Inheritance class.
A) Default class
B) Superclass
C) Subclass.
D) Inheritance class.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
10
Consider the following inheritance hierarchy diagram:
Which of the following statements is correct?
A) Auto is a superclass of LandVehicle, and LandVehicle is a superclass of Vehicle.
B) Auto is a superclass of LandVehicle, and LandVehicle is a subclass of Vehicle.
C) Auto is a subclass of LandVehicle, and LandVehicle is a superclass of Vehicle.
D) Auto is a subclass of LandVehicle, and LandVehicle is a subclass of Vehicle.

A) Auto is a superclass of LandVehicle, and LandVehicle is a superclass of Vehicle.
B) Auto is a superclass of LandVehicle, and LandVehicle is a subclass of Vehicle.
C) Auto is a subclass of LandVehicle, and LandVehicle is a superclass of Vehicle.
D) Auto is a subclass of LandVehicle, and LandVehicle is a subclass of Vehicle.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
11
Which of the following indicates that the Motorcycle class is a subclass of the Vehicle class?
A) public class Motorcycle extends Vehicle
B) public class Motorcycle implements Vehicle
C) public class Vehicle extends Motorcycle
D) public class Vehicle implements Motorcycle
A) public class Motorcycle extends Vehicle
B) public class Motorcycle implements Vehicle
C) public class Vehicle extends Motorcycle
D) public class Vehicle implements Motorcycle
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
12
Consider the following inheritance hierarchy diagram:
Which of the following statements is correct?
A) Auto class inherits from LandVehicle class, and LandVehicle class inherits from Vehicle class.
B) Auto class inherits from LandVehicle class, and Vehicle class inherits from LandVehicle class.
C) LandVehicle class inherits from Auto class, and LandVehicle class inherits from Vehicle class.
D) LandVehicle class inherits from Auto class, and Vehicle class inherits from LandVehicle class.

A) Auto class inherits from LandVehicle class, and LandVehicle class inherits from Vehicle class.
B) Auto class inherits from LandVehicle class, and Vehicle class inherits from LandVehicle class.
C) LandVehicle class inherits from Auto class, and LandVehicle class inherits from Vehicle class.
D) LandVehicle class inherits from Auto class, and Vehicle class inherits from LandVehicle class.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
13
Which of the following is true regarding subclasses?
A) A subclass inherits methods from its superclass but not instance variables.
B) A subclass inherits instance variables from its superclass but not methods.
C) A subclass inherits methods and instance variables from its superclass.
D) A subclass does not inherit methods or instance variables from its superclass.
A) A subclass inherits methods from its superclass but not instance variables.
B) A subclass inherits instance variables from its superclass but not methods.
C) A subclass inherits methods and instance variables from its superclass.
D) A subclass does not inherit methods or instance variables from its superclass.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
14
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?
A) public class Vehicle extends Motorcycle
B) public class Motorcycle extends Vehicle
C) public class Vehicle inherits Motorcycle
D) public class Motorcycle inherits Vehicle
A) public class Vehicle extends Motorcycle
B) public class Motorcycle extends Vehicle
C) public class Vehicle inherits Motorcycle
D) public class Motorcycle inherits Vehicle
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
15
Which of the following indicates that a class named Class1 is a subclass of a class named Class2?
A) public class Class1 extends Class2
B) public class Class1 implements Class2
C) public class Class2 extends Class1
D) public class Class2 implements Class1
A) public class Class1 extends Class2
B) public class Class1 implements Class2
C) public class Class2 extends Class1
D) public class Class2 implements Class1
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
16
Consider the following code snippet:
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) A Motorcycle object inherits and can directly use both the instance variable manufacturer and the method setVehicleClass.
B) A Motorcycle object inherits and can directly use the instance variable manufacturer but not the method setVehicleClass.
C) A Motorcycle object inherits but cannot directly use either the instance variable manufacturer or the method setVehicleClass.
D) A Motorcycle object inherits and can directly use the method setVehicleClass but cannot directly use the instance variable manufacturer.
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) A Motorcycle object inherits and can directly use both the instance variable manufacturer and the method setVehicleClass.
B) A Motorcycle object inherits and can directly use the instance variable manufacturer but not the method setVehicleClass.
C) A Motorcycle object inherits but cannot directly use either the instance variable manufacturer or the method setVehicleClass.
D) A Motorcycle object inherits and can directly use the method setVehicleClass but cannot directly use the instance variable manufacturer.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
17
You are creating a class inheritance hierarchy about motor vehicles that will contain classes named Vehicle, Auto, and Motorcycle. Which of the following statements is correct?
A) Vehicle should be the default class, while Auto and Motorcycle should be the subclasses.
B) Vehicle should be the superclass, while Auto and Motorcycle should be the subclasses.
C) Vehicle should be the subclass, while Auto and Motorcycle should be the superclasses.
D) Vehicle should be the subclass, while Auto and Motorcycle should be the default classes.
A) Vehicle should be the default class, while Auto and Motorcycle should be the subclasses.
B) Vehicle should be the superclass, while Auto and Motorcycle should be the subclasses.
C) Vehicle should be the subclass, while Auto and Motorcycle should be the superclasses.
D) Vehicle should be the subclass, while Auto and Motorcycle should be the default classes.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
18
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?
A) public class Motorcycle inherits Vehicle
B) public class Motorcycle implements Vehicle
C) public class Motorcycle interfaces Vehicle
D) public class Motorcycle extends Vehicle
A) public class Motorcycle inherits Vehicle
B) public class Motorcycle implements Vehicle
C) public class Motorcycle interfaces Vehicle
D) public class Motorcycle extends Vehicle
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
19
Which of the following is true regarding subclasses?
A) A subclass that inherits methods from its superclass may not override the methods.
B) A subclass that inherits instance variables from its superclass may not declare additional instance variables.
C) A subclass may inherit methods or instance variables from its superclass but not both.
D) 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) A subclass that inherits methods from its superclass may not override the methods.
B) A subclass that inherits instance variables from its superclass may not declare additional instance variables.
C) A subclass may inherit methods or instance variables from its superclass but not both.
D) A subclass may inherit methods and instance variables from its superclass, and may also implement its own methods and declare its own instance variables.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
20
Insert the missing code in the following code fragment. This fragment is intended to call the Vessel class's method.
Public class Vessel
{
) . .
Public void set VesselClass(double vesselLength)
{
) . .
}
}
Public class SpeedBoat extends Vessel
{
) . .
Public SpeedBoat()
{
_______________;
}
}
A) SpeedBoat.vesselLength(26.0);
B) Vessel.vesselLength(26.0);
C) this.vesselLength(26.0);
D) vesselLength(26.0);
Public class Vessel
{
) . .
Public void set VesselClass(double vesselLength)
{
) . .
}
}
Public class SpeedBoat extends Vessel
{
) . .
Public SpeedBoat()
{
_______________;
}
}
A) SpeedBoat.vesselLength(26.0);
B) Vessel.vesselLength(26.0);
C) this.vesselLength(26.0);
D) vesselLength(26.0);
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
21
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.
A) implement.
B) inherit.
C) override.
D) overload.
A) implement.
B) inherit.
C) override.
D) overload.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
22
Consider the following code snippet:
Public class Vehicle
{
) . .
Public void setVehicleAtrributes()
{
) . .
}
}
Public class Auto extends Vehicle
{
) . .
Public void setVehicleAtrributes()
{
) . .
}
}
Which of the following statements is correct?
A) The subclass is shadowing a superclass method.
B) The subclass is overloading a superclass method.
C) The subclass is overriding a superclass method.
D) This code will not compile.
Public class Vehicle
{
) . .
Public void setVehicleAtrributes()
{
) . .
}
}
Public class Auto extends Vehicle
{
) . .
Public void setVehicleAtrributes()
{
) . .
}
}
Which of the following statements is correct?
A) The subclass is shadowing a superclass method.
B) The subclass is overloading a superclass method.
C) The subclass is overriding a superclass method.
D) This code will not compile.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
23
Consider the following code snippet:
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?
A) The Speedboat class overrides the setVesselClass method.
B) The Vessel class overrides the setVesselClass method.
C) The Speedboat class overloads the setVesselClass method.
D) The Vessel class overloads the setVesselClass 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?
A) The Speedboat class overrides the setVesselClass method.
B) The Vessel class overrides the setVesselClass method.
C) The Speedboat class overloads the setVesselClass method.
D) The Vessel class overloads the setVesselClass method.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
24
Consider the following class hierarchy:
Public class Vehicle
{
Private String type;
Public Vehicle(String type)
{
This.type = type;
}
Public String displayInfo()
{
Return type;
}
}
Public class LandVehicle extends Vehicle
{
Public LandVehicle(String type)
{
Super(type);
}
}
Public class Auto extends LandVehicle
{
Public Auto(String type)
{
Super(type);
}
}
You have written a program to use these classes, as shown in the following code snippet:
Public class VehicleTester
{
Public static void main(String[] args)
{
Auto myAuto = new Auto("sedan");
System.out.println("MyAuto type = " + ______);
}
}
Complete the code in this program snippet to correctly display the auto's type.
A) myAuto.displayInfo()
B) myAuto.super.displayInfo()
C) myAuto.super.super.displayInfo()
D) This cannot be done unless the Auto class overrides the displayInfo method.
Public class Vehicle
{
Private String type;
Public Vehicle(String type)
{
This.type = type;
}
Public String displayInfo()
{
Return type;
}
}
Public class LandVehicle extends Vehicle
{
Public LandVehicle(String type)
{
Super(type);
}
}
Public class Auto extends LandVehicle
{
Public Auto(String type)
{
Super(type);
}
}
You have written a program to use these classes, as shown in the following code snippet:
Public class VehicleTester
{
Public static void main(String[] args)
{
Auto myAuto = new Auto("sedan");
System.out.println("MyAuto type = " + ______);
}
}
Complete the code in this program snippet to correctly display the auto's type.
A) myAuto.displayInfo()
B) myAuto.super.displayInfo()
C) myAuto.super.super.displayInfo()
D) This cannot be done unless the Auto class overrides the displayInfo method.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
25
Consider the following code snippet:
Public class Vessel
{
) . .
Public void setVesselAtrributes()
{
) . .
}
}
Public class Speedboat extends Vessel
{
) . .
Public void setVesselAtrributes()
{
) . .
}
}
Which of the following statements is correct?
A) The subclass is shadowing a superclass method.
B) The subclass is overloading a superclass method.
C) The subclass is overriding a superclass method.
D) This code will not compile.
Public class Vessel
{
) . .
Public void setVesselAtrributes()
{
) . .
}
}
Public class Speedboat extends Vessel
{
) . .
Public void setVesselAtrributes()
{
) . .
}
}
Which of the following statements is correct?
A) The subclass is shadowing a superclass method.
B) The subclass is overloading a superclass method.
C) The subclass is overriding a superclass method.
D) This code will not compile.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
26
Consider the following code snippet that appears in a subclass:
Public void deposit(double amount)
{
TransactionCount ++;
Deposit(amount);
}
Which of the following statements is true?
A) This method will call itself.
B) This method calls a public method in its subclass.
C) This method calls a private method in its superclass
D) This method calls a public method in its superclass.
Public void deposit(double amount)
{
TransactionCount ++;
Deposit(amount);
}
Which of the following statements is true?
A) This method will call itself.
B) This method calls a public method in its subclass.
C) This method calls a private method in its superclass
D) This method calls a public method in its superclass.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
27
Consider the following class hierarchy:
Public class Vehicle
{
Private String type;
Public Vehicle(String type)
{
This.type = type;
}
Public String displayInfo()
{
Return type;
}
}
Public class LandVehicle extends Vehicle
{
Public LandVehicle(String type)
{
Super(type);
}
}
Public class Auto extends LandVehicle
{
Public Auto(String type)
{
_________;
}
}
Complete the code in the Auto class constructor to store the type data.
A) super(type);
B) super(super(type));
C) super.super(type);
D) This cannot be done unless the Auto declares an instance variable named type.
Public class Vehicle
{
Private String type;
Public Vehicle(String type)
{
This.type = type;
}
Public String displayInfo()
{
Return type;
}
}
Public class LandVehicle extends Vehicle
{
Public LandVehicle(String type)
{
Super(type);
}
}
Public class Auto extends LandVehicle
{
Public Auto(String type)
{
_________;
}
}
Complete the code in the Auto class constructor to store the type data.
A) super(type);
B) super(super(type));
C) super.super(type);
D) This cannot be done unless the Auto declares an instance variable named type.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
28
If a subclass uses the same method name but different parameter types for a method that appears in its superclass, ____.
A) the subclass has overloaded its superclass's method.
B) the subclass has overridden its superclass's method.
C) the subclass has implemented its superclass's method.
D) a compiler error will occur.
A) the subclass has overloaded its superclass's method.
B) the subclass has overridden its superclass's method.
C) the subclass has implemented its superclass's method.
D) a compiler error will occur.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
29
Consider the following code snippet:
Public class Vehicle
{
) . .
Public void setVehicleClass(double numberAxles)
{
) . .
}
}
Public class Motorcycle extends Vehicle
{
) . .
Public void setModelName(String model)
{
) . .
}
Public void setVehicleClass(double numberAxles)
{
) . .
}
}
Which of the following statements is NOT correct?
A) The Motorcycle class can call the setVehicleClass method of the Vehicle class.
B) The Vehicle class can call the setModelName method.
C) The Motorcycle class overrides the setVehicleClass method.
D) The Motorcycle class can call the setVehicleClass method of the Motorcycle class.
Public class Vehicle
{
) . .
Public void setVehicleClass(double numberAxles)
{
) . .
}
}
Public class Motorcycle extends Vehicle
{
) . .
Public void setModelName(String model)
{
) . .
}
Public void setVehicleClass(double numberAxles)
{
) . .
}
}
Which of the following statements is NOT correct?
A) The Motorcycle class can call the setVehicleClass method of the Vehicle class.
B) The Vehicle class can call the setModelName method.
C) The Motorcycle class overrides the setVehicleClass method.
D) The Motorcycle class can call the setVehicleClass method of the Motorcycle class.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
30
Consider the following code snippet:
Public class Employee
{
) . .
Public void setEmployeeDept(String deptNum)
{
) . .
}
}
Public class Programmer extends Employee
{
) . .
Public void setEmployeeDept(int deptNum)
{
) . .
}
}
Which of the following statements is correct?
A) The Programmer class overrides the setEmployeeDept method.
B) The Employee class overrides the setEmployeeDept method.
C) The Programmer class overloads the setEmployeeDept method.
D) The Employee class overloads the setEmployeeDept method.
Public class Employee
{
) . .
Public void setEmployeeDept(String deptNum)
{
) . .
}
}
Public class Programmer extends Employee
{
) . .
Public void setEmployeeDept(int deptNum)
{
) . .
}
}
Which of the following statements is correct?
A) The Programmer class overrides the setEmployeeDept method.
B) The Employee class overrides the setEmployeeDept method.
C) The Programmer class overloads the setEmployeeDept method.
D) The Employee class overloads the setEmployeeDept method.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
31
Which of the following statements about superclasses and subclasses is true?
A) A superclass is larger than its subclass.
B) A superclass inherits from a subclass.
C) A superclass extends a subclass.
D) A subclass extends a superclass.
A) A superclass is larger than its subclass.
B) A superclass inherits from a subclass.
C) A superclass extends a subclass.
D) A subclass extends a superclass.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
32
Consider the following code snippet:
Public class Vehicle
{
) . .
Public void setVehicleClass(double numberAxles)
{
) . .
}
}
Public class Motorcycle extends Vehicle
{
) . .
Public void setVehicleClass(double numberAxles)
{
) . .
}
}
Which of the following statements is correct?
A) The Motorcycle class overrides the setVehicleClass method.
B) The Vehicle class overrides the setVehicleClass method.
C) The Motorcycle class overloads the setVehicleClass method.
D) The Vehicle class overloads the setVehicleClass method.
Public class Vehicle
{
) . .
Public void setVehicleClass(double numberAxles)
{
) . .
}
}
Public class Motorcycle extends Vehicle
{
) . .
Public void setVehicleClass(double numberAxles)
{
) . .
}
}
Which of the following statements is correct?
A) The Motorcycle class overrides the setVehicleClass method.
B) The Vehicle class overrides the setVehicleClass method.
C) The Motorcycle class overloads the setVehicleClass method.
D) The Vehicle class overloads the setVehicleClass method.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
33
To override a superclass method in a subclass, the subclass method ____.
A) Must use a different method name.
B) Must use the same method name and the same parameter types.
C) Must use a different method name and the same parameter types.
D) Must use a different method name and different parameter types.
A) Must use a different method name.
B) Must use the same method name and the same parameter types.
C) Must use a different method name and the same parameter types.
D) Must use a different method name and different parameter types.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
34
Which reserved word must be used to call a method of a superclass?
A) this
B) my
C) parent
D) super
A) this
B) my
C) parent
D) super
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
35
Consider the following code snippet:
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?
A) The Auto class overrides the setVehicleClass method.
B) The Vehicle class overrides the setVehicleClass method.
C) The Auto class overloads the setVehicleClass method.
D) The Vehicle class overloads the setVehicleClass 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?
A) The Auto class overrides the setVehicleClass method.
B) The Vehicle class overrides the setVehicleClass method.
C) The Auto class overloads the setVehicleClass method.
D) The Vehicle class overloads the setVehicleClass method.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
36
If a subclass defines the same method name and the same parameter types for a method that appears in its superclass, ____.
A) the subclass has overloaded its superclass's method.
B) the subclass has overridden its superclass's method.
C) the subclass has implemented its superclass's method.
D) a compiler error will occur.
A) the subclass has overloaded its superclass's method.
B) the subclass has overridden its superclass's method.
C) the subclass has implemented its superclass's method.
D) a compiler error will occur.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
37
Consider the following code snippet:
Public class Vessel
{
Private String manufacturer;
) . .
Public void setVesselClass(double engineRPM)
{
) . .
}
}
If a Speedboat class is created as a subclass of the Vessel class, which of the following statements is correct?
A) A Speedboat object inherits and can directly use both the instance variable manufacturer and the method setVesselClass.
B) A Speedboat object inherits and can directly use the instance variable manufacturer but not the method setVesselClass.
C) A Speedboat object inherits but cannot directly use either the instance variable manufacturer or the method setVesselClass.
D) A Speedboat object inherits and can directly use the method setVesselClass but cannot directly use the instance variable manufacturer.
Public class Vessel
{
Private String manufacturer;
) . .
Public void setVesselClass(double engineRPM)
{
) . .
}
}
If a Speedboat class is created as a subclass of the Vessel class, which of the following statements is correct?
A) A Speedboat object inherits and can directly use both the instance variable manufacturer and the method setVesselClass.
B) A Speedboat object inherits and can directly use the instance variable manufacturer but not the method setVesselClass.
C) A Speedboat object inherits but cannot directly use either the instance variable manufacturer or the method setVesselClass.
D) A Speedboat object inherits and can directly use the method setVesselClass but cannot directly use the instance variable manufacturer.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
38
Consider the following class hierarchy:
Public class Vehicle
{
Private String type;
Public Vehicle(String type)
{
This.type = type;
}
Public String displayInfo()
{
Return type;
}
}
Public class LandVehicle extends Vehicle
{
Public LandVehicle(String type)
{
) . .
}
}
Public class Auto extends LandVehicle
{
Public Auto(String type)
{
) . .
}
Public String displayAutoType()
{
Return _____;
}
}
Complete the code in the Auto class method named displayAutoType to return the type data.
A) super(type);
B) super.type;
C) super.super.type;
D) This cannot be done unless the Auto class uses a public method in a superclass that returns the type data.
Public class Vehicle
{
Private String type;
Public Vehicle(String type)
{
This.type = type;
}
Public String displayInfo()
{
Return type;
}
}
Public class LandVehicle extends Vehicle
{
Public LandVehicle(String type)
{
) . .
}
}
Public class Auto extends LandVehicle
{
Public Auto(String type)
{
) . .
}
Public String displayAutoType()
{
Return _____;
}
}
Complete the code in the Auto class method named displayAutoType to return the type data.
A) super(type);
B) super.type;
C) super.super.type;
D) This cannot be done unless the Auto class uses a public method in a superclass that returns the type data.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
39
Consider the following code snippet:
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?
A) The Programmer class can call the setDepartment method of the Employee class.
B) The Employee class can call the setProjectName method.
C) The Programmer class overrides the setDepartment method.
D) The Programmer class can call the setDepartment method of the Programmer class.
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?
A) The Programmer class can call the setDepartment method of the Employee class.
B) The Employee class can call the setProjectName method.
C) The Programmer class overrides the setDepartment method.
D) The Programmer class can call the setDepartment method of the Programmer class.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
40
Consider the following code snippet:
Public void deposit(double amount)
{
TransactionCount ++;
Super.deposit(amount);
}
Which of the following statements is true?
A) This method will call itself.
B) This method calls a public method in its subclass.
C) This method calls a private method in its superclass
D) This method calls a public method in its superclass.
Public void deposit(double amount)
{
TransactionCount ++;
Super.deposit(amount);
}
Which of the following statements is true?
A) This method will call itself.
B) This method calls a public method in its subclass.
C) This method calls a private method in its superclass
D) This method calls a public method in its superclass.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
41
Consider the following class hierarchy:
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?
A) Vehicle myAuto = new Auto("sedan");
B) LandVehicle myAuto = new Auto("sedan");
C) Auto myAuto = new Auto("sedan");
D) LandVehicle myAuto = new Vehicle("sedan");
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?
A) Vehicle myAuto = new Auto("sedan");
B) LandVehicle myAuto = new Auto("sedan");
C) Auto myAuto = new Auto("sedan");
D) LandVehicle myAuto = new Vehicle("sedan");
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
42
Consider the following code snippet:
Public abstract class Employee
{
Public abstract void setSalary();
) . .
}
You wish to create a subclass named Programmer. Which of the following is the correct way to declare this subclass?
A) public class Programmer implements Employee
{
Public void setSalary() { . . . }
}
B) public class Programmer extends Employee
{
Void setSalary() { . . . }
}
C) public class Programmer implements Employee
{
Void setSalary() { . . . }
}
D) public class Programmer extends Employee
{
Public void setSalary() { . . . }
}
Public abstract class Employee
{
Public abstract void setSalary();
) . .
}
You wish to create a subclass named Programmer. Which of the following is the correct way to declare this subclass?
A) public class Programmer implements Employee
{
Public void setSalary() { . . . }
}
B) public class Programmer extends Employee
{
Void setSalary() { . . . }
}
C) public class Programmer implements Employee
{
Void setSalary() { . . . }
}
D) public class Programmer extends Employee
{
Public void setSalary() { . . . }
}
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
43
Consider the following code snippet:
Public class Motorcycle extends Vehicle
{
Private String model;
) . .
Public Motorcycle(int numberAxles, String modelName)
{
Model = modelName;
Super(numberAxles);
}
}
What does this code do?
A) It invokes the constructor of the Vehicle class from within the constructor of the Motorcycle class.
B) It invokes the constructor of the Motorcycle class from within the constructor of the Vehicle class.
C) It invokes a private method of the Vehicle class from within a method of the Motorcycle class.
D) This code will not compile.
Public class Motorcycle extends Vehicle
{
Private String model;
) . .
Public Motorcycle(int numberAxles, String modelName)
{
Model = modelName;
Super(numberAxles);
}
}
What does this code do?
A) It invokes the constructor of the Vehicle class from within the constructor of the Motorcycle class.
B) It invokes the constructor of the Motorcycle class from within the constructor of the Vehicle class.
C) It invokes a private method of the Vehicle class from within a method of the Motorcycle class.
D) This code will not compile.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
44
Consider the following code snippet:
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?
A) The moveForward method of the Auto class will be executed.
B) The moveForward method of the Vehicle class will be executed.
C) You must specify in the code which class's moveForward method is to be used.
D) It is not possible to determine which class's method is called.
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?
A) The moveForward method of the Auto class will be executed.
B) The moveForward method of the Vehicle class will be executed.
C) You must specify in the code which class's moveForward method is to be used.
D) It is not possible to determine which class's method is called.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
45
Consider the following code snippet:
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?
A) The increaseSalary method of the Programmer class will be executed.
B) The increaseSalary method of the Employee class will be executed.
C) You must specify in the code which class's increaseSalary method is to be used.
D) It is not possible to determine which class's method is called.
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?
A) The increaseSalary method of the Programmer class will be executed.
B) The increaseSalary method of the Employee class will be executed.
C) You must specify in the code which class's increaseSalary method is to be used.
D) It is not possible to determine which class's method is called.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
46
Consider the following code snippet:
Public class Motorcycle extends Vehicle
{
Private String model;
) . .
Public Motorcycle(int numberAxles, String modelName)
{
Super(numberAxles);
Model = modelName;
}
}
What does this code do?
A) It invokes the constructor of the Vehicle class from within the constructor of the Motorcycle class.
B) It invokes the constructor of the Motorcycle class from within the constructor of the Vehicle class.
C) It invokes a private method of the Vehicle class from within a method of the Motorcycle class.
D) This code will not compile.
Public class Motorcycle extends Vehicle
{
Private String model;
) . .
Public Motorcycle(int numberAxles, String modelName)
{
Super(numberAxles);
Model = modelName;
}
}
What does this code do?
A) It invokes the constructor of the Vehicle class from within the constructor of the Motorcycle class.
B) It invokes the constructor of the Motorcycle class from within the constructor of the Vehicle class.
C) It invokes a private method of the Vehicle class from within a method of the Motorcycle class.
D) This code will not compile.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
47
A class that cannot be instantiated is called a/an ____.
A) Abstract class.
B) Anonymous class.
C) Concrete class.
D) Non-inheritable.
A) Abstract class.
B) Anonymous class.
C) Concrete class.
D) Non-inheritable.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
48
Consider the following code snippet:
Public abstract class Machine
{
Public abstract void setRPMs();
) . .
}
You wish to create a subclass named PolisherMachine. Which of the following is the correct way to declare this subclass?
A) public class PolisherMachine implements Machine
{
Public void setRPMs() { . . . }
}
B) public class PolisherMachine extends Machine
{
Void setRPMs() { . . . }
}
C) public class PolisherMachine implements Machine
{
Void setRPMs() { . . . }
}
D) public class PolisherMachine extends Machine
{
Public void setRPMs() { . . . }
}
Public abstract class Machine
{
Public abstract void setRPMs();
) . .
}
You wish to create a subclass named PolisherMachine. Which of the following is the correct way to declare this subclass?
A) public class PolisherMachine implements Machine
{
Public void setRPMs() { . . . }
}
B) public class PolisherMachine extends Machine
{
Void setRPMs() { . . . }
}
C) public class PolisherMachine implements Machine
{
Void setRPMs() { . . . }
}
D) public class PolisherMachine extends Machine
{
Public void setRPMs() { . . . }
}
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
49
Consider the following code snippet:
Public class Motorcycle extends Vehicle
{
) . .
Public Motorcycle(int numberAxles)
{
Super(numberAxles);
}
}
What does this code do?
A) It invokes the constructor of the Vehicle class from within the constructor of the Motorcycle class.
B) It invokes the constructor of the Motorcycle class from within the constructor of the Vehicle class.
C) It invokes a private method of the Vehicle class from within a method of the Motorcycle class.
D) This code will not compile.
Public class Motorcycle extends Vehicle
{
) . .
Public Motorcycle(int numberAxles)
{
Super(numberAxles);
}
}
What does this code do?
A) It invokes the constructor of the Vehicle class from within the constructor of the Motorcycle class.
B) It invokes the constructor of the Motorcycle class from within the constructor of the Vehicle class.
C) It invokes a private method of the Vehicle class from within a method of the Motorcycle class.
D) This code will not compile.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
50
A class from which you cannot create objects is called a/an ____.
A) Abstract class.
B) Concrete class.
C) Non-inheritable class.
D) Superclass.
A) Abstract class.
B) Concrete class.
C) Non-inheritable class.
D) Superclass.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
51
If a class has an abstract method, which of the following statements is NOT true?
A) You can construct an object from this class.
B) You cannot construct an object from this class.
C) You cannot inherit from this class.
D) All non-abstract subclasses of this class must implement this method.
A) You can construct an object from this class.
B) You cannot construct an object from this class.
C) You cannot inherit from this class.
D) All non-abstract subclasses of this class must implement this method.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
52
Consider the following code snippet:
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 ____.
A) the hierarchy of the classes.
B) the variable's type.
C) the actual object type.
D) it is not possible to determine which method is executed.
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 ____.
A) the hierarchy of the classes.
B) the variable's type.
C) the actual object type.
D) it is not possible to determine which method is executed.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
53
Consider the following code snippet:
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 ____.
A) the actual object type.
B) the variable's type.
C) the hierarchy of the classes.
D) it is not possible to determine which method is executed.
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 ____.
A) the actual object type.
B) the variable's type.
C) the hierarchy of the classes.
D) it is not possible to determine which method is executed.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
54
Consider the following code snippet:
Public class Motorcycle extends Vehicle
{
) . .
Public Motorcycle(int numberAxles)
{
Super.numberAxles;
}
}
What does this code do?
A) It invokes the constructor of the Vehicle class from within the constructor of the Motorcycle class.
B) It invokes the constructor of the Motorcycle class from within the constructor of the Vehicle class.
C) It invokes a private method of the Vehicle class from within a method of the Motorcycle class.
D) This code will not compile.
Public class Motorcycle extends Vehicle
{
) . .
Public Motorcycle(int numberAxles)
{
Super.numberAxles;
}
}
What does this code do?
A) It invokes the constructor of the Vehicle class from within the constructor of the Motorcycle class.
B) It invokes the constructor of the Motorcycle class from within the constructor of the Vehicle class.
C) It invokes a private method of the Vehicle class from within a method of the Motorcycle class.
D) This code will not compile.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
55
Which of the following statements about abstract methods is true?
A) An abstract method has a name, parameters, and a return type, but no code in the body of the method.
B) An abstract method has parameters, a return type, and code in its body, but has no defined name.
C) An abstract method has a name, a return type, and code in its body, but has no parameters.
D) An abstract method has only a name and a return type, but no parameters or code in its body.
A) An abstract method has a name, parameters, and a return type, but no code in the body of the method.
B) An abstract method has parameters, a return type, and code in its body, but has no defined name.
C) An abstract method has a name, a return type, and code in its body, but has no parameters.
D) An abstract method has only a name and a return type, but no parameters or code in its body.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
56
Which of the following statements about classes is true?
A) You can create an object from a concrete class, but not from an abstract class.
B) You can create an object from an abstract class, but not from a concrete class.
C) You cannot have an object reference whose type is an abstract class.
D) You cannot create subclasses from abstract classes.
A) You can create an object from a concrete class, but not from an abstract class.
B) You can create an object from an abstract class, but not from a concrete class.
C) You cannot have an object reference whose type is an abstract class.
D) You cannot create subclasses from abstract classes.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
57
Consider the following code snippet:
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 ____.
A) inheritance disambiguation.
B) inheritance hierarchy.
C) dynamic inheritance.
D) dynamic lookup.
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 ____.
A) inheritance disambiguation.
B) inheritance hierarchy.
C) dynamic inheritance.
D) dynamic lookup.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
58
Which of the following statements about classes is NOT true?
A) You cannot create an object from a class declared with the keyword final.
B) You can override methods in a class declared with the keyword final.
C) You cannot extend a class declared with the keyword final.
D) You cannot create subclasses from a class declared with the keyword final.
A) You cannot create an object from a class declared with the keyword final.
B) You can override methods in a class declared with the keyword final.
C) You cannot extend a class declared with the keyword final.
D) You cannot create subclasses from a class declared with the keyword final.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
59
Consider the following code snippet:
Public class Auto extends Vehicle
{
) . .
Public Auto(int numberAxles)
{
Super(numberAxles);
}
}
What does this code do?
A) It invokes the constructor of the Vehicle class from within the constructor of the Auto class.
B) It invokes the constructor of the Auto class from within the constructor of the Vehicle class.
C) It invokes a private method of the Vehicle class from within a method of the Auto class.
D) This code will not compile.
Public class Auto extends Vehicle
{
) . .
Public Auto(int numberAxles)
{
Super(numberAxles);
}
}
What does this code do?
A) It invokes the constructor of the Vehicle class from within the constructor of the Auto class.
B) It invokes the constructor of the Auto class from within the constructor of the Vehicle class.
C) It invokes a private method of the Vehicle class from within a method of the Auto class.
D) This code will not compile.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
60
Consider the following code snippet:
Public class Motorcycle extends Vehicle
{
) . .
Public Motorcycle(int numberAxles)
{
Super(numberAxles); //line #1
}
}
If the line marked "//line #1" was missing, which of these statements would be correct?
A) The Motorcycle class constructor would invoke the constructor of the Vehicle class with no parameters.
B) The Vehicle class constructor would invoke the constructor of the Motorcycle class with no parameters.
C) The Motorcycle class constructor would invoke the constructor of the Vehicle class with a parameter value of 0.
D) This code would not compile.
Public class Motorcycle extends Vehicle
{
) . .
Public Motorcycle(int numberAxles)
{
Super(numberAxles); //line #1
}
}
If the line marked "//line #1" was missing, which of these statements would be correct?
A) The Motorcycle class constructor would invoke the constructor of the Vehicle class with no parameters.
B) The Vehicle class constructor would invoke the constructor of the Motorcycle class with no parameters.
C) The Motorcycle class constructor would invoke the constructor of the Vehicle class with a parameter value of 0.
D) This code would not compile.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
61
Consider the following code snippet:
Public class Coin
{
) . .
Public boolean equals(Coin otherCoin)
{
) . .
}
) . .
}
What is wrong with this code?
A) A class cannot override the equals method of the Object class.
B) The equals method must be declared as private.
C) A class cannot change the parameters of a superclass method when overriding it.
D) There is nothing wrong with this code.
Public class Coin
{
) . .
Public boolean equals(Coin otherCoin)
{
) . .
}
) . .
}
What is wrong with this code?
A) A class cannot override the equals method of the Object class.
B) The equals method must be declared as private.
C) A class cannot change the parameters of a superclass method when overriding it.
D) There is nothing wrong with this code.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
62
Consider the following code snippet:
Employee programmer = new Employee(10254, "exempt");
String s = programmer.toString();
Assume that the Employee class has not implemented its own toString() method. What value will s contain when this code is executed?
A) s will contain the values of the instance variables in programmer.
B) s will contain only the name of the programmer object.
C) s will contain the name of the programmer object followed by a hash code.
D) This code will not compile.
Employee programmer = new Employee(10254, "exempt");
String s = programmer.toString();
Assume that the Employee class has not implemented its own toString() method. What value will s contain when this code is executed?
A) s will contain the values of the instance variables in programmer.
B) s will contain only the name of the programmer object.
C) s will contain the name of the programmer object followed by a hash code.
D) This code will not compile.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
63
Consider the following code snippet:
Public class Score
{
Private String name;
) . .
Public boolean equals(Object otherScore)
{
Return name.equals(otherScore.name);
}
) . .
}
What is wrong with this code?
A) The return statement should use the == operator instead of the equals method.
B) The parameter in the equals method should be declared as Score otherScore.
C) otherScore must be cast as a Score object before using the equals method.
D) There is nothing wrong with this code.
Public class Score
{
Private String name;
) . .
Public boolean equals(Object otherScore)
{
Return name.equals(otherScore.name);
}
) . .
}
What is wrong with this code?
A) The return statement should use the == operator instead of the equals method.
B) The parameter in the equals method should be declared as Score otherScore.
C) otherScore must be cast as a Score object before using the equals method.
D) There is nothing wrong with this code.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
64
Which of the following is true regarding inheritance?
A) When creating a subclass, all methods of the superclass must be overridden.
B) When creating a subclass, no methods of a superclass can be overridden.
C) A superclass can force a programmer to override a method in any subclass created from it.
D) A superclass cannot prevent a programmer from overriding a method in any subclass created from it.
A) When creating a subclass, all methods of the superclass must be overridden.
B) When creating a subclass, no methods of a superclass can be overridden.
C) A superclass can force a programmer to override a method in any subclass created from it.
D) A superclass cannot prevent a programmer from overriding a method in any subclass created from it.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
65
When declared as protected, data in an object can be accessed by ____.
A) Only by that class's methods and by all of its subclasses
B) Only by that class's methods, by all of its subclasses, and by methods in classes within the same package.
C) Only by that class's methods.
D) By any class.
A) Only by that class's methods and by all of its subclasses
B) Only by that class's methods, by all of its subclasses, and by methods in classes within the same package.
C) Only by that class's methods.
D) By any class.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
66
To ensure that an instance variable can only be accessed by the class that declared it, the variable should be declared as ____.
A) public
B) private
C) protected
D) final
A) public
B) private
C) protected
D) final
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
67
Consider the following code snippet:
Public class Coin
{
Private String name;
) . .
Public boolean equals(Object otherCoin)
{
Return name.equals(otherCoin.name);
}
) . .
}
What is wrong with this code?
A) The return statement should use the == operator instead of the equals method.
B) The parameter in the equals method should be declared as Coin otherCoin.
C) otherCoin must be cast as a Coin object before using the equals method.
D) There is nothing wrong with this code.
Public class Coin
{
Private String name;
) . .
Public boolean equals(Object otherCoin)
{
Return name.equals(otherCoin.name);
}
) . .
}
What is wrong with this code?
A) The return statement should use the == operator instead of the equals method.
B) The parameter in the equals method should be declared as Coin otherCoin.
C) otherCoin must be cast as a Coin object before using the equals method.
D) There is nothing wrong with this code.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
68
With a few exceptions, instance variables of classes should always have ___ access.
A) final
B) private
C) public
D) protected
A) final
B) private
C) public
D) protected
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
69
Consider the following code snippet:
If (anObject instanceof Auto)
{
Auto anAuto = (Auto) anObject;
) . .
}
What does this code do?
A) This code tests whether anObject was created from a superclass of Auto.
B) This code creates a subclass type object from a superclass type object.
C) This class safely converts an object of any type to an object of type Auto.
D) This code safely converts an object of type Auto or a subclass of Auto to an object of type Auto.
If (anObject instanceof Auto)
{
Auto anAuto = (Auto) anObject;
) . .
}
What does this code do?
A) This code tests whether anObject was created from a superclass of Auto.
B) This code creates a subclass type object from a superclass type object.
C) This class safely converts an object of any type to an object of type Auto.
D) This code safely converts an object of type Auto or a subclass of Auto to an object of type Auto.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
70
Consider the following code snippet:
Int numAxles = 4;
String s = "Number of axles is " + numAxles;
Which of the following statements is correct?
A) The toString method of the Object class is being used to set the value of s.
B) The toString method of the Integer class is being used to set the value of s.
C) No toString method is being used to set the value of s.
D) This code will not compile.
Int numAxles = 4;
String s = "Number of axles is " + numAxles;
Which of the following statements is correct?
A) The toString method of the Object class is being used to set the value of s.
B) The toString method of the Integer class is being used to set the value of s.
C) No toString method is being used to set the value of s.
D) This code will not compile.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
71
Consider the following code snippet:
Int vacationDays = 10;
String output = "Number of earned vacation days is " + vacationDays;
Which of the following statements is correct?
A) The toString method of the Object class is being used to set the value of output.
B) The toString method of the Integer class is being used to set the value of output.
C) No toString method is being used to set the value of output.
D) This code will not compile.
Int vacationDays = 10;
String output = "Number of earned vacation days is " + vacationDays;
Which of the following statements is correct?
A) The toString method of the Object class is being used to set the value of output.
B) The toString method of the Integer class is being used to set the value of output.
C) No toString method is being used to set the value of output.
D) This code will not compile.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
72
Consider the following code snippet:
Public class Vehicle
{
Protected int numberAxles;
) . .
}
Data in the numberAxles variable can be accessed by ____.
A) Only by the Vehicle class's methods and by all of its subclasses
B) Only by the Vehicle class's methods, by all of its subclasses, and by methods in classes within the same package.
C) Only by the Vehicle class's methods.
D) By any class.
Public class Vehicle
{
Protected int numberAxles;
) . .
}
Data in the numberAxles variable can be accessed by ____.
A) Only by the Vehicle class's methods and by all of its subclasses
B) Only by the Vehicle class's methods, by all of its subclasses, and by methods in classes within the same package.
C) Only by the Vehicle class's methods.
D) By any class.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
73
Consider the following code snippet:
Auto consumerAuto = new Auto(4, "gasoline");
String s = consumerAuto.toString();
Assume that the Auto class has not implemented its own toString() method. What value will s contain when this code is executed?
A) s will contain the values of the instance variables in consumerAuto.
B) s will contain only the name of the consumerAuto object.
C) s will contain the name of the consumerAuto object followed by a hash code.
D) This code will not compile.
Auto consumerAuto = new Auto(4, "gasoline");
String s = consumerAuto.toString();
Assume that the Auto class has not implemented its own toString() method. What value will s contain when this code is executed?
A) s will contain the values of the instance variables in consumerAuto.
B) s will contain only the name of the consumerAuto object.
C) s will contain the name of the consumerAuto object followed by a hash code.
D) This code will not compile.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
74
Consider the following code snippet of a function object
Public interface Measurer
{
Double measure(______ anObject);
}
Complete this code to allow the interface to handle all classes?
A) Class
B) Object
C) Any
D) Void
Public interface Measurer
{
Double measure(______ anObject);
}
Complete this code to allow the interface to handle all classes?
A) Class
B) Object
C) Any
D) Void
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
75
Consider the following code snippet:
Double salary = 45000.00;
String sal = "Current salary is " + salary;
Which of the following statements is correct?
A) The toString method of the Object class is being used to set the value of sal.
B) The toString method of the Double class is being used to set the value of sal.
C) No toString method is being used to set the value of sal.
D) This code will not compile.
Double salary = 45000.00;
String sal = "Current salary is " + salary;
Which of the following statements is correct?
A) The toString method of the Object class is being used to set the value of sal.
B) The toString method of the Double class is being used to set the value of sal.
C) No toString method is being used to set the value of sal.
D) This code will not compile.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
76
Which of the following statements about comparing objects is correct?
A) The equals method is used to compare whether two references are to the same object.
B) The equals method is used to compare whether two objects have the same contents.
C) The == operator is used to compare whether two objects have the same contents.
D) The equals method and the == operator are perform the same actions.
A) The equals method is used to compare whether two references are to the same object.
B) The equals method is used to compare whether two objects have the same contents.
C) The == operator is used to compare whether two objects have the same contents.
D) The equals method and the == operator are perform the same actions.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
77
Consider the following code snippet:
Employee anEmployee = new Programmer();
String emp = anEmployee.toString();
Assume that the Programmer class inherits from the Employee class, and neither class has an implementation of the toString method. Which of the following statements is correct?
A) The toString method of the Object class will be used when this code is executed.
B) The toString method of the String class will be used when this code is executed.
C) This code will not compile because there is no toString method in the Employee class.
D) This code will not compile because there is no toString method in the Programmer class.
Employee anEmployee = new Programmer();
String emp = anEmployee.toString();
Assume that the Programmer class inherits from the Employee class, and neither class has an implementation of the toString method. Which of the following statements is correct?
A) The toString method of the Object class will be used when this code is executed.
B) The toString method of the String class will be used when this code is executed.
C) This code will not compile because there is no toString method in the Employee class.
D) This code will not compile because there is no toString method in the Programmer class.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
78
The ____ reserved word in a class definition ensures that subclasses cannot be created from this class.
A) abstract
B) anonymous
C) final
D) static
A) abstract
B) anonymous
C) final
D) static
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
79
The ____ reserved word in a method definition ensures that subclasses cannot override this method.
A) abstract
B) anonymous
C) final
D) static
A) abstract
B) anonymous
C) final
D) static
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck
80
Consider the following code snippet:
Vehicle aVehicle = new Auto(4,"gasoline");
String s = aVehicle.toString();
Assume that the Auto class inherits from the Vehicle class, and neither class has an implementation of the toString() method. Which of the following statements is correct?
A) The toString() method of the Object class will be used when this code is executed.
B) The toString() method of the String class will be used when this code is executed.
C) This code will not compile because there is no toString() method in the Vehicle class.
D) This code will not compile because there is no toString() method in the Auto class.
Vehicle aVehicle = new Auto(4,"gasoline");
String s = aVehicle.toString();
Assume that the Auto class inherits from the Vehicle class, and neither class has an implementation of the toString() method. Which of the following statements is correct?
A) The toString() method of the Object class will be used when this code is executed.
B) The toString() method of the String class will be used when this code is executed.
C) This code will not compile because there is no toString() method in the Vehicle class.
D) This code will not compile because there is no toString() method in the Auto class.
Unlock Deck
Unlock for access to all 99 flashcards in this deck.
Unlock Deck
k this deck