Exam 9: Inheritance
Exam 1: Introduction65 Questions
Exam 2: Data and Expressions77 Questions
Exam 3: Using Classes and Objects58 Questions
Exam 4: Writing Classes56 Questions
Exam 5: Conditionals and Loops37 Questions
Exam 6: More Conditionals and Loops36 Questions
Exam 7: Object-Oriented Design76 Questions
Exam 8: Arrays70 Questions
Exam 9: Inheritance71 Questions
Exam 10: Polymorphism70 Questions
Exam 11: Exceptions68 Questions
Exam 12: Recursion68 Questions
Exam 13: Collections68 Questions
Select questions type
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.
Free
(Essay)
4.8/5
(39)
Correct Answer:
public class MouseFollower extends Applet implements MouseMotionListener
{
private int[ ][ ] points;
private int numPoints;
public void init( )
{
numPoints = 0;
points = new int[1000][2];
setBackground(Color.blue);
addMouseMotionListener(this);
}
public void paint(Graphics page)
{
page.setColor(Color.red);
for(int j = 0; j < numPoints; j++)
page.drawOval(points[j][0], points[j][1], 1, 1);
}
public void mouseMoved(MouseEvent me)
{
if (numPoints < 1000)
{
points[numPoints][0] = me.getX();
points[numPoints][1] = me.getY();
numPoints++;
}
repaint( );
}
public void mouseDragged(MouseEvent me) { }
}
For the next questions, consider the following class definition:
public class Q26_27
{
private int x;
public Q26_27(int newValue)
{
x = newValue;
}
}
-If q1 and q2 are objects of Q26_27, then q1.equals(q2)
Free
(Multiple Choice)
4.8/5
(42)
Correct Answer:
C
For the questions below, assume that Student, Employee and Retired are all extended classes of Person, and all four classes have different implementations of the method getMoney. Consider the following code where ... are the required parameters for the constructors:
Person p = new Person(...);
int m1 = p.getMoney( ); // assignment 1
p = new Student(...);
int m2 = p.getMoney( ); // assignment 2
if (m2 < 100000) p = new Employee(...);
else if (m1 > 50000) p = new Retired(...);
int m3 = p.getMoney( ); // assignment 3
-The reference to getMoney( ) in assignment 1 is to the class
Free
(Multiple Choice)
4.8/5
(37)
Correct Answer:
A
An Applet implements MouseMotionListener and is 600x600 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.
(Essay)
4.7/5
(38)
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;
}
}
-In order to determine the type that a polymorphic variable refers to, the decision is made
(Multiple Choice)
4.9/5
(32)
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.
(True/False)
4.9/5
(32)
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.
(Essay)
4.9/5
(36)
Although classes can be inherited from one-another, even Abstract classes, interfaces cannot be inherited.
(True/False)
4.9/5
(37)
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.
(Essay)
4.9/5
(37)
For the next questions, consider the following class definition:
public class Q26_27
{
private int x;
public Q26_27(int newValue)
{
x = newValue;
}
}
-Which of the following is True about the class Q26_27?
(Multiple Choice)
4.9/5
(38)
Why is it a contradiction for an abstract method to be modified as final or static?
(Essay)
4.7/5
(30)
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;
...
}
-Which of the following lists of instance data are accessible in class A2?
(Multiple Choice)
4.8/5
(32)
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.
-The assignment statement p = d; is legal even though p is not a Dog.
(True/False)
4.8/5
(41)
Aside from permitting inheritance, the visibility modifier protected is also used to
(Multiple Choice)
4.7/5
(34)
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.
-The assignment statement d = p; is legal even though d is not a Poodle.
(True/False)
4.7/5
(40)
For the questions below, assume that Student, Employee and Retired are all extended classes of Person, and all four classes have different implementations of the method getMoney. Consider the following code where ... are the required parameters for the constructors:
Person p = new Person(...);
int m1 = p.getMoney( ); // assignment 1
p = new Student(...);
int m2 = p.getMoney( ); // assignment 2
if (m2 < 100000) p = new Employee(...);
else if (m1 > 50000) p = new Retired(...);
int m3 = p.getMoney( ); // assignment 3
-The reference to getMoney( ) in assignment 3 is to the class
(Multiple Choice)
4.8/5
(35)
What instance data and methods might you define for SportsCar that are not part of Car?
(Essay)
4.9/5
(38)
Showing 1 - 20 of 71
Filters
- Essay(0)
- Multiple Choice(0)
- Short Answer(0)
- True False(0)
- Matching(0)