100% found this document useful (23 votes)
358 views4 pages

C How To Program Late Objects Version 7th Edition Deitel Test Bank

The document contains a test bank of multiple choice questions about object-oriented programming concepts in C++ including polymorphism, inheritance, abstract classes, virtual functions, and runtime type information. It covers topics such as implementing polymorphism via virtual functions, invoking base class functions from derived classes, downcasting pointers, and using virtual destructors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (23 votes)
358 views4 pages

C How To Program Late Objects Version 7th Edition Deitel Test Bank

The document contains a test bank of multiple choice questions about object-oriented programming concepts in C++ including polymorphism, inheritance, abstract classes, virtual functions, and runtime type information. It covers topics such as implementing polymorphism via virtual functions, invoking base class functions from derived classes, downcasting pointers, and using virtual destructors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

C++ How to Program, 7/e Multiple Choice Test Bank 1 of 4

Chapter 13: Object-Oriented Programming: Polymorphism

Section 13.1 Introduction

13.1 Q1: Polymorphism is implemented via:


a. Member functions.
b. virtual functions and dynamic binding.
c. inline functions.
d. Non-virtual functions.
ANS b. virtual functions and dynamic binding.

Section 13.2 Base Classes and Derived Classes

13.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
ANS d. flapWings

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

13.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.
ANS a. Yes.

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

13.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.
ANS: b. Assigning the address of a base-class object to a derived-class pointer.

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

13.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.

© Copyright 1992-2010 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
C++ How to Program, 7/e Multiple Choice Test Bank 2 of 4

ANS: c. Making a base-class pointer into a derived-class pointer.

Section 13.3.4 Virtual Functions

13.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
ANS: b. virtual

13.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.
ANS: c. Be declared virtual in the base class.

13.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.
ANS: c. They do not remain virtual down the inheritance hierarchy.

Section 13.4 Type Fields and switch Statements

13.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.
ANS: d. Not being able to implement separate functions on different objects.

Section 13.5 Abstract Classes and Pure virtual Functions

13.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.
ANS: a. All classes that directly inherit from this class will override this method.

13.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.
ANS: d. Are defined, but the programmer never intends to instantiate any objects from them.

© Copyright 1992-2010 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
C++ How to Program, 7/e Multiple Choice Test Bank 3 of 4

13.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.
ANS: c. That a pure virtual function cannot have an implementation.

13.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.
ANS: a. Objects of abstract classes.

Section 13.6 Case Study: Payroll System Using Polymorphism

13.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.
ANS: a. There are no pure virtual functions.

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

13.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.
ANS: b. Have at least one 0 in its vtable.

13.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.
ANS: c. Receive pointers to their base classes' virtual functions.

13.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.
ANS: b. Have virtual functions.

© Copyright 1992-2010 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
C++ How to Program, 7/e Multiple Choice Test Bank 4 of 4

13.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.
ANS: d. Zero references to their class.

Section 13.8 Case Study: Payroll System Using Polymorphism and Run-Time Type Information with
downcasting, dynamic_cast, typeid and type_info

13.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.
ANS: a. Base class.

13.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.
ANS: c. An object’s type.

13.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
ANS: a. typeid, type_info

13.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.
ANS: d. Downcast pointers.

Section 13.9 Virtual Destructors

13.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.
ANS: b. delete is used on a base-class pointer to a derived-class object.

© Copyright 1992-2010 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.

You might also like