Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Chapter 21 – C How to Program 6e Multiple Choice Test Bank


 

Download  file with the answers

Not a member!
Create a FREE account here to get access and download this file with answers


Chapter 21: Object-Oriented Programming: Polymorphism

Section 21.1 Introduction

21.1 Q1: Polymorphism is implemented via:
a. Member functions.
b. virtual functions and dynamic binding.
c. inline functions.
d. Non-virtual functions.

Section 21.2 Introduction to Polymorphism: Polymorphic Video Game
21.2 Q1: Which of the following statements about polymorphism is false?
a. With polymorphism, you can direct a variety of objects to behave in manners appropriate to those objects without even knowing their types.
b. With polymorphism, new types of objects that can respond to existing messages can easily be incorporated into a system without modifying the base system.
c. Polymorphism enables you to deal in specifics and let the execution-time environment concern itself with the generalities.
d. To get polymorphic behavior among existing objects, those objects must be instantiated from classes in the same inheritance hierarchy.

Section 21.3 Relationships Among Objects in an Inheritance Hierarchy

21.2 Q1: Which of the following would not be a member function that derived classes Fish, Frog and Bird should inherit from base class Animal and then provide their own definitions for, so that the function call can be performed polymorphically?
a. eat
b. sleep
c. move
d. flapWings

Section 21.3.1 Invoking Base-Class Functions from Derived-Class Objects

21.3.1 Q1: Employee is a base class and HourlyWorker is a derived class, with a redefined non-virtual print function. Given the following statements, will the output of the two print function calls be identical?

HourlyWorker h;
Employee *ePtr = &h;

ePtr->print();
ePtr->Employee::print();

a. Yes.
b. Yes, if print is a static function.
c. No.
d. It would depend on the implementation of the print function.

Section 21.3.2 Aiming Derived-Class Pointers at Base-Class Objects

21.3.2 Q1: Which of the following assignments would be a compilation error?
a. Assigning the address of a base-class object to a base-class pointer.
b. Assigning the address of a base-class object to a derived-class pointer.
c. Assigning the address of a derived-class object to a base-class pointer.
d. Assigning the address of a derived-class object to a derived-class pointer.

Section 21.3.3 Derived-Class Member-Function Calls via Base-Class Pointers

21.3.3 Q1: Downcasting enables:
a. A derived-class object to be treated as a base-class object.
b. A base-class object to be treated as a derived-class object.
c. Making a base-class pointer into a derived-class pointer.
d. Making a derived-class pointer into a base -class pointer.

Section 21.3.4 Virtual Functions

21.3.4 Q1: If objects of all the classes derived from the same base class all need to draw themselves, the draw function would most likely be declared:
a. private
b. virtual
c. protected
d. friend

21.3.4 Q2: virtual functions must:
a. Be overridden in every derived class.
b. Be declared virtual in every derived class.
c. Be declared virtual in the base class.
d. Have the same implementation in every derived class.

21.3.4 Q3: Which of the following statements about virtual functions is false?
a. They allow the program to select the correct implementation at execution time.
b. They can use either static or dynamic binding, depending on the handles on which the functions are called.
c. They do not remain virtual down the inheritance hierarchy.
d. They can be called using the dot operator.

Section 21.4 Type Fields and switch Statements

21.4 Q1: Problems using switch logic to deal with many objects of different types do not include:
a. Forgetting to include an object in one of the cases.
b. Having to update the switch statement whenever a new type of object is added.
c. Having to track down every switch statement to do an update of object types.
d. Not being able to implement separate functions on different objects.

Section 21.5 Abstract Classes and Pure virtual Functions

21.5 Q1: The line:

virtual double earnings() const = 0;

appears in a class definition. You cannot deduce that:
a. All classes that directly inherit from this class will override this method.
b. This class is an abstract class.
c. Any concrete class derived from this class will have an earnings function.
d. This class will probably be used as a base class for other classes.

21.5 Q2: Abstract classes:
a. Contain at most one pure virtual function.
b. Can have objects instantiated from them if the proper permissions are set.
c. Cannot have abstract derived classes.
d. Are defined, but the programmer never intends to instantiate any objects from them.

21.5 Q3: The main difference between a pure virtual function and a virtual function is:
a. The return type.
b. The member access specifier.
c. That a pure virtual function cannot have an implementation.
d. The location in the class.

21.5 Q4: Which of the following is not allowed?
a. Objects of abstract classes.
b. Multiple pure virtual functions in a single abstract class.
c. References to abstract classes.
d. Arrays of pointers to abstract classes.

Section 21.6 Case Study: Payroll System Using Polymorphism

21.6 Q1: What mistake prevents the following class declaration from functioning properly as an abstract class?

class Shape
{
public:
virtual double print() const;
double area() const { return base * height; }
private:
double base;
double height;
};

a. There are no pure virtual functions.
b. There is a non-virtual function.
c. private variables are being accessed by a public function.
d. Nothing, it functions fine as an abstract class.

Section 21.7 (Optional) Polymorphism, Virtual Functions and Dynamic Binding “Under the Hood”

21.7 Q1: An abstract class will:
a. Have all zeros in its vtable.
b. Have at least one 0 in its vtable.
c. Share a vtable with a derived class.
d. Have fewer 0’s in its vtable than concrete classes have.

21.7 Q2: Concrete classes that inherit virtual functions but do not override their implementations:
a. Have vtables which are the same as those of their base classes.
b. Receive their own copies of the virtual functions.
c. Receive pointers to their base classes’ virtual functions.
d. Receive pointers to pure virtual functions.

21.7 Q3: The C++ compiler makes objects take up more space in memory if they:
a. Are derived from base classes.
b. Have virtual functions.
c. Have only protected members.
d. Are referenced by pointers.

21.7 Q4: Abstract classes do not necessarily have:
a. A 0 pointer in their vtable.
b. A virtual function prototype with the notation = 0.
c. Zero instances of their class.
d. Zero references to their class.

Section 21.8 Case Study: Payroll System Using Polymorphism and Runtime Type Information with Downcasting, dynamic_cast, typeid and type_info

21.8 Q1: The line:

virtual double functionX() const = 0;

in a class definition indicates that the class is probably a:
a. Base class.
b. Derived class.
c. Protected class.
d. Library class.

21.8 Q2: Run-time type information can be used to determine:
a. A function’s return type.
b. A function’s argument type.
c. An object’s type.
d. The number of arguments a function takes.

21.8 Q3: The __________ operator returns a reference to a __________ object:
a. typeid, type_info
b. typeinfo, type_id
c. typeid, data_type
d. typeinfo, type

21.8 Q4: dynamic_cast is often used to:
a. Perform type checking for objects.
b. Convert pointers to strings.
c. Upcast pointers.
d. Downcast pointers.

Section 21.9 Virtual Destructors

21.9 Q1: virtual destructors must be used when:
a. The constructor in the base class is virtual.
b. delete is used on a base-class pointer to a derived-class object.
c. delete is used on a derived-class object.
d. A constructor in either the base class or derived class is virtual.

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!