Ch 11 - Inheritance

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

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

(c) 10, 4

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

(c) Line 14

In an interface all methods have (a) Private access (b) Protected access (c) Public access (d) Packaged access

(c) Public access

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

(c) The call to the method super must be the first statement in the constructor

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

(c) it will call the constructor of ClassA that receives an integer as an argument

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

(c) the signature of the function

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

(d) 4

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

(d) All of the above

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

(d) All of the above

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

(d) All the characteristics of the general object, plus additional characteristics

True/False If two methods in the same class have the same name but different signatures, the second overrides the first.

False

True/False Every class has a toString method and an equal's method inherited from the Object class.

True

True/False Every class is either directly or indirectly derived from the Object class.

True

True/False It is not possible for a superclass to call a subclass's method.

True

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

(a) It does not override methodA

True/False All methods in an abstract class must also be declared abstract.

False

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.

False

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

(b) the method will have only a header, but not a body, and end with a semicolon

Protected members are (a) Not quite private (b) Not quite public (c) Both (a) and (b) (d) Neither (a) or (b)

(c) Both (a) and (b)

Protected class members are denoted in a UML diagram with the symbol (a) * (b) # (c) + (d) -

(b) #

Which line will cause a compiler error? (a) 4 (b) 5 (c) 10 (d) 11

(d) 11

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

(d) This is an error and will cause the program to crash

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

(c) 10

True/False An abstract class is not instantiated, but serves as a superclass for other classes.

True

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.

False

True/False In an inheritance relationship, the derived class constructor always executes before the base class constructor.

False

Which line in ClassA has an error: (a) 6 (b) 7 (c) 8 (d) 9

(c) 8

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

(b) a chain of inheritance

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

(b) the more general classes are toward the top of the tree and the more specialized are toward the bottom

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

(b) then a class that is derived from it, must call one of the constructors that the base class does have

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

(a) Nothing is wrong with the code

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

(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

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

(a) are final and static

When declaring class data members, it is best to declare them as (a) Private members (b) Public members (c) Protected members (d) Restricted members

(a) private members

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

(a) public class ClassA implements Interface1, Interface2, Interface3

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

(a) public class Salaried extends PayType

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

(a) with a line that has an open arrowhead at one end that points to the base class

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

(a) with a line that has an open arrowhead at one end that points to the superclass

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"

(b) "is a"

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

(b) ClassB

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

(b) ClassB must override each method in ClassA

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

(b) It will call the constructor of ClassA that receives an integer as an argument

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

(b) Line 9

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

(b) Public members in ClassB are public in ClassA, but private members in ClassB cannot be directly accessed in ClassA

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

(d) Java will automatically call the superclass's default constructor just before the code in the subclass's constructor executes

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

(d) None of the above

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

(d) Package

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

(d) Since FIELDA is defined in an interface, it is static and final and cannot be assigned a value

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

(d) This is an error and will cause the program to crash

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

(d) only public and protected members of the base class


Ensembles d'études connexes

Occlusion Midterm Sample Questions

View Set

Paramedic - Anatomy and Physiology Prep Quiz

View Set

Week 7: Managing patient Care, teamwork and collaboration

View Set

Lab 15-3: Using a Restore Point to Repair Startup

View Set