Java Chapter 9 Written Review

25 July 2022
4.7 (114 reviews)
61 test answers

Unlock all answers in this set

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