CSC 205 AB Module 3 Quiz : Inheritance and Abstract classes

¡Supera tus tareas y exámenes ahora con Quizwiz!

Because every class directly or indirectly inherits from the Object class, every class inherits the Object class's members. a.) True b.) False

a.) True

Every class has a toString method and an equals method inherited from the Object class. a.) True b.) False

a.) True

Consider the following class hierarchy: 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. a.) True b.) False

a.) True Anything in common between A, B, Y and Z should be declared in X to properly identify that it is a class trait common amongst them. That is, common elements should be defined as high up in the hierarchy as possible. If all descendants of X have the trait (instance data) d1, then it is part of X too.

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. a.) True b.) False

a.) True Since Poodle extends Dog, Dog is a wider class (Poodle is more specific and therefore narrower). An assignment statement can assign a narrower item into a wider variable (since p is narrower, it can be assigned to d).

Inheritance through an extended (derived) class supports which of the following concepts? a.) code reuse b.) modulary c.) information hiding d.) correctness e.) interfaces

a.) code reuse By extending a class and inheriting from it, the new class does not have to reimplement any of those inherited methods or instance data, thus saving the programmer time and effort. So, code reuse is the ability to reuse someone else's code for your benefit by extending it for your needs.

All methods in an abstract class must also be declared abstract. a.) True b.) False

b.) False

In an inheritance relationship, the subclass constructor always executes before the superclass constructor. a.) True b.) Flase

b.) False

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 d is not a Poodle? a.) True b.) False

b.) False Inheritance is an is-a relationship. This is not legal because a dog is not always a poodle. We are assigning a dog reference to a poodle which is a special type of a dog and might have methods which a dog does not even know about. These are the reasons that this is not a legal assignment in Java.

The instruction super(); does which of the following? a.) It calls the method super as defined in java.lang. b.) It calls the constructor as defined in the current class's parent class. c.) It calls the constructor as defined in the current class. d.) It calls the method super as defined in the current class's parent class. e.) It calls the method super as defined in the current class.

b.) It calls the constructor as defined in the current class's parent class. The instruction super(); represents an invocation of something in the current class's parent class. Since there is no message but merely super(), it is an invocation of the parent class's constructor.

When a subclass overloads a superclass method ________. a.) only the superclass method may be called with a subclass object b.) both methods may be called with a subclass object c.) neither method may be called with a subclass object d.) only the subclass method may be called with a subclass object

b.) both methods may be called with a subclass object

Which of the following is correct? a.) A parent class is a subclass of its child. b.) None of these are correct. c.) A base class is a parent class or super class. d.) A base class is a child class or derived class. e.) A child class is a super class of its parent.

c.) A base class is a parent class or super class. The terms base class, parent class, and super class are synonyms for one another. They all imply that the class will be used as a base for inheritance, and that subsequent classes will extend (inherit from) the base class

Which of the following is TRUE about Java classes? a.) All classes can have either zero or one parent class and any number of child (derived or extended) classes. b.) All classes must have one parent and may have a single child (derived or extended) class. c.) All classes must have one parent but may have any number of children (derived or extended). d.) All classes must have one parent and may have a single child (derived or extended) but may have any number of parent classes. e.) All classes can have any number (zero or more) of parent classes and any number of children (derived or extended) classes.

c.) All classes must have one parent but may have any number of children (derived or extended). Java supports inheritance but not multiple inheritance, so a Java class can have any number of children but only one parent. Further, since all Java classes inherit either directly or indirectly from the Object class, all Java classes have exactly 1 parent class.

If a subclass constructor does not explicitly call a superclass constructor ________. a.) the superclass fields will be set to the default values for their data types b.) it must include the code necessary to initialize the superclass fields c.) Java will automatically call the superclass's default or no-arg constructor just before the code in the subclass's constructor executes d.) Java will automatically call the superclass's default or no-arg constructor immediately after the code in the subclass's constructor executes

c.) Java will automatically call the superclass's default or no-arg constructor just before the code in the subclass's constructor executes

All methods specified by an interface are ________. a.) protected b.) static c.) public d.) private

c.) public

The ________ key word is used to call a superclass constructor explicitly. a.) this b.) goto c.) super d.) extends

c.) super

If a class contains an abstract method ________. a.) you must create an instance of the class b.) None of the others are true c.) the method will only have a header, but not a body, and will end with a semicolon d.) the method cannot be overridden in subclasses

c.) the method will only have a header, but not a body, and will end with a semicolon

A subclass may call an overridden superclass method by ________. a.) using the extends key word before the method is called b.) prefixing its name with the name of the superclass in parentheses c.) calling the superclass method first and then calling the subclass method d.) prefixing its name with the super key word and a dot (.)

d.) prefixing its name with the super key word and a dot (.)

If you instantiate an abstract class, the class or object you wind up with a.) is also an abstract class b.) is a reference to an Object c.) is a normal class d.) you can't instantiate an abstract class. e.) is an interface

d.) you can't instantiate an abstract class. You only can instantiate concrete classes, not abstract ones. But you can extend abstract classes as well as interfaces.

Java does not support multiple inheritance but some of the abilities of multiple inheritance are available by a.) overriding parent class methods b.) creating aliases c.) importing classes d.) using public rather than protected or private modifiers e.) implementing interfaces

e.) implementing interfaces Since a class can implement any number of interfaces, that class is in essence using the interface classes as if those interfaces were defined in this class. So this class is inheriting the methods and constants of the interfaces. Further, the class could extend another class and thus inherit directly and indirectly from multiple classes. This is not the exact same as multiple inheritance, but it is as close as Java comes to that concept.

Aside from permitting inheritance, the visibility modifier protected is also used to a.) define abstract elements of an interface b.) permit access to the protected item by any static class c.) ensure that the class cannot throw a NullPointException d.) permit access to the protected item by any parent class e.) permit access to the protected item by any class defined in the same package

e.) permit access to the protected item by any class defined in the same package The visibility modifier protected is used to control access to the item in a protected (guarded) manner. The protection is that access is restricted to the current class (like private items), classes in the same package, or extended classes of this class.


Conjuntos de estudio relacionados

Week #7.2 Quiz - CIST1220-Structured Query Language-SQL

View Set

Macro Final Chapters 11, 12, 13, 14, 19

View Set

Beaufort 5 - Contact 8 - Woorden

View Set