APCS Chapter 9 Test Review
Review question 9 and 11
Review question 9 and 11
62. Consider the classes shown below: public class Parent { public int getValue() { return 24; } public void display() { System.out.print(getValue() + " "); } } public class Child extends Parent { public int getValue() { return -7; } } Using the classes above, what is the output of the following lines of code? Parent kid = new Child(); Parent adult = new Parent(); kid.display(); adult.display(); a) -7 24 b) 24 24 c) -7 -7 d) 24 -7
a) -7 24
16. Consider the classes shown below: public class Parent { private int value = 100; public int getValue() { return value; } } public class Child extends Parent { private int value; public Child(int number) { value = number; } } What is the output of the following lines of code? Child kid = new Child(-14); Parent adult = new Parent(); System.out.println(kid.getValue() + " " + adult.getValue()); a) 100 100 b) -14 100 c) -14 -14 d) 100 -14
a) 100 100
18. Consider the classes shown below: public class Parent { private int value = 100; public int getValue() { return value; } } public class Child extends Parent { private int value; public Child(int number) { value = number; } } What is the output of the following lines of code? Child kid = new Child(-14); Parent kid2 = new Child(21); System.out.println(kid.getValue() + " " + kid2.getValue()); a) 100 100 b) -14 21 c) 21 21 d) -14 100
a) 100 100
15. Consider the classes shown below: public class Parent { public void doSomething(){/* Implementation not shown */} } public class Child extends Parent { public void doAnotherThing(){/* Implementation not shown */} } Which lines in the following code will compile without error? Parent kid = new Child(); kid.doSomething(); // line 1 kid.doAnotherThing(); // line 2 a) Line 1 only b) Line 2 only c) Lines 1 and 2 d) Neither line will compile without error
a) Line 1 only
13. All rodents are mammals and all canines are mammals. No canines are rodents and no rodents are canines. What hierarchy best captures this information? a) Mammal is a superclass of Rodent and Mammal b) Rodent is a superclass of Mammal and Canine is a superclass of Mammal c) Mammal is a superclass of Rodent and Rodent is a superclass of Canine d) Mammal is a superclass of Canine and Canine is a superclass of Rodent
a) Mammal is a superclass of Rodent and Mammal
60. Consider the classes shown below: public class Parent { public void doSomething() // method 1 { /* Implementation not shown */ } } public class Child extends Parent { public void doSomething(int n) // method 2 { /* Implementation not shown */ } public void doSomething() // method 3 { /* Implementation not shown */ } } If the variable kid is defined below, which version of the doSomething method can be called on the variable kid? Parent kid = new Child(); a) Method 1 only b) Methods 2 and 3 only c) Methods 1 and 2 only d) Methods 1, 2, and 3
a) Method 1 only
43. 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? (I changed the wording below, because methods override methods. . .classes don't override methods) a) The Motorcycle class's setVehicleClass method overrides the Vehicle class's setVehicleClass method. b) The Vehicle class's setVehicleClass method overrides the Motorcycle class's setVehicleClass method. c) The Motorcycle class's setVehicleClass method overloads the Vehicle class's setVehicleClass method. d) The Vehicle class's setVehicleClass method overloads the Motorcycle class's setVehicleClass method.
a) The Motorcycle class's setVehicleClass method overrides the Vehicle class's setVehicleClass method.
44. 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's setVesselClass method overrides the Vessel class's setVesselClass method. b) The Vessel class's setVesselClass method overrides the Speedboat class's setVesselClass method. c) The Speedboat class's setVesselClass method overloads the Vessel class's setVesselClass method. d) The Vessel class's setVesselClass method overloads the Speedboat class's setVesselClass method.
a) The Speedboat class's setVesselClass method overrides the Vessel class's setVesselClass method.
52. 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.
a) This method will call itself.
34. 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.
a) myAuto.displayInfo()
59. Consider the classes shown below: public class Parent { public void doSomething() // method 1 { /* Implementation not shown */ } } public class Child extends Parent { public void doSomething(int n) // method 2 { /* Implementation not shown */ } public void doSomething() // method 3 { /* Implementation not shown */ } } If the variable kid is defined below, which version of the doSomething method can be called on the variable kid? Child kid = new Child(); a) Methods 1 and 2 only b) Method 2 only c) Methods 2 and 3 only d) Methods 1, 2, and 3
b) Method 2 only
47. 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.
b) Must use the same method name and the same parameter types.
1. 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.
b) Superclass
46. 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's setDepartment method overrides the Employee class's setDepartment method. d) The Programmer class can call the setDepartment method of the Programmer class.
b) The Employee class can call the setProjectName method.
45. 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's SetVehicleClass method overrides the Vehicle class's setVehicleClass method. d) The Motorcycle class can call the setVehicleClass method of the Motorcycle class.
b) The Vehicle class can call the setModelName method.
3. 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.
b) Vehicle should be the superclass, while Auto and Motorcycle should be the subclasses.
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.
b) You can always use a subclass object in place of a superclass object.
64. Consider the Counter class below. public class Counter { public int count = 0; public int getCount() { return count; } public void increment() { count++; } } Using the class above and the variable declared below, what is the value of num.toString()? Counter num = new Counter(); a) a string with count's value b) a string with num's type and hashcode c) a string with count's type and hashcode d) nothing since toString is not defined for Counter
b) a string with num's type and hashcode
19. Suppose the class Value is partially defined below public class Value { private int number; public int getValue() { 11Test Bank Chapter 9 © John Wiley & Sons, Inc. All rights reserved. return number; } } A subclass of Value, LargerValue, is defined with a getValue method that returns twice the value of the parent. Which line is the body of LargerValues9 getValue method? a) return getValue() * 2; b) return super.getValue() * 2; c) return number * 2; d) return super.number * 2;
b) return super.getValue() * 2;
42. If a subclass defines the same method name and the same parameter types for a method that appears in its superclass, ____. a) the subclass method overloads the superclass method. b) the subclass method overrides the superclass method. c) the subclass has implemented its superclass's method. d) a compiler error will occur.
b) the subclass method overrides the superclass method.
61. Consider the classes shown below: public class Parent { public int getValue() { return 24; } public void display() { System.out.print(getValue() + " "); } } public class Child extends Parent { public int getValue() { return -7; } } Using the classes above, what is the output of the following lines of code? Child kid = new Child(); Parent adult = new Parent(); kid.display(); adult.display(); a) 24 24 b) -7 -7 c) -7 24 d) 24 -7
c) -7 24
25. 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.
c) A subclass inherits methods and instance variables from its superclass.
14. Consider the classes shown below: public class Parent { public void doSomething(){/* Implementation not shown */} } public class Child extends Parent { public void doAnotherThing(){/* Implementation not shown */} } Which lines in the following code will compile without error? Child kid = new Child(); kid.doSomething(); // line 1 kid.doAnotherThing(); // line 2 a) Line 1 only b) Line 2 only c) Lines 1 and 2 d) Neither line will compile without error
c) Lines 1 and 2
2. 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.
c) Subclass.
49. 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.
c) The Programmer class overloads the setEmployeeDept method.
38. 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.
c) The subclass is overriding a superclass method.
39. Consider the following code snippet: public class Vessel { 21Test Bank Chapter 9 © John Wiley & Sons, Inc. All rights reserved. . . . 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.
c) The subclass is overriding a superclass method.
30. 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 private instance variable. d) The subclass must have its own public method to update the superclass
c) The subclass must use a public method of the superclass (if it exists) to update the superclass
17. Consider the classes shown below: public class Parent { private int value = 100; public int getValue() { return value; } } public class Child extends Parent { private int value; public Child(int number) { value = number; } } What is the output of the following lines of code? Child kid = new Child(-14); Child kid2 = new Child(21); System.out.println(kid.getValue() + " " + kid2.getValue()); a) -14 21 b) 21 21 c) 21 100 d) 100 100
d) 100 100
32. 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.
d) A Motorcycle object inherits and can directly use the method setVehicleClass but cannot directly use the instance variable manufacturer.
27. 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.
d) A subclass has no access to private instance variables of its superclass.
26. 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.
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.
67. 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");
d) LandVehicle myAuto = new Vehicle("sedan");
56. 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.
d) This code will not compile.
36. 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) super.displayInfo()
d) super.displayInfo()
20. Suppose the class Message is partially defined as shown below public class Message { private String value; public Message(String initial) { value = initial; } public String getMessage() { return value; } } A subclass of Message, ExcitedMessage, is defined that will behave like Message, except that it will add two exclamation points to the end of the message. Sample code that uses ExcitedMessage is shown below. ExcitedMessage greeting = new ExcitedMessage("Hello"); System.out.print(greeting.getMessage()); // will print "Hello!!" Which ExcitedMessage constructor will give this behavior? a) public ExcitedMessage(String line) { super(line + "!!"); } b) public ExcitedMessage(String line) { value = line + "!!"; } c) public ExcitedMessage(String line) { line = line + "!!"; super(line); } d) public ExcitedMessage(String line) { new Message(line + "!!"); }
a) public ExcitedMessage(String line) { super(line + "!!"); }
35. 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.
a) super(type);
51. If a subclass uses the same method name but different parameter types for a method that appears in its superclass, ____. a) the subclass method has overloaded its superclass's method. b) the subclass method has overridden its superclass's method. c) the subclass has implemented its superclass's method. d) a compiler error will occur.
a) the subclass method has overloaded its superclass's method.
63. Suppose the abstract class Message is defined below public abstract class Message { private String value; public Message(String initial) { value = initial; } public String getMessage() { return value; } public abstract String translate(); } A concrete subclass of Message, FrenchMessage, is defined. Which methods must FrenchMessage define? a) translate() only b) getMessage() only c) The FrenchMessage constructor and translate() only d) The FrenchMessage constructor, getMessage(), and translate()
a) translate() only
66. Consider the Counter class below. public class Counter { public int count = 0; public int getCount() { return count; } public void increment() { count++; } } Using the class above and the variables declared below, what is the value of num1.equals(num2)? Counter num1 = new Counter(); Counter num2 = num1; a) true b) false c) nothing since equals is not defined for Counter
a) true
12. All hamsters are rodents and all rodents are mammals. What hierarchy best captures this information? a) Hamster is a superclass of Rodent and Rodent is a superclass of Mammal b) Mammal is a superclass of Rodent and Rodent is a superclass of Hamster c) Mammal is a superclass of Rodent and Hamster d) Hamster is a superclass of Rodent and Mammal
b) Mammal is a superclass of Rodent and Rodent is a superclass of Hamster