13
© Pearson Education Limited, 2015
Answer: C
Explanation: C) In answers A-D, all of the code works properly by comparing the mouse’s
current location to the box. If the mouse is inside of the box when the event occurs, then inside
is set to true. However, the only event we are interested in is when the mouse button is clicked,
which consists of a pressing and releasing action. If the mouse button is not released, we are not
interested in performing the operation. If the mouse button is pressed, it does not necessarily
mean that the user released the mouse button, so the answer in B is incorrect. The answer in E is
incorrect because we have not checked to see what region the mouse entered or exited.
For the next questions, consider the following class definition:
public class Q26_27
{
private int x;
public Q26_27(int newValue)
{
x = newValue;
}
}
26) Which of the following is true about the class Q26_27?
A) It has no parent class
B) Its parent class is Object
C) Its parent class is Java
D) It can not be extended
E) It has a default child called Object
27) If q1 and q2 are objects of Q26_27, then q1.equals(q2)
A) is a syntax error since equals is not defined in the Q26_27 class
B) is true if q1 and q2 both store the same value of x
C) is true if q1 and q2 reference the same Q26_27 object
D) is never true
E) throws a NullPointerException
28) JApplet is an extension of the Applet class and is found in the ________ library.
A) java.applet
B) java.awt
C) java.lang
D) java.japplet
E) javax.swing
29) A mouse event is generated by
A) moving the mouse
B) clicking the mouse button
C) double clicking the mouse button
D) dragging the mouse
E) all of the above
30) A mouse event is a(n)
A) listener
B) object
C) interface
D) GUI component
E) all of the above
31) In order to implement the MouseListener interface, the programmer must define all of the
following methods except for
A) mouseClicked
B) mouseDragged
C) mouseEntered
D) mouseReleased
E) all of the above must be defined to implement the MouseListener interface
32) Which of these is correct?
A) a base class is a parent class or super class
B) a base class is a child class or derived class
C) a child class is a super class of its parent
D) a parent class is a subclass of its child
E) none of the above
33) Using the reserved word, super, one can
A) access a parent class’constructor(s)
B) access a parent class’methods and instance data
C) access a child class’constructor(s)
D) access a child class’methods and instance data
E) none of the above
34) Why shouldn’t an abstract method be declared final?
A) There’s nothing wrong with doing so
B) Abstract methods cannot be overridden and they must be if a concrete class ever is to be
instantiated
C) So long as the Abstract method never actually is used in by any other method, there’s no
problem with doing this
D) So long as the Abstract method is declared in a Class (not an Interface), there’s nothing wrong
with doing this
E) None of the above
35) What is the advantage of using an Adapter class?
A) It relieves the programmer from having to declare required methods with empty bodies
B) It is more efficient at run time
C) It relieves the programmer from having to shadow the required Interface variables
D) It allows a programmer to be more explicit about exactly which methods are being overridden
and in what manner
E) none of the above
36) If you instantiate an Abstract class, the class or object you wind up with
A) is also an Abstract class
B) is a normal class
C) is an Interface
D) is a reference to an Object
E) can’t exist you cannot instantiate an Abstract class
37) As described in the Software Failure, the cause of the 2004 LA Air Traffic Control incident
was
A) human error
B) the fact that the system had not been rebooted within 50 days
C) the fact that it takes just under 50 days for the countdown timer to count down four billion
milliseconds and the subsystem shuts down when it reaches zero
D) there was no software patch to automatically reset the countdown timer without human
intervention
E) all of the above
9.2 True/False Questions
1) Interface classes cannot be extended but classes that implement interfaces can be extended.
2) Clicking the mouse button generates three mouse events, mousePressed, mouseClicked, and
mouseReleased.
3) A derived class has access to all of the methods of the parent class, but only the protected or
public instance data of the parent class.
4) If class AParentClass has a protected instance data x, and AChildClass is a derived class of
AParentClass, then AChildClass can access x but can not redefine x to be a different type.
5) A is a derived class of X.
6) Y is a derived class of Z.
7) If A, B, Y and Z all contain the same instance data d1, then d1 should be declared in X and
inherited into Y, Z, A and B.
8) If classes C1 and C2 both implement an interface Cint, which has a method whichIsIt, and if
C1 c = new C1( ); is performed at one point of the program, then a later instruction c.whichIsIt(
); will invoke the whichIsIt method defined in C1.
18
© Pearson Education Limited, 2015
For the questions below, assume that Poodle is a derived class of Dog and that Dog d = new
Dog(…) and Poodle p = new Poodle(…) where the … are the necessary parameters for the two
classes.
9) The assignment statement d = p; is legal even though d is not a Poodle.
10) The assignment statement p = d; is legal even though p is not a Dog.
11) The reserved word, extends, is used to denote a has-a relationship.
12) The protected visibility modifier provides the best possible encapsulation that permits
inheritance.
13) You may use the super reserved word to access a parent class’private members.
14) Java doesn’t support multiple inheritance; but it does support the implementation of multiple
Interfaces.
15) Although classes can be inherited from one-another, even Abstract classes, interfaces cannot
be inherited.
16) Regarding the Software Failure: Mid-air collisions were avoided in the 2004 LA Air Traffic
Control incident due to the on-board collision avoidance systems now found on commercial jets.
9.3 Free-Form Questions
1) An applet implements MouseListener, and has int instance data x and y. These variables will
be the (X, Y) coordinates of where the mouse is clicked. Every time the mouse is clicked, draw
a 40×40 Oval centered around x and y. Write the mouseClicked and paint methods to draw the
Oval every time the mouse is clicked.
}
2) An Applet implements MouseMotionListener. Write the mouseMoved and paint methods for
an applet that will display the location of the mouse as an (x, y) coordinate. The output should
be given at location (10, 10) of the applet no matter where the mouse is.
3) An Applet implements MouseMotionListener. Write the mouseMoved and paint methods for
an applet that will display the location of the mouse as an (x, y) coordinate. The output should
be given at the current mouse location.
}
4) An Applet implements MouseMotionListener and is 600×600 in size. Write a mouseMoved
method so that if the mouse is moved into the upper left hand quadrant of the Applet, the entire
applet turns blue, if the mouse is moved into the upper right hand quadrant of the Applet, the
entire applet turns yellow, if the mouse is moved into the lower left hand quadrant of the Applet,
the entire applet turns green, and if the mouse is moved into the lower right hand quadrant of the
Applet, the entire applet turns red.
}
5) If a class extends Applet and also implements MouseListener and MouseMotionListener, what
methods must be declared in this class?
6) Consider a class Plane and three subclasses, Glider, PropPlane and Jet. What instance data
that should be declared in Plane and what instance data should be declared in one of the three
subclasses. Come up with at least 2 instance data unique to each of the four classes given that all
instance data of Plane will be inherited into the subclasses.
7) Assume a class Triangle has been defined. It has three instance data, Point p1, p2, p3. The
class also has a constructor that receives the 3 Points and initializes p1, p2, and p3, a toString
method that outputs the 3 Points, and a computeSurfaceArea method that calculates the surface
area of the Triangle (as an int value). We want to extend this class to represent a 3-dimensional
Pyramid, which consists of 4 Points. Since the Pyramid will consist of in essence, 4 triangles,
computeSurfaceArea for the Pyramid will compute 4 * the surface area of the Triangle made up
of 3 of those 4 Points. Write the Pyramid class to handle the difference(s) between a Pyramid
and the previously defined Triangle. Assume the instance data of Triangle are protected and all
methods are public. Also assume that the class Point has a toString method.
}
}
8) Explain the difference between implementing an interface and a derived class.
9) Explain the difference between using an imported class and extending a class.
10) Write the init and paint methods of an Applet and the methods needed to implement
MouseMotionListener so that the applet can draw a point on the applet for every mouse position
as the mouse is being moved on the screen. Use a 2-D array of ints to represent the X and Y
coordinates of the mouse as it is being moved. For instance, point[0][0] and point[0][1]
represent the X and Y coordinates of the mouse at its first position. The paint method will need
to be called every time the mouse is moved and all points redrawn on the applet.
11) Assume the class Student implements the Speaker interface from the textbook. Recall that
this interface includes two abstract methods, speak( ) and announce(String str). A Student
contains one instance data, String classRank. Write the Student class so that it implements
Speaker as follows. The speak method will output “I am a newbie here” if the Student is a
“Freshman”, “I hate school” if the Student is either a “Sophomore” or a “Junior”, or “I can not
wait to graduate” if the student is a “Senior”. The announce method will output “I am a Student,
here is what I have to say” followed by the String parameter on a separate line. Finally, the
classRank is initialized in the constructor. Only implement the constructor and the methods to
implement the Speaker interface.
12) What instance data and methods might you define for SportsCar that are not part of Car?
13) What methods inherited from Car should SportsCar override?
14) A motorcycle inherits properties from both a bicycle and a car. Java does not permit
multiple inheritance. Assume that Bicycle and Car have both been declared. Explain how you
might go about implementing a Motorcycle as a derived class.
15) Consider a class Name, which has four instance data (all Strings): first, middle, last and title.
Even though Name inherits the equal method from Object, it would make more sense to override
it. Why?
16) Why is it a contradiction for an abstract method to be modified as final or static?
17) In what manners are Timer objects similar to and different from other GUI components?
18) What is the advantage of extending an adapter class rather than implementing a listener
interface?