ITSC 1213 - TEST 3 QUESTIONS AND CORRECT
ANSWERS
If a java class named Dog inherits a class named Animal then - Answers - Animal is the
superclass and Dog is the subclass
What is the proper class header for Dog if Dog inherits Animal? - Answers - public void
Dog() extends Animal
If you want a class called Collie to inherit both Dog and Animal, how should the headers
of three classes appear? - Answers - public void Animal()
public void Dog() extends Animal
public void Collie() extends Dog
You define a class, Animal, and create an object of Animal, pantherObject. You do not
include a toString() method in Animal. Would the following code compile and execute
normally:
pantherObject.toString(); - Answers - Yes. Because Animal inherits the Java class,
Object. Object includes a toString() method which is automatically inherited by your
class, Animal.
If Dog inherits Animal, Dog has direct access to the ___________________ members
of Animal. - Answers - public
Dog inherits Animal. Animal has a private field named dietField and a public
getter/setter method pair to access dietField. How may Dog access dietField? -
Answers - Dog inherits the public getter/setter methods. It may access dietField through
these methods.
Dog inherits Animal. Dog has a public method getBreed(). Animal does not have a
getBreed() method. What access does Animal have to the getBreed() method that is in
Dog? - Answers - Even though getBreed() is public, Animal has NO access because
Dog inherits Animal, but Animal does not inherit Dog.
or
Only Dog objects created within the Animal class can call the getBreed() method
If Dog inherits Animal, what is the first thing that must happen within the Dog
constructor when a Dog object is created? Assume Animal does not have a default
constructor. - Answers - Call the Animal constructor
, Dog inherits Animal. Animal has only one constructor, and it requires a single String
parameter. In the Dog constructor, how is the Animal constructor called? - Answers -
super(<String_parameter>);
Dog inherits Animal. Some methods of Dog may have the same name as methods of
Animal. If you create a Dog object and and call one of these methods, which version of
the method gets control (i.e. is executed)? - Answers - If the methods have identical
signatures, control is passed to the Dog version of the method.
or
If the methods have identical signatures, control is passed to the Animal method.
Which of the following are valid data types for fields of a class? - Answers - Turtle
String
int
double
Which of the data types below would be considered aggregation if you used them to
define fields in a given class. - Answers - Double
String
Integer
Turtle
You have a fully defined class named Author. Author has a default constructor. You are
defining a new class, Book. One of the fields of Book is an Author object reference
variable. When must you call the Author constructor? - Answers - It is called
automatically when you instantiate the Author object within Book.
A class named Actor extends class Movie and both classes have a method called
getName() that contains different code. An actor object tomCruise and a movie object
missionImpossible have been created. Assume tomCruise gets assigned to
missionImpossible
(i.e. missionImpossible = tomCruise) and we call missionImpossible.getName(), the
code inside of the getName() method in the Actor class will be executed.
To get a polymorphic operation as described above, which of the following must be true.
- Answers - The getName() method must be a member of the subclass: Actor
The getName() method must be a member in the superclass: Movie
The superclass and subclass method return types must be the same.
The superclass and subclass method signatures must be the same
ANSWERS
If a java class named Dog inherits a class named Animal then - Answers - Animal is the
superclass and Dog is the subclass
What is the proper class header for Dog if Dog inherits Animal? - Answers - public void
Dog() extends Animal
If you want a class called Collie to inherit both Dog and Animal, how should the headers
of three classes appear? - Answers - public void Animal()
public void Dog() extends Animal
public void Collie() extends Dog
You define a class, Animal, and create an object of Animal, pantherObject. You do not
include a toString() method in Animal. Would the following code compile and execute
normally:
pantherObject.toString(); - Answers - Yes. Because Animal inherits the Java class,
Object. Object includes a toString() method which is automatically inherited by your
class, Animal.
If Dog inherits Animal, Dog has direct access to the ___________________ members
of Animal. - Answers - public
Dog inherits Animal. Animal has a private field named dietField and a public
getter/setter method pair to access dietField. How may Dog access dietField? -
Answers - Dog inherits the public getter/setter methods. It may access dietField through
these methods.
Dog inherits Animal. Dog has a public method getBreed(). Animal does not have a
getBreed() method. What access does Animal have to the getBreed() method that is in
Dog? - Answers - Even though getBreed() is public, Animal has NO access because
Dog inherits Animal, but Animal does not inherit Dog.
or
Only Dog objects created within the Animal class can call the getBreed() method
If Dog inherits Animal, what is the first thing that must happen within the Dog
constructor when a Dog object is created? Assume Animal does not have a default
constructor. - Answers - Call the Animal constructor
, Dog inherits Animal. Animal has only one constructor, and it requires a single String
parameter. In the Dog constructor, how is the Animal constructor called? - Answers -
super(<String_parameter>);
Dog inherits Animal. Some methods of Dog may have the same name as methods of
Animal. If you create a Dog object and and call one of these methods, which version of
the method gets control (i.e. is executed)? - Answers - If the methods have identical
signatures, control is passed to the Dog version of the method.
or
If the methods have identical signatures, control is passed to the Animal method.
Which of the following are valid data types for fields of a class? - Answers - Turtle
String
int
double
Which of the data types below would be considered aggregation if you used them to
define fields in a given class. - Answers - Double
String
Integer
Turtle
You have a fully defined class named Author. Author has a default constructor. You are
defining a new class, Book. One of the fields of Book is an Author object reference
variable. When must you call the Author constructor? - Answers - It is called
automatically when you instantiate the Author object within Book.
A class named Actor extends class Movie and both classes have a method called
getName() that contains different code. An actor object tomCruise and a movie object
missionImpossible have been created. Assume tomCruise gets assigned to
missionImpossible
(i.e. missionImpossible = tomCruise) and we call missionImpossible.getName(), the
code inside of the getName() method in the Actor class will be executed.
To get a polymorphic operation as described above, which of the following must be true.
- Answers - The getName() method must be a member of the subclass: Actor
The getName() method must be a member in the superclass: Movie
The superclass and subclass method return types must be the same.
The superclass and subclass method signatures must be the same