Java Chapter 9 Written Review
True
When a subclass extends a superclass, the public members of the superclass become public members of the subclass.
final
When a method is declared with the ____________ modifier, it cannot be overridden in a subclass.
All of the above.
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.
False
In an inheritance relationship, the subclass constructor always executes before the superclass constructor.
ClassA
In the following statement, which is the subclass? public class ClassA extends ClassB implements ClassC
ClassB
In the following statement, which is the superclass? public class ClassA extends ClassB implements ClassC
c. class hierarchy
Like a family tree, a ____________ shows the inheritance relationship between classes. a. flowchart b. class map c. class hierarchy d. binary tree
c. Both A and B
Protected members are: a. not quite private b. not quite public c. Both A and B d. Neither A or B
Java will automatically call the superclass's default or no-arg constructor just before the code in the subclass's constructor executes.
If a subclass constructor does not explicitly call a superclass constructor:
then a class that inherits from it, must call one of the constructors that the superclass does have.
If a superclass does not have a default constructor or a no-arg constructor:
overloaded
If two methods have the same name but different signatures, they are:
False
If two methods in the same class have the same name but different signatures, the second overrides the first.
package
If you do not provide an access specifier for a class member, the class member is given ___________ by default.
With a line that has an open arrowhead at one end that points to the superclass
In UML diagrams, inheritance is shown:
the more general classes are toward the top of the tree and the more specialized are toward the bottom.
In a class hierarchy:
The superclass constructor always executes before the subclass constructor
In an inheritance relationship:
public access
In an interface all methods have:
It will call the constructor of ClassA that receives an integer as an argument.
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."); } }
The call to the method super must be the first statement in the constructor.
What is wrong with the following code? public class ClassB extends ClassA { public ClassB() { int init = 10; super(40); } }
Nothing is wrong with the code
What is wrong with the following code? public class ClassB extends ClassA { public ClassB() { super(40); System.out.println("This is the last statement " + "in the constructor."); } }
all the characteristics of the general object, plus additional characteristics.
When an "is a" relationship exists between objects, it means that the specialized object has:
False
When an interface variable references an object, you can use the interface variable to call any and all of the methods in the class implementing the interface.
private members
When declaring class data members, it is best to declare them as:
ClassC
In the following statement, which is the interface? public class ClassA extends ClassB implements ClassC
False
Inheritance involves a subclass, which is the general class, and a superclass, which is the specialized class.
10, 4
Look at the following code. The method in line ________ will override the method in line __________. Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public int method1(int a){} Line 5 public int method2(int 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 }
4
Look at 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 }
Protected members may be accessed by methods in the same package or in a subclass, even when the subclass is in a different package.
Which of the following is true about protected access?
Line 9
Look at 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();
Both methods may be called with a subclass object
When a subclass overloads a superclass method:
Method overriding
Replacing inadequate superclass methods with more suitable subclass methods is known as what?
are final and static
All fields declared in an interface:
False
All methods in an abstract class must also be declared abstract.
True
Every class has a toString method and an equals method inherited from the Object class.
True
Every class is either directly or indirectly derived from the Object class.
True
It is not possible for a superclass to call a subclass's method.
Line 14
Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public void method1(){} 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 as a result of the following statements? ClassA item1 = new ClassC(); item1.method1();
d. This is an error and will cause the program to crash.
Look at 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();
super
What key word can you use to call a superclass constructor explicitly?
only public and protected members of the superclass
A subclass can directly access:
prefixing its name with the super key word and a dot (.)
A subclass may call an overridden superclass method by:
It will call the constructor of ClassA that receives an integer as an argument.
Look at the following code and determine what the call to super will do. public class ClassB extends ClassA { public ClassB() { super(10); } }
must be the first statement in the subclass's constructor
The super statement that calls the superclass constructor:
True
An abstract class is not instantiated, but serves as a superclass for other classes.
10
Look at the following code. Which line will cause a compiler error? Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public final int method1(int a){} Line 5 public double method2(int 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 double method2(double c){} Line 12 }
a. public class ClassA implements Interface1, Interface2, Interface3
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
d. None of the above
Look at the following code. The method in line _______ will override the method in line _______. Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public int method1(int a){} Line 5 public double method2(int b){} Line 6 } Line 7 public ClassB extends ClassA Line 8 { Line 9 public ClassB(){} Line 10 public int method1(int b, int c){} Line 11 public double method2(double c){} Line 12 }
True
Private members of the superclass cannot be accessed by the subclass.
public class Salaried extends PayType
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
#
Protected class members are denoted in a UML diagram with the symbol
public members in ClassB are public in ClassA, but private members in ClassB cannot be directly accessed in ClassA
If ClassA extends ClassB, then: a. public and private members of ClassB are public and private, respectively, in ClassA b. public members in ClassB are public in ClassA, but private members in ClassB cannot be directly accessed in ClassA c. neither public or private members in ClassB can be directly accessed in ClassA d. private members in ClassB are changed to protected members in ClassA
a chain of inheritance.
If ClassC extends ClassB, which extends ClassA, this would be an example of:
b. ClassB must override each method in ClassA
Given the following code 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 inherits from ClassA d. ClassA inherits from ClassB
the method will have only a header, but not a body, and end with a semicolon
If a class contains an abstract method:
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
False
If a method in a subclass has the same signature as a method in the superclass, the subclass method overloads the superclass method.
This is an error and will cause the program to crash
Look at 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 as a result of the following statements? ClassB item1 = new ClassA(); item1.method1();
11
Look at 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 }
It does not override methodA.
Look at 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 }
8
Look at 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 }
"is a"
When one object is a specialized version of another object, there is this type of relationship between them.