chapter 9 part 2
82) Which of the following statements about a Java interface is NOT true? a) A Java interface defines a set of methods that are required. b) A Java interface must contain more than one method. c) A Java interface specifies behavior that a class will implement. d) All methods in a Java interface must be abstract.
b) A Java interface must contain more than one method.
89) All ____ methods must be implemented when using an interface. a) abstract. b) private. c) public. d) static
a) abstract.
69) 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
a) The toString method of the Object class will be used when this code is executed.
58) 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.
59) 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.
88) Consider the following code snippet: public class Inventory implements Measurable { private int onHandCount; . . . public double getMeasure(); { return onHandCount; } } Why is it necessary to declare getMeasure as public? a) All methods in a class are not public by default. b) All methods in an interface are private by default. c) It is necessary only to allow other classes to use this method. d) It is not necessary to declare this method as public.
a) All methods in a class are not public by default.
99) Which of the following could be used in place of an interface to handle operations on different classes? a) Function objects b) Operator overloading c) Dynamic method lookup d) Abstract classes
a) Function objects
80) 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.
d) This code safely converts an object of type Auto or a subclass of Auto to an object of type Auto.
83) A method that has no implementation is called a/an ____ method. a) interface b) implementation c) overloaded d) abstract
d) abstract
85) Which of the following statements about an interface is true? a) An interface has methods and instance variables. b) An interface has methods but no instance variables. c) An interface has neither methods nor instance variables. d) An interface has instance variables but no methods.
b) An interface has methods but no instance variables.
75) 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.
b) The equals method is used to compare whether two objects have the same contents
60) 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.
b) You can override methods in a class declared with the keyword final
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
b) private
67) With a few exceptions, instance variables of classes should always have ___ access. a) final b) private c) public d) protected
b) private
95) Consider the following code snippet: public class Inventory implements Measurable { private int onHandCount; . . . double getMeasure(); { return onHandCount; } } If getMeasure() is a method in the interface Measurable, what is wrong with this code? a) The getMeasure method must be declared as private. b) The getMeasure method must include the implements keyword. c) The getMeasure method must be declared as public. d) The getMeasure method must not have any code within it.
c) The getMeasure method must be declared as public.
94) Consider the following code snippet: public class BankAccount implements Comparable<BankAccount> { . . . public int compareTo(T other) { What is wrong with this code? a) The type parameter for the Comparable interface in the implements clause must be the same as the implementing class. b) The type parameter for the Comparable interface in the implements clause must be the generic <T>. c) The type parameter for the compareTo method must be the same as the implementing class. d) The type parameter for the compareTo interface must be the generic <T>.
c) The type parameter for the compareTo method must be the same as the implementing class.
57) 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() { . . . }
d) public class Programmer extends Employee { public void setSalary() { . . . }
87) Consider the following code snippet: public interface Sizable { double size; double getSize(); } What is wrong with this code? a) The getSize method must be declared as public. b) The getSize method must be declared as public. c) The getSize method must be declared as final. d) An interface type cannot have instance variables.
d) An interface type cannot have instance variables.
56) Consider the following code snippet: public abstract class Machine { public abstract void setRPMs(); . . . } 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() { . . . } } 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 {
d) public class PolisherMachine extends Machine { public void setRPMs() { . . . } }
63) 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 superclass can force a programmer to override a method in any subclass created from it.
53) 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.
97) Consider the following code snippet: public interface Sizable { int LARGE_CHANGE = 100; int SMALL_CHANGE = 20; void changeSize(); } Which of the following statements is true? a) LARGE_CHANGE and SMALL_CHANGE are automatically public static final. b) LARGE_CHANGE and SMALL_CHANGE are instance variables. c) LARGE_CHANGE and SMALL_CHANGE must be defined with the keywords private static final. d) LARGE_CHANGE and SMALL_CHANGE must be defined with the keywords public static final.
a) LARGE_CHANGE and SMALL_CHANGE are automatically public static final.
68) 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.
a) The toString() method of the Object class will be used when this code is executed.
55) 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.
54) 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
84) Which statement about methods in an interface is true? a) All methods in an interface are automatically private. b) All methods in an interface are automatically public. c) All methods in an interface are automatically static. d) All methods in an interface must be explicitly declared as private or public
b) All methods in an interface are automatically public.
96) Consider the following code snippet: public class Inventory implements Measurable { private int onHandCount; . . . double getMeasure(); { return onHandCount; } } Assume that getMeasure() is a method in the interface Measurable. The compiler complains that the getMeasure method has a weaker access level than the Measurable interface. Why? a) All of the methods in a class have a default access level of package access, while the methods of an interface have a default access level of private. b) All of the methods in a class have a default access level of package access, while the methods of an interface have a default access level of public. c) The variable onHandCount was not declared with public access. d) The getMeasure method was declared as private in the Measurable interface.
b) All of the methods in a class have a default access level of package access, while the methods of an interface have a default access level of public.
79) 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
b) Object
64) 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.
b) Only by that class's methods, by all of its subclasses, and by methods in classes within the same package.
65) 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.
b) Only by the Vehicle class's methods, by all of its subclasses, and by methods in classes within the same package.
98) Consider the following code snippet: public interface Sizable { int LARGE_CHANGE = 100; void changeSize(); } Which of the following indicates how to use the constant LARGE_CHANGE in your program? a) LARGE_CHANGE b) Sizable.LARGE_CHANGE c) Sizable(LARGE_CHANGE) d) You cannot directly use the LARGE_CHANGE constant in your program
b) Sizable.LARGE_CHANGE
93) Consider the following code snippet: public class Inventory implements Measurable Which of the following statements about this code is correct? a) The Inventory class is an interface, while the Measurable class is a concrete class. b) The Measurable class is an interface, while the Inventory class is a concrete class. c) The Inventory class is an abstract class, while the Measurable class is a concrete class. d) The Inventory class is a subclass, while the Measurable class is a superclass.
b) The Measurable class is an interface, while the Inventory class is a concrete class.
91) You are creating a Motorcycle class which is supposed to use an interface named Measurable. Which of the following class declaration statements will accomplish this? a) public class Motorcycle inherits Measurable b) public class Motorcycle implements Measurable c) public class Motorcycle interfaces Measurable d) public class Motorcycle extends Measurable
b) public class Motorcycle implements Measurable
92) You are creating a Vessel class which is supposed to use an interface named Measurable. Which of the following class declaration statements will accomplish this? a) public class Vessel inherits Measurable b) public class Vessel implements Measurable c) public class Vessel interfaces Measurable d) public class Vessel extends Measurable
b) public class Vessel implements Measurable
76) 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.
c) A class cannot change the parameters of a superclass method when overriding it.
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.
c) No toString method is being used to set the value of output
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.
c) No toString method is being used to set the value of s.
72) 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.
c) No toString method is being used to set the value of sal.
61) The ____ reserved word in a class definition ensures that subclasses cannot be created from this class. a) abstract b) anonymous c) final d) static
c) final
62) The ____ reserved word in a method definition ensures that subclasses cannot override this method. a) abstract b) anonymous c) final d) static
c) final
78) 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.
c) otherScore must be cast as a Score object before using the equals method
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
c) s will contain the name of the consumerAuto object followed by a hash code
74) 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.
c) s will contain the name of the programmer object followed by a hash code.
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.
c) the actual object type.
81) To test whether an object belongs to a particular type, use ___. a) the this reserved word b) the subclassOf reserved word. c) the instanceof operator. d) the equals method
c) the instanceof operator.
86) To define a class that fulfills the requirements of an interface, the class header should include which of the following? a) The keyword extends and the name of an abstract method in the interface b) The keyword extends and the name of the interface c) The keyword implements and the name of an abstract method in the interface d) The keyword implements and the name of the interface
d) The keyword implements and the name of the interface
90) Which of the following can potentially be changed when implementing an interface? a) The parameters of a method defined by the interface. b) The name of a method defined by the interface. c) The return type of a method defined by the interface. d) You cannot change the name, return type, or parameters of a method defined by the interface.
d) You cannot change the name, return type, or parameters of a method defined by the interface.