Homework Inheritance

advertisement
1052
HOMEWORK1 – INHERITANCE, INTERFACES
2. Inheritance and Methods
1. Create a constructor for an alarm clock class which is a child class of Clock. It initializes
its parent class instance variables – second and minute – and initializes it own instance
variable – a boolean – AlarmOn.
2. Write a print method for the alarm clock class which makes use of its parent class print
method.
3. Consider this code that creates some Location objects with coordinates x=10 and y=20:
Location a, b, c;
a = new Location(10,20);
b = new Location(10,20);
c = b;
a == b
a.equals(b)
a==c
a.equals(c)
b==c
b.equals(c)
___________________
_____________________
_____________________
_____________________
_____________________
______________________
After this code executes, what are the values of these boolean expressions?
4.Suppose that the Foo class does not have an equals method. What happens when an
expression x.equals(y); is evaluated for two Foo objects?
o
o
o
o
A. The expression is true if x and y refer to the same object
B. The expression is true if x and y refer to objects with the same values
C. Compiler error
D. Run-time error
5. Suppose that the Foo class has a typical equals method. What happens when an
expression x.equals(y); is evaluated for two Foo objects?
o
o
o
o
o
A. The expression is true if x and y refer to the same object
B. The expression is true if x and y refer to objects with the same values
C. Compiler error
D. Run-time error
6. 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
7. Inheritance through an extended class supports which of the following concepts?
a) interfaces
b) modularity
c) information hiding
d) code reuse
e) correctness
1
1052
HOMEWORK1 – INHERITANCE, INTERFACES
8. 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
9. 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
10. 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
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
14. 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.
2
1052
HOMEWORK1 – INHERITANCE, INTERFACES
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.
15. Two children of the same parent class are known as
a) aliases
b) relatives
c) clones
d) brothers
e) siblings
16. A variable declared to be of one class can later reference an extended class of that class(
become an alias of a child 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
17. In
made
a)
b)
c)
d)
e)
order to determine the type that a polymorphic variable refers to, the decision is
by the programmer at the time the program is written
by the compiler at compile time
by the operating system when the program is loaded into memory
by the Java run-time environment at run time
by the user at run time
For the following questions, 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
18. 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
19. The reference to getMoney( ) in assignment 2 is to the class
a) Person
b) Student
3
1052
HOMEWORK1 – INHERITANCE, INTERFACES
c) Employee
d) Retired
e) none of the above, this cannot be determined by examining the code
20) 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
21) 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
22. Assume that you are defining a class and you want to implement an ActionListener.
You state addActionListener(this); in your class’ constructor. What does this mean?
a) The class must import another class which implements ActionListener
b) The class must define the method actionPerformed
c) The class must define the method ActionListener
d) The class must define an inner class called ActionListener
e) The class must define the method actionPerformed in an inner class named
ActionListener
23. A listener is an object that
a) implements any type of interface
b) is used to accept any form of input
c) is an inner class to a class that has abstract methods
d) waits for some action from the user
e) uses the InputStreamReader class
/*****************************************************************************
True/False Questions
1.____Interface classes cannot be extended but classes that implement interfaces can be
extended.
2.____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.
3.___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.
Consider the following class hierarchy and answer questions 4-6
4
1052
HOMEWORK1 – INHERITANCE, INTERFACES
X
Y
A
Z
B
4._____A is a derived class of X.
5.____Y is a derived class of Z.
6.___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.
7. Which of these function calls will cause an exception to be thrown when x is 42. (x is an
int variable).
a.
if (0 < x)
throw new IllegalArgumentException("Bad x");
B. if (0 == x)
throw new IllegalArgumentException("Bad x");
a.
if (0 > x)
throw new IllegalArgumentException("Bad x");
c. None of the above will cause an exception when x is 42.
8. __Will the following compile – is it an narrowing or a widening conversion.
String s = “Objection”;
Object obj;
Obj = s;
s = obj; _______________________________
9. In order to implement Comparable in a class, what method(s) must be defined in that
class?
1. equals
2. compares
3. both lessThan and greaterThan
4. compareTo
5. both compares and equals
/************************************************************************
5
1052
HOMEWORK1 – INHERITANCE, INTERFACES
For questions 10, consider a class called ChessPiece. This class has two instance data,
String type and int player. The variable type will store “King”, “Queen”, “Bishop”, etc and
the int player will store 0 or 1 depending on whose piece it is. We wish to implement
Comparable for the ChessPiece class. Assume that, the current ChessPiece is compared to a
ChessPiece passed as a parameter. Pieces are ordered as follows: “Pawn” is a lesser piece
to a “Knight” and a “Bishop”, “Knight” and “Bishop” are equivalent for this example, both
are lesser pieces to a “Rook” which is a lesser piece to a “Queen” which is a lesser piece to a
“King.”
10. Which of the following pieces of logic could be used in the method that implements
Comparable? Assume that the method is passed Object a, which is really a ChessPiece.
Also assume that ChessPiece has a method called returnType which returns the type of the
given piece. Only one of these answers has correct logic.
a. if (this.type < a.returnType( )) return –1;
b. if (this.type = = a.returnType( )) return 0;
c. if (this.type.equals(a.returnType( )) return 0;
d. if (a.returnType( ).equals(“King”)) return -1;
e. if (a.returnType( ).equals(“Pawn”)) return 1;
/*********************************************************************
Free Form
1.Explain what a NullPointerException is and how it can arise.
2.Consider the condition (x = = y). How is this handled if x and y are primitive types? How
is this handled if x and y are objects?
Polymorphic Objects
3.___Given the Philosopher, Speaker example from the Interface lecture – available on my
website – which of the following are true:
a) All philosophers can speak and announce and pontificate but dogs can only speak.
b) All dogs are speakers but not all speakers are dogs.
c) An interface name can be used to declare a reference variable. It can then be used
to refer to all classes that implement the interface.
d) In class talking, the variable current is initially set to a new dog object. Then
current is assigned to a Philosopher object which is also valid because a philosopher
can be a dog.
e) This action is not possible with regular object references. You cannot assign an
object reference of one class to another class.
4. _____Method Name Conflicts - A class implements two interfaces that each have a
method with the same name Square():
Which of the following is true:
a)If both Square() methods have different signatures, then the class implements two
overloaded methods.
b)If both methods have the same signature and the same return type, then the class
implements one square method to satisfy both.
6
1052
HOMEWORK1 – INHERITANCE, INTERFACES
c) If both Square() methods have the same signature and different return types, then the
class can implement both interfaces.
5.Given the following inheritance structure:
Fruit
Apple
Orange
GoldenDelicious
MacIntosh
Assume the following declarations:
Fruit fruit = new GoldenDelicious();
Orange orange = new Orange();
Answer the following questions:
 Is fruit instanceof Orange true ____
 Is fruit instanceof Apple true ____
 Is orange instanceof Fruit true
____
 Is fruit instanceof MacIntosh true____
7
1052
HOMEWORK1 – INHERITANCE, INTERFACES
8
Download