ITP 120 Chapter 10
What type of relationship exists between two objects when one object is a specialized version of another object?
"is a"
A functional interface is simply an interface that has one abstract method.
TRUE
An abstract class is not instantiated itself but serves as a superclass for other classes.
TRUE
Because every class directly or indirectly inherits from the Object class, every class inherits the Object class's members.
TRUE
If a method in a subclass has the same signature as a method in the superclass, the subclass method overrides the superclass method.
TRUE
It is not possible for a superclass to call a subclass's method.
TRUE
When a subclass extends a superclass, the public members of the superclass become public members of the subclass.
TRUE
When a subclass overrides a superclass method, only the subclass's version of the method can be called with a subclass object.
TRUE
If ClassC is derived from ClassB which is derived from ClassA, this would be an example of
a chain of inheritance
Which of the following is an example of a lambda expression?
int x = x * factor;
__________ is a special type of expression used to create an object that implements a functional interface.
lambda
Replacing inadequate superclass methods with more suitable subclass methods is known as __________.
method overriding
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();
method1 on Line 9
In a class hierarchy __________.
the more general classes are toward the top of the tree and the more specialized classes are toward the bottom
In an inheritance relationship __________.
the superclass constructor always executes before the subclass constructor
The __________ key word is used to call a superclass constructor explicitly.
super
In __________, inheritance is shown with a line that has an open arrowhead at one end that points to the superclass.
a UML diagram
Which of the following is true about protected access?
Protected members may be accessed by methods in the same package or in a subclass, even when the subclass is in a different package
If a class contains an abstract method __________.
All of these are true. -you must create an instance of the class -the method will only have a header, but not a body, and will end with a semicolon -the method cannot be overridden in subclasses
Which of the following statements declares Salaried as a subclass of PayType?
public class Salaried extends PayType
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."); } }
It will call the constructor of ClassA that receives an integer as an argument.
What is wrong with the following code? public class ClassB extends ClassA { public ClassB() { int init = 10; super(40); } }
The call to the method super must be the first statement in the constructor.
A compiler error will result if an anonymous inner class tries to use a variable that is not final, or not effectively final.
TRUE
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.
TRUE
Every class has a toString method and an equals method inherited from the Object class.
TRUE
You can write a super statement that calls a superclass constructor but only in the subclass's constructor.
TRUE
In the following statement, which is the subclass? public class ClassA extends ClassB implements ClassC
ClassA
__________ tells the Java compiler that a method is meant to override a method in the superclass
@Override
All methods in an abstract class must also be declared abstract.
FALSE
If two methods in the same class have the same name but different signatures, the second overrides the first.
FALSE
In an inheritance relationship, the subclass constructor always executes before the superclass constructor.
FALSE
Inheritance involves a subclass, which is the general class, and a superclass, which is the specialized class.
FALSE
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 }
It does not override methodA.
If a subclass constructor does not explicitly call a superclass constructor __________.
Java will automatically call the superclass's default or no-arg constructor just before the code in the subclass's constructor executes
A(n) __________ method is a method that appears in a superclass but expects to be overridden in a subclass.
abstract
When an "is a" relationship exists between objects, the specialized object has __________.
all of the characteristics of the general object plus additional characteristics
All fields declared in an interface __________.
are treated as final and static
When a subclass overloads a superclass method __________.
both methods may be called with a subclass object
Which of the following shows the inheritance relationships among classes in a manner similar to that of a family tree?
class hierarchy
Which key word indicates that a class inherits from another class?
extends
subclass can directly access __________.
only public and protected members of the superclass
If two methods have the same name but different signatures they are __________.
overloaded
If you don't provide an access specifier for a class member, the class member is given __________ access by default.
package
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.
polymorphic
A subclass may call an overridden superclass method by __________.
prefixing its name with the super key word and a dot (.)
When declaring class data members it is best to declare them as __________.
private members
A __________ member's access is somewhere between public and private.
protected
All methods specified by an interface are __________.
public
Protected class members can be denoted in a UML diagram with the __________ symbol.
#
A protected member of a class may be directly accessed by __________.
Any of these -methods of the same class -methods in the same package -methods of a subclass
In the following statement, which is the superclass? public class ClassA extends ClassB implements ClassC
ClassB
Given the following code, which statement is true? public class ClassB implements ClassA{ }
ClassB must override each method in ClassA.
In the following statement, which is the interface? public class ClassA extends ClassB implements ClassC
ClassC
What is required for an interface method that has a body?
The method header must begin with the key word default.
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();
This is an error and will cause the program to crash.
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 }
Line 4
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 }
Line 8
When a method is declared with the __________ modifier, it cannot be overridden in a subclass
final
Which of the following is the operator used to determine whether an object is an instance of a particular class?
instanceOf
If a method in a subclass has the same signature as a method in the superclass, the subclass method __________ the superclass method.
overrides
Which of the following statements correctly specifies two interfaces?
public class ClassA implements Interface1, Interface2
A class becomes abstract when you place the __________ key word in the class definition.
abstract
If a subclass constructor does not explicitly call a superclass constructor, __________.
Java will automatically call the superclass's default or no-arg constructor just before the code in the subclass's constructor executes
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 }
Line 11