Java Software Solutions, 8e, Global Edition (Lewis/Loftus)
Chapter 9 Inheritance
9.1 Multiple-Choice Questions
For the questions below, use the following partial class definitions:
public class A1
{
public int x;
private int y;
protected int z;
…
}
public class A2 extends A1
{
protected int a;
private int b;
…
}
public class A3 extends A2
{
private int q;
…
}
1) Which of the following is true with respect to A1, A2 and A3?
A) A1 is a subclass of A2 and A2 is a subclass of A3
B) A3 is a subclass of A2 and A2 is a subclass of A1
C) A1 and A2 are both subclasses of A3
D) A2 and A3 are both subclasses of A1
E) A1, A2 and A3 are all subclasses of the class A
2) Which of the following lists of instance data are accessible in class A2?
A) x, y, z, a, b
B) x, y, z, a
C) x, z, a, b
D) z, a, b
E) a, b
3) Which of the following lists of instance data are accessible in A3?
A) x, y, z, a, b, q
B) a, b, q
C) a, q
D) x, z, a, q
E) x, a, q
4) Which of the following is true regarding the use of instance data y of class A1?
A) It is accessible in A1, A2 and A3
B) It is accessible in A1 and A2
C) It is accessible only in A1
D) It is accessible only in A3
E) It is not accessible to any of the three classes
5) The instruction super( ); does which of the following?
A) calls the method super as defined in the current class
B) calls the method super as defined in the current class’parent class
C) calls the method super as defined in java.lang
D) calls the constructor as defined in the current class
E) calls the constructor as defined in the current class’parent class
6) Inheritance through an extended (derived) class supports which of the following concepts?
A) interfaces
B) modulary
C) information hiding
D) code reuse
E) correctness
7) Aside from permitting inheritance, the visibility modifier protected is also used to
A) permit access to the protected item by any class defined in the same package
B) permit access to the protected item by any static class
C) permit access to the protected item by any parent class
D) ensure that the class can not throw a NullPointerException
E) define abstract elements of an interface
8) Which of the following is an example of multiple inheritance?
A) A computer can be a mainframe or a PC
B) A PC can be a desktop or a laptop
C) A laptop is both a PC and a portable device
D) A portable device is a lightweight device
E) Macintosh and IBM PC are both types of PCs
9) Java does not support multiple inheritance, but some of the abilities of multiple inheritance are
available by
A) importing classes
B) implementing interfaces
C) overriding parent class methods
D) creating aliases
E) using public rather than protected or private modifiers
10) If a programmer writes a class wanting it to be extended by another programmer, then this
programmer must
A) change private methods and instance data to be protected
B) change public methods and instance data to be protected
C) change all methods to be protected
D) change the class to be protected
E) none of the above, the programmer does not have to change anything
11) All classes in Java are directly or indirectly subclasses of the ________ class.
A) Wrapper
B) String
C) Reference
D) this
E) Object
12) Which of the following is not a method of the Object class?
A) clone
B) compareTo
C) equals
D) toString
E) all of the above are methods of the Object class
13) Abstract methods are used when defining
A) interface classes
B) derived classes
C) classes that have no constructor
D) arrays
E) classes that have no methods
6
© Pearson Education Limited, 2015
For the questions below, consider the following class definition:
public class AClass
{
protected int x;
protected int y;
public AClass(int a, int b)
{
x = a;
y = b;
}
public int addEm( )
{
return x + y;
}
public void changeEm( )
{
x++;
y—;
}
public String toString( )
{
return “” + x + ” ” + y;
}
}
14) Consider that you want to extend AClass to BClass. BClass will have a third int instance
data, z. Which of the following would best define BClass’constructor?
A) public BClass(int a, int b, int c)
{
super(a, b, c);
}
B) public BClass(int a, int b, int c)
{
x = a;
y = b;
z = c;
}
C) public BClass(int a, int b, int c)
{
z = c;
}
D) public BClass(int a, int b, int c)
{
super(a, b);
z = c;
}
E) public BClass(int a, int b, int c)
{
super( );
}
15) You want addEm to now add all three values and return the sum and changeEm to change x
and y, but leave z alone. Which should you do?
A) Redefine addEm and changeEm without referencing super.addEm( ) or super.changeEm( )
B) Redefine addEm to return the value of z + super.addEm( ), but leave changeEm alone
C) Redefine changeEm to call super.changeEm( ) and then set z = x + y, but leave addEm alone
D) Redefine addEm to return the value of z + super.addEm( ) and redefine changeEm to call
super.changeEm( ) and then set z = x + y
E) Redefine changeEm to call super.changeEm( ) without doing anything to z, and redefine
addEm to return super.addEm( )
16) Which of the following would best redefine the toString method for BClass?
A) public String toString(int z)
{
return ” ” + x + ” ” + y + ” ” + z;
}
B) public String toString( )
{
return super.toString( );
}
C) public String toString( )
{
return super.toString( ) + ” ” + z;
}
D) public String toString( )
{
return super.toString( ) + ” ” x + ” ” + y + ” ” + z;
}
E) public String toString( )
{
return ” ” + x + ” + y + ” ” + z;
}
17) Which of the following is true regarding Java classes?
A) All classes must have 1 parent but may have any number of children (derived or extended)
classes
B) All classes must have 1 child (derived or extended) class but may have any number of parent
classes
C) All classes must have 1 parent class and may have a single child (derived or extended) class
D) All classes can have any number (0 or more) of parent classes and any number of children
(derived or extended) classes
E) All classes can have either 0 or 1 parent class and any number of children (derived or
extended) classes
18) Two children of the same parent class are known as
A) aliases
B) relatives
C) clones
D) brothers
E) siblings
19) A variable declared to be of one class can later reference an extended class of that class.
This variable is known as
A) protected
B) derivable
C) cloneable
D) polymorphic
E) none of the above, a variable declared to be of one class can never reference any other type of
class, even an extended class
20) In order to determine the type that a polymorphic variable refers to, the decision is made
A) by the programmer at the time the program is written
B) by the compiler at compile time
C) by the operating system when the program is loaded into memory
D) by the Java run-time environment at run time
E) by the user at run time
21) The reference to getMoney( ) in assignment 1 is to the class
A) Person
B) Student
C) Employee
D) Retired
E) none of the above, this cannot be determined by examining the code
22) The reference to getMoney( ) in assignment 2 is to the class
A) Person
B) Student
C) Employee
D) Retired
E) none of the above, this cannot be determined by examining the code
23) The reference to getMoney( ) in assignment 3 is to the class
A) Person
B) Student
C) Employee
D) Retired
E) none of the above, this cannot be determined by examining the code
Answer: E
24) The relationship between a parent class and a child class is referred to as a(n) ________
relationship.
A) has-a
B) is-a
C) was–a
D) instance-of
E) alias
12
25) Consider an applet that implements MouseListener. Assume that the applet has five instance
data, int x1, x2, y1, y2, and boolean inside. The four int values represent the two end-points of a
box (x1, y1 is the upper left hand point and x2, y2 is the lower right hand point). Which of the
following properly defines code that will determine whenever the mouse button is clicked if the
mouse is currently inside this box or not. If the mouse is inside the box, inside is set to true,
otherwise it is set to false.
A) public void mouseMoved(MouseEvent me)
{
if (me.getX( ) >= x1 && me.getX( ) <= x2 && me.getY( ) >= y1 && me.getY( ) <= y2)
inside = true;
else
inside = false;
}
B) public void mousePressed(MouseEvent me)
{
if (me.getX( ) >= x1 && me.getX( ) <= x2 && me.getY( ) >= y1 && me.getY( ) <= y2)
inside = true;
else
inside = false;
}
C) public void mouseReleased(MouseEvent me)
{
if (me.getX( ) >= x1 && me.getX( ) <= x2 && me.getY( ) >= y1 && me.getY( ) <= y2)
inside = true;
else
inside = false;
}
D) public void mouseEntered(MouseEvent me)
{
if (me.getX( ) >= x1 && me.getX( ) <= x2 && me.getY( ) >= y1 && me.getY( ) <= y2)
inside = true;
else
inside = false;
}
E) Requires both of the following methods:
public void mouseEntered(MouseEvent me)
{
inside = true;
}
public void mouseExited(MouseEvent me)
{
inside = false;
}