Test Bank Answers Inheritance Chapter 10 - Java Control Structures 7e Test Bank by Tony Gaddis. DOCX document preview.
Starting Out with Java: From Control Structures through Objects 7e (Gaddis)
Chapter 10 Inheritance
TRUE/FALSE
1. Because every class directly or indirectly inherits from the Object class, every class inherits the Object class's members.
2. If a method in a subclass has the same signature as a method in the superclass, the subclass method overrides the superclass method.
3. An abstract class is not instantiated itself but serves as a superclass for other classes.
4. You can write a super statement that calls a superclass constructor but only in the subclass's constructor.
5. In an inheritance relationship, the subclass constructor always executes before the superclass constructor.
6. Because the subclass is more specialized than the superclass, it is sometimes necessary for the subclass to replace inadequate superclass methods with more suitable ones.
7. When a subclass overrides a superclass method, only the subclass's version of the method can be called with a subclass object.
8. Inheritance involves a subclass, which is the general class, and a superclass, which is the specialized class.
9. It is not possible for a superclass to call a subclass's method.
10. When a subclass extends a superclass, the public members of the superclass become public members of the subclass.
11. If two methods in the same class have the same name but different signatures, the second overrides the first.
12. Every class has a toString method and an equals method inherited from the Object class.
13. All methods in an abstract class must also be declared abstract.
14. A compiler error will result if an anonymous inner class tries to use a variable that is not final, or not effectively final.
15. A functional interface is simply an interface that has one abstract method.
MULTIPLE CHOICE
1. A __________ member's access is somewhere between public and private.
a. | package | b. | protected | c. | static | d. | final |
2. Which of the following shows the inheritance relationships among classes in a manner similar to that of a family tree?
a. | UML diagram | c. | flowchart |
b. | CRC card | d. | class hierarchy |
3. In Java, a reference variable is __________ because it can reference objects of types different from its own, as long as those types are related to its type through inheritance.
a. | static | b. | dynamic | c. | polymorphic | d. | public |
4. A(n) __________ method is a method that appears in a superclass but expects to be overridden in a subclass.
a. | abstract | b. | protected | c. | static | d. | overloaded |
5. If a subclass constructor does not explicitly call a superclass constructor, __________.
a. | the superclass's fields will be set to the default values for their data types |
b. | Java will automatically call the superclass's default or no-arg constructor immediately after the code in the subclass's constructor executes |
c. | it must include the code necessary to initialize the superclass fields |
d. | Java will automatically call the superclass's default or no-arg constructor just before the code in the subclass's constructor executes |
6. Which of the following is true about protected access?
a. | Protected members may be accessed by methods in the same package or in a subclass, even when the subclass is in a different package. |
b. | Protected members may be accessed by methods in the same package or in a subclass, but only if the subclass is in the same package. |
c. | Protected members cannot be accessed by methods in any other classes. |
d. | Protected members are actually named constants. |
7. All fields declared in an interface __________.
a. | have protected access |
b. | must be initialized in the class implementing the interface |
c. | have private access |
d. | are treated as final and static |
8. If a class contains an abstract method __________.
a. | you must create an instance of the class |
b. | the method will only have a header, but not a body, and will end with a semicolon |
c. | the method cannot be overridden in subclasses |
d. | All of these are true. |
9. In an inheritance relationship __________.
a. | the subclass constructor always executes before the superclass constructor |
b. | the superclass constructor always executes before the subclass constructor |
c. | the constructor with the lowest overhead always executes first regardless of inheritance in subclasses |
d. | the unified constructor always executes first regardless of inheritance |
10. __________ is a special type of expression used to create an object that implements a functional interface.
a. | lambda | b. | beta | c. | alpha | d. | sigma |
11. What is required for an interface method that has a body?
a. | The method header must begin with the key word default. |
b. | A class that implements the interface must override the method. |
c. | The @Default annotation must precede the method header. |
d. | All of these are true. |
12. __________ tells the Java compiler that a method is meant to override a method in the superclass.
a. | @Override | c. | @Protected |
b. | @Overload | d. | @Inherited |
13. The __________ key word is used to call a superclass constructor explicitly.
a. | goto | b. | this | c. | super | d. | extends |
14. Which of the following is an example of a lambda expression?
a. | int x = x * factor; |
b. | IntCalculator = new divider(x, 2); |
c. | IntCalculator multiplier = x -> x * factor; |
d. | Any of these are examples of a lambda expression. |
15. What type of relationship exists between two objects when one object is a specialized version of another object?
a. | "is a" | c. | "has a" |
b. | "contains a" | d. | "consists of" |
16. A subclass can directly access __________.
a. | only protected and private members of the superclass |
b. | all members of the superclass |
c. | only public and private members of the superclass |
d. | only public and protected members of the superclass |
17. When an "is a" relationship exists between objects, the specialized object has __________.
a. | some of the characteristics of the general class, but not all, plus additional characteristics |
b. | some, but not all, of the characteristics of the general object |
c. | none of the characteristics of the general object |
d. | all of the characteristics of the general object plus additional characteristics |
18. If you don't provide an access specifier for a class member, the class member is given __________ access by default.
a. | private | b. | public | c. | protected | d. | package |
19. A protected member of a class may be directly accessed by __________.
a. | methods of the same class | c. | methods in the same package |
b. | methods of a subclass | d. | Any of these |
20. All methods specified by an interface are __________.
a. | private | b. | public | c. | protected | d. | static |
21. If a method in a subclass has the same signature as a method in the superclass, the subclass method __________ the superclass method.
a. | inherits | c. | overrides |
b. | overloads | d. | implements |
22. In __________, inheritance is shown with a line that has an open arrowhead at one end that points to the superclass.
a. | pseudocode | c. | a CRC card |
b. | a UML diagram | d. | a hierarchy chart |
23. In a class hierarchy __________.
a. | the more general classes are toward the right of the tree and the more specialized classes are toward the left |
b. | the more general classes are toward the top of the tree and the more specialized classes are toward the bottom |
c. | the more general classes are toward the left of the tree and the more specialized classes are toward the right |
d. | the more general classes are toward the bottom of the tree and the more specialized classes are toward the top |
24. A subclass may call an overridden superclass method by __________.
a. | prefixing its name with the super key word and a dot (.) |
b. | prefixing its name with the name of the superclass in parentheses |
c. | using the extends key word before the method is called |
d. | calling the superclass method first and then calling the subclass method |
25. Which key word indicates that a class inherits from another class?
a. | final | c. | implements |
b. | super | d. | extends |
26. When a method is declared with the __________ modifier, it cannot be overridden in a subclass.
a. | final | b. | super | c. | void | d. | public |
27. Which of the following is the operator used to determine whether an object is an instance of a particular class?
a. | equals | c. | >> |
b. | instanceOf | d. | isa |
28. A class becomes abstract when you place the __________ key word in the class definition.
a. | super | b. | extends | c. | final | d. | abstract |
29. Protected class members can be denoted in a UML diagram with the __________ symbol.
a. | + | b. | - | c. | * | d. | # |
30. Which of the following statements declares Salaried as a subclass of PayType?
a. | public class Salaried implements PayType |
b. | public class PayType derives Salaried |
c. | public class Salaried extends PayType |
d. | public class Salaried derivedFrom(PayType) |
31. If ClassC is derived from ClassB which is derived from ClassA, this would be an example of
a. | a chain of inheritance | c. | multiple interfaces |
b. | linear inheritance | d. | cascading classes |
32. Which of the following statements correctly specifies two interfaces?
a. | public class ClassA implements [Interface1, Interface2] |
b. | public class ClassA implements (Interface1, Interface2) |
c. | public class ClassA implements Interface1, Interface2 |
d. | public class ClassA implements Interface1 | Interface2 |
33. In the following statement, which is the superclass?
public class ClassA extends ClassB implements ClassC
a. | ClassA | c. | ClassC |
b. | ClassB | d. | cannot tell |
34. In the following statement, which is the subclass?
public class ClassA extends ClassB implements ClassC
a. | ClassA | c. | ClassC |
b. | ClassB | d. | cannot tell |
35. In the following statement, which is the interface?
public class ClassA extends ClassB implements ClassC
a. | ClassA | c. | ClassC |
b. | ClassB | d. | all are interfaces |
36. What is wrong with the following code?
public class ClassB extends ClassA
{
public ClassB()
{
int init = 10;
super(40);
}
}
a. | Nothing is wrong with this code. |
b. | The method super is not defined. |
c. | The call to the method super must be the first statement in the constructor. |
d. | No values may be passed to super. |
37. In the following code, what will the call to super do?
public class ClassB extends ClassA
{
public ClassB()
{
super(40);
System.out.println("This is the last statement "+
"in the constructor.");
}
}
a. | This cannot be determined from the code. |
b. | It will call the method super and pass the value 40 to it as an argument. |
c. | It will call the constructor of ClassA that receives an integer as an argument. |
d. | The method super will have to be defined before we can say what will happen. |
38. In the following code, which line has an error?
Line 1 public interface Interface1
Line 2 {
Line 3 int FIELDA = 55;
Line 4 public int methodA(double){}
Line 5 }
a. | Line 1 | b. | Line 2 | c. | Line 3 | d. | Line 4 |
39. In the following code, which line in ClassA has an error?
Line 1 public interface MyInterface
Line 2 {
Line 3 int FIELDA = 55;
Line 4 public int methodA(double);
Line 5 }
Line 6 public class ClassA implements MyInterface
Line 7 {
Line 8 FIELDA = 60;
Line 9 public int methodA(double) { }
Line 10 }
a. | Line 6 | b. | Line 7 | c. | Line 8 | d. | Line 9 |
40. In the following code, which line will cause a compiler error?
Line 1 public class ClassA
Line 2 {
Line 3 public ClassA() {}
Line 4 public int method1(int a){}
Line 5 public final int method2(double b){}
Line 6 }
Line 7 public ClassB extends ClassA
Line 8 {
Line 9 public ClassB(){}
Line 10 public int method1(int b){}
Line 11 public int method2(double c){}
Line 12 }
a. | Line 4 | b. | Line 5 | c. | Line 10 | d. | Line 11 |
41. Given the following code:
Line 1 public class ClassA
Line 2 {
Line 3 public ClassA() {}
Line 4 public void method1(int a){}
Line 5 }
Line 6 public class ClassB extends ClassA
Line 7 {
Line 8 public ClassB(){}
Line 9 public void method1(){}
Line 10 }
Line 11 public class ClassC extends ClassB
Line 12 {
Line 13 public ClassC(){}
Line 14 public void method1(){}
Line 15 }
Which method will be executed when the following statements are executed?
ClassC item1 = new ClassA();
item1.method1();
a. | Line 4 |
b. | Line 9 |
c. | Line 14 |
d. | This is an error and will cause the program to crash. |
42. Given the following code:
Line 1 public class ClassA
Line 2 {
Line 3 public ClassA() {}
Line 4 public void method1(int a){}
Line 5 }
Line 6 public class ClassB extends ClassA
Line 7 {
Line 8 public ClassB(){}
Line 9 public void method1(){}
Line 10 }
Line 11 public class ClassC extends ClassB
Line 12 {
Line 13 public ClassC(){}
Line 14 public void method1(){}
Line 15 }
Which method1 will be executed when the following statements are executed?
ClassA item1 = new ClassB();
item1.method1();
a. | method1 on Line 4 |
b. | method1 on Line 9 |
c. | method1 on Line 14 |
d. | This is an error and will cause the program to crash. |
43. In the following code, what is missing from ClassA?
Line 1 public interface MyInterface
Line 2 {
Line 3 int FIELDA = 55;
Line 4 public int methodA(double);
Line 5 }
Line 6 public class ClassA implements MyInterface
Line 7 {
Line 8 FIELDA = 60;
Line 9 public int methodB(double) { }
Line 10 }
a. | It does not override methodA. |
b. | It does not have a constructor. |
c. | It does not overload methodA. |
d. | Nothing is missing. It is a complete class. |
44. Given the following code, which statement is true?
public class ClassB implements ClassA{ }
a. | ClassA must override each method in ClassB. |
b. | ClassB must override each method in ClassA. |
c. | ClassB inherits from ClassA. |
d. | ClassA inherits from ClassB. |
45. If a subclass constructor does not explicitly call a superclass constructor __________.
a. | it must include the code necessary to initialize the superclass fields |
b. | the superclass fields will be set to the default values for their data types |
c. | Java will automatically call the superclass's default or no-arg constructor immediately after the code in the subclass's constructor executes |
d. | Java will automatically call the superclass's default or no-arg constructor just before the code in the subclass's constructor executes |
46. Replacing inadequate superclass methods with more suitable subclass methods is known as __________.
a. | method upgrading | c. | method overriding |
b. | tactical inheritance | d. | method overloading |
47. If two methods have the same name but different signatures they are __________.
a. | overridden | c. | superclass methods |
b. | overloaded | d. | subclass methods |
48. When a subclass overloads a superclass method __________.
a. | both methods may be called with a subclass object |
b. | only the subclass method may be called with a subclass object |
c. | only the superclass method may be called with a subclass object |
d. | neither method may be called with a subclass object |
49. When declaring class data members it is best to declare them as __________.
a. | private members | c. | protected members |
b. | public members | d. | restricted members |