Ch 11 - Inheritance

25 July 2022
4.7 (114 reviews)
50 test answers

Unlock all answers in this set

Unlock answers (46)
question
When an "is a" relationship exists between objects, it means that the specialized object has (a) Some of the characteristics of the general class, but not all, plus additional characteristics (b) Some of the characteristics of the general object, but not all (c) None of the characteristics of the general object (d) All the characteristics of the general object, plus additional characteristics
answer
(d) All the characteristics of the general object, plus additional characteristics
question
Which of the following statements declares Salaried as a subclass of PayType? (a) public class Salaried extends PayType (b) public class Salaried implements PayType (c) public class Salaried derivedFrom(Paytype) (d) public class PayType derives Salaried
answer
(a) public class Salaried extends PayType
question
Which of the following statements declares Salaried as a subclass of PayType? (a) public class Salaried extends PayType (b) public class Salaried implements PayType (c) public class Salaried derivedFrom(Paytype) (d) public class PayType derives Salaried
answer
(b) Public members in ClassB are public in ClassA, but private members in ClassB cannot be directly accessed in ClassA
question
In UML diagrams, inheritance is shown (a) With a line that has an open arrowhead at one end that points to the superclass (b) With a line that has an open arrowhead at one end that points to the subclass (c) With a line that has a closed arrowhead at one end that points to the superclass (d) With a line that has a closed arrowhead at one end that points to the subclass
answer
(a) with a line that has an open arrowhead at one end that points to the superclass
question
True/False It is not possible for a superclass to call a subclass's method.
answer
True
question
Use the following code for Questions 6 and 7. public class ClassB extends ClassA { public ClassB() { int init = 10, final = 60; super(40); } } What is wrong with the above code? (a) Nothing is wrong with the 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
answer
(c) The call to the method super must be the first statement in the constructor
question
Assuming the above code has been corrected, what will the call to super do? (a) This cannot be determined form the above code (b) It will call the constructor of ClassA that receives an integer as an argument (c) It will call the method super and pass the value 40 to it as an argument (d) The method super will have to be defined before we can say what will happen
answer
(b) It will call the constructor of ClassA that receives an integer as an argument
question
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 constructor immediately after the code in the subclass's constructor executes (d) Java will automatically call the superclass's default constructor just before the code in the subclass's constructor executes
answer
(d) Java will automatically call the superclass's default constructor just before the code in the subclass's constructor executes
question
1. public class ClassA 2. { 3. public ClassA() {} 4. public final Method1(int a){} 5. public Method 2(int b){} 6. } 7. public ClassB extends ClassA 8. { 9. public ClassB(){} 10. public Method1(int b){} 11. public Method2(double c){} 12. } The method in line ____ will override the method in line ____. (a) 10, 4 (b) 11, 5 (c) Both (a) and (b) (d) None of the above
answer
(d) None of the above
question
The method in line ____ will override the method in line ____. (a) 10, 4 (b) 11, 5 (c) Both (a) and (b) (d) None of the above
answer
(c) 10
question
True/False If a method in a subclass has the same signature as a method in the superclass, the subclass method overloads the superclass method.
answer
False
question
A protected member of a class may be directly accessed by (a) Methods of the same class (b) Methods of a subclass (c) Methods in the same package (d) All of the above
answer
(d) All of the above
question
When declaring class data members, it is best to declare them as (a) Private members (b) Public members (c) Protected members (d) Restricted members
answer
(a) private members
question
If you do not provide an access specifier for a class member, the class member is given ____ access by default. (a) Private (b) Public (c) Protected (d) Package
answer
(d) Package
question
If ClassC is derived from ClassB, which is derived from ClassA, this would be an example of (a) Multiple inheritance (b) A chain of inheritance (c) A family tree (d) Packaging
answer
(b) a chain of inheritance
question
True/False Every class is either directly or indirectly derived from the Object class.
answer
True
question
Use the following code for Questions 17-18. 1. public class ClassA 2. { 3. public ClassA() {} 4. public method1(int a){} 5. } 6. public class ClassB extends ClassA 7. { 8. public ClassB(){} 9. public method11(){} 10. } 11. public class ClassC extends ClassB 12. { 13. public ClassC(){} 14. public method1(){} 15. } Which method will be executed when the following statements are executed? ClassA item1 new ClassC(); item1.method1(); (a) Line 4 (b) Line 9 (c) Line 14 (d) This is an error and will cause the program to crash
answer
(c) Line 14
question
Which method will be executed when the following statements are executed? ClassB item1 new ClassA(); item1.method11(); (a) Line 4 (b) Line 9 (c) Line 14 (d) This is an error and will cause the program to crash
answer
(d) This is an error and will cause the program to crash
question
If a class contains an abstract method, (a) You cannot create an instance of the class (b) The method will have only a header, but not a body, and end with a semicolon (c) The method must be overridden in subclasses (d) All of the above
answer
(d) All of the above
question
True/False An abstract class is not instantiated, but serves as a superclass for other classes.
answer
True
question
Given the following statement which of the following 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 is derived from ClassA (d) ClassA is derived from ClassB
answer
(b) ClassB must override each method in ClassA
question
All fields declared in an interface (a) Are final and static (b) Have protected access (c) Must be initialized in the class implementing the interface (d) Have private access
answer
(a) are final and static
question
Use the following code for Questions 23-25. 1. public interface Interface1 2. { 3. int FIELDA 55; 4. public int methodA(double){} 5. } 6. public class ClassA implements Interface1 7. { 8. FIELDA 60; 9. System.out.println("The end of the class"); 10. } Which line in the interface has an error? (a) 1 (b) 2 (c) 3 (d) 4
answer
(d) 4
question
Which line in ClassA has an error: (a) 6 (b) 7 (c) 8 (d) 9
answer
(c) 8
question
What is missing from ClassA? (a) It does not override methodA (b) It does not have a constructor (c) It does not overload methodA (d) It is a complete class
answer
(a) It does not override methodA
question
When one object is a specialized version of another object, there is a(n) ____ relationship between them. (a) "has a" (b) "is a" (c) Direct (d) "contains a"
answer
(b) "is a"
question
A derived class can directly access (a) All members of the base class (b) Only public and private members of the base class (c) Only protected and private members of the base class (d) Only public and protected members of the base class
answer
(d) only public and protected members of the base class
question
In the following statement, which is the base class? public class ClassA extends ClassB implements ClassC (a) ClassA (b) ClassB (c) ClassC (d) Cannot tell
answer
(b) ClassB
question
In UML diagrams, inheritance is shown (a) With a line that has an open arrowhead at one end that points to the base class (b) With a line that has an open arrowhead at one end that points to the derived class (c) With a line that has a closed arrowhead at one end that points to the base class (d) With a line that has a closed arrowhead at one end that points to the derived class
answer
(a) with a line that has an open arrowhead at one end that points to the base class
question
True/False In an inheritance relationship, the derived class constructor always executes before the base class constructor.
answer
False
question
Use the following code for Questions 6 and 7. public class ClassB extends ClassA { public ClassB() { super(40); System.out.println("This is the last statement in the constructor."); } } What is wrong with the above code? (a) Nothing is wrong with the 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
answer
(a) Nothing is wrong with the code
question
Assuming the above code has been corrected, what will the call to super do? (a) This cannot be determined form the above 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
answer
(c) it will call the constructor of ClassA that receives an integer as an argument
question
If a base class does not have a default constructor, (a) Then a class that is derived from it, must initialize the base class values (b) Then a class that is derived from it, must call one of the constructors that the base class does have (c) Then a class that is derived from it, does not inherit the data member fields from the base class (d) Then a class that is derived from it, must contain the default constructor for the base class
answer
(b) then a class that is derived from it, must call one of the constructors that the base class does have
question
Use the following code for Questions 9 and 10. NOTE: This code is not complete. It consists primarily of method headers. The numbers on the left are for line identification. 1. public class ClassA 2. { 3. public ClassA() {} 4. public method1(int a){} 5. public final method 2(double b){} 6. } 7. public ClassB extends ClassA 8. { 9. public ClassB(){} 10. public method1(int b){} 11. public method2(double c){} 12. } The method in line ____ will override the method in line ____. (a) 4, 10 (b) 5, 11 (c) 10, 4 (d) 11, 5
answer
(c) 10, 4
question
Which line will cause a compiler error? (a) 4 (b) 5 (c) 10 (d) 11
answer
(d) 11
question
True/False If two methods in the same class have the same name but different signatures, the second overrides the first.
answer
False
question
Protected members are (a) Not quite private (b) Not quite public (c) Both (a) and (b) (d) Neither (a) or (b)
answer
(c) Both (a) and (b)
question
Protected class members are denoted in a UML diagram with the symbol (a) * (b) # (c) + (d) -
answer
(b) #
question
The difference between protected access and package access is (a) Protected members may be accessed by methods in the same package or in a derived class, even when the derived class is in a different package (b) Packaged members may be accessed by methods in the same package or in a derived class, even when the derived class is in a different package (c) Protected members cannot be accessed by method in other classes in the same package, but packaged members can be accessed (d) There is no difference in accessibility between protected and packaged members
answer
(a) Protected members may be accessed by methods in the same package or in a derived class, even when the derived class is in a different package
question
In a class hierachy (a) The more general classes are toward the bottom of the tree and the more specialized are toward the top (b) The more general classes are toward the top of the tree and the more specialized are toward the bottom (c) The more general classes are toward the left of the tree and the more specialized are toward the right (d) The more general classes are toward the right of the tree and the more specialized are toward the left
answer
(b) the more general classes are toward the top of the tree and the more specialized are toward the bottom
question
True/False Every class has a toString method and an equal's method inherited from the Object class.
answer
True
question
Use the following code for Questions 17-18. 1. public class ClassA 2. { 3. public ClassA() {} 4. public method1(int a){} 5. } 6. public class ClassB extends ClassA 7. { 8. public ClassB(){} 9. public method1(){} 10. } 11. public class ClassC extends ClassB 12. { 13. public ClassC(){} 14. public method11(){} 15. } Which method will be executed when the following statements are executed? ClassA item1 new ClassB(); item1.method1(); (a) Line 4 (b) Line 9 (c) Line 14 (d) This is an error and will cause the program to crash
answer
(b) Line 9
question
Which method will be executed when the following statements are executed? ClassC item1 new ClassA(); item1.method11(); (a) Line 4 (b) Line 9 (c) Line 14 (d) This is an error and will cause the program to crash
answer
(d) This is an error and will cause the program to crash
question
If a class contains an abstract method, (a) You must create an instance of the class (b) The method will have only a header, but not a body, and end with a semicolon (c) The method cannot be overridden in derived classes (d) All of the above
answer
(b) the method will have only a header, but not a body, and end with a semicolon
question
True/False All methods in an abstract class must also be declared abstract.
answer
False
question
In an interface all methods have (a) Private access (b) Protected access (c) Public access (d) Packaged access
answer
(c) Public access
question
Which of the following statements correctly specifies three interfaces: (a) public class ClassA implements Interface1, Interface2, Interface3 (b) public class ClassA implements [Interface1, Interface2, Interface3] (c) public class ClassA implements (Interface1, Interface2, Interface3) (d) public class ClassA implements Interface1 Interface2 Interface3
answer
(a) public class ClassA implements Interface1, Interface2, Interface3
question
Use the following code for Questions 23-24. 1. public interface Interface1 2. { 3. int FIELDA 55; 4. public int methodA(double); 5. } 6. public class ClassA implements Interface1 7. { 8. FIELDA 60; 9. public int methodA(int a) 10. { 11. return a; 12. } 13. } What is the error in line 8? (a) There is no error (b) Since FIELDA is an interface field, there should not be a semicolon (c) It should be Interface1.FIELDA = 60; (d) Since FIELDA is defined in an interface, it is static and final and cannot be assigned a value
answer
(d) Since FIELDA is defined in an interface, it is static and final and cannot be assigned a value
question
What is the error in line 9? (a) The argument a cannot be returned (b) Since methodA is declared in the interface, it should be defined there as well (c) The signature of the function (d) The statement is correct
answer
(c) the signature of the function
question
True/False When an interface variable references an object, you can use the interface variable to call all the methods in the class implementing the interface.
answer
False