Practice Test 1
Which of the following statements is false?
a. A subclass is generally larger than its superclass.
b. A superclass object is a subclass object.
c. The class following the extends keyword in a class declaration is the direct superclass of the
class being declared.
d. Java uses interfaces to provide the benefits of multiple inheritance.
Inheritance is also known as the
a. ¡°knows-a¡± relationship.
b. ¡°has-a¡± relationship.
c. ¡°uses-a¡± relationship.
d. ¡°is-a¡± relationship.
An advantage of inheritance is that:
a. All methods can be inherited.
b. All instance variables can be uniformly accessed by subclasses and superclasses.
c. Objects of a subclass can be treated like objects of their superclass.
d. None of the above.
Which of the following keywords allows a subclass to access a superclass method even when the subclass has overridden the superclass method?
a. base.
b. this.
c. public.
d. super.
Using the protected keyword gives a member:
a. public access.
b. package access.
c. private access.
d. block scope.
Superclass methods with this level of access cannot be called from subclasses.
a. private.
b. public.
c. protected.
d. package.
Every class in Java, except ________, extends an existing class.
a. Integer.
b. Object.
c. String.
d. Class.
Overriding a method differs from overloading a method because:
a. Overloaded methods have the same signature.
b. Overridden methods have the same signature.
c. Both of the above.
d. Neither of the above.
To avoid duplicating code (and possibly errors), use ________, rather than ________.
a. inheritance, the ¡°copy-and-past¡± approach.
b. the ¡°copy-and-past¡± approach, inheritance.
c. a class that explicitly extends Object, a class that does not extend Object.
d. a class that does not extend Object, a class that explicitly extends Object.
Consider the classes below, declared in the same file:
class A {
int a;
public A()
{
a = 7; }
}
class B extends A
{
int b;
public B()
{
b = 8; }
}
Which of the statements below is false?
a. Both variables a and b are instance variables.
b. After the constructor for class B executes, the variable a will have the value 7.
c. After the constructor for class B executes, the variable b will have the value 8.
d. A reference of type A can be treated as a reference of type B.
Which of the following is the superclass constructor call syntax?
a. keyword super, followed by a dot (.) .
b. keyword super, followed by a set of parentheses containing the superclass constructor
arguments.
c. keyword super, followed by a dot and the superclass constructor name. d. None of the above.
Which superclass members are inherited by all subclasses of that superclass?
a. private instance variables and methods.
b. protected instance variables and methods.
c. private constructors.
d. protected constructors.
Which statement is true when a superclass has protected instance variables?
a. A subclass object can assign an invalid value to the superclass¡¯s instance variables, thus leaving an object in an inconsistent state.
b. Subclass methods are more likely to be written so that they depend on the superclass¡¯s data implementation.
c. We may need to modify all the subclasses of the superclass if the superclass implementation changes.
d. All of the above.
private fields of a superclass can be accessed in a subclass
a. by calling private methods declared in the superclass.
b. by calling public or protected methods declared in the superclass.
c. directly.
d. All of the above.
Failure to prefix the superclass method name with the keyword super and a dot (.) separator when referencing the superclass¡¯s method causes a(n) ________.
a. compile-time error.
b. syntax error.
c. infinite recursion.
d. runtime error.
When a subclass constructor calls its superclass constructor, what happens if the superclass¡¯s constructor does not assign a value to an instance variable?
a. A syntax error occurs.
b. A compile-time error occurs.
c. A run-time error occurs.
d. The program compiles and runs because the instance variables are initialized to their default
values.
Which of the following statements are true?
A. We can use inheritance to customize existing software.
B. A superclass specifies commonality.
C. A superclass can be modified without modifying subclasses
D. A subclass can be modified without modifying its superclass.
a. All of the above.
b. None of the above.
c. A,BandC.
d. A,BandD.
Which of the following is an example of a functionality that should not be ¡°factored out¡± to a superclass?
a. Both ducks and geese are birds that know how to start flying from the water.
b. All vehicles know how to start and stop.
c. All animals lay eggs, except for mammals.
d. All paints have a color.
The default equals implementation determines:
a. whether two references refer to the same object in memory.
b. whether two references have the same type.
c. whether two objects have the same instance variables.
d. whether two objects have the same instance variable values.
Solutions
ANS: b. A superclass object is a subclass object.
ANS: d. ¡°is-a¡± relationship
ANS: c. Objects of a subclass can be treated like objects of their superclass.
ANS: d. super.
ANS: b. package access.
ANS: a. private.
ANS: b. Object.
ANS: b. Overridden methods have the same signature.
ANS: a. inheritance, the ¡°copy-and-past¡± approach.
ANS: d. A reference of type A can be treated as a reference of type B. The converse is true because class A is the superclass.
ANS: b. keyword super, followed by a set of parentheses containing the superclass constructor arguments.
ANS: b.
ANS: d.
ANS: b.
ANS: c.
ANS: d. values.
ANS: a. ANS: c. ANS: a.
protected instance variables and methods.
All of the above.
by calling public or protected methods declared in the superclass.
infinite recursion.
The program compiles and runs because the instance variables are initialized to their default
All of the above.
All animals lay eggs except for mammals.
whether two references refer to the same object in memory