Engineering Problem Solving with C++, 3e Chapter 10 Test Bank
1. Write a template function which will determine the maximum value in an array, where you
can assume that the operator > is defined on the template type.
2. Implement the non-recursive and recursive methods count for the binary tree that will
calculate the number of nodes in a binary tree, using the following definition for a binary tree
class and the node class.
class Node {
//Private attributes
private:
int data;
Node *left;
Node *right;
//Public interface.
public:
Node(); //Default constructor
Node(int); //Parameterized constructor
//Accessors
Node* getLeft() const;
Node* getRight() const;
int getData() const;
//Mutators
void setLeft(Node*);
void setRight(Node*);
void setData(int v);
};
#include “node.h” //Required for node.
#include <iostream> //Required for ostream.
using namespace std;
class BinaryTree {
public:
//Default constructor.
BinaryTree();
//public, non recursive print , insert, and count
void print(ostream& out) const;
void insert(int value);
int count() const;
void clear();
private:
//private recursive overloaded print and insert
void print(ostream& out, node* rt) const;
void insert(node* rt, int value);
int count(node* rt) const;
void clear(node* rt);
//private attribute
Node* root;
};
3. Draw the binary search tree that would be generated from the following input sequence.
24 39 13 7 3 15 29 54 40 22 19
4. Using the following class definition segment for the class card implement the friend function
for operator <<, assuming that the classes suit and pips both contain overloaded operators for
the operator <<.
class card
{
private: suit s;
pips p;
public: …
friend ostream & operator << (ostream & out, const card & c);
};
5. Why should the recursive clear function be written using a post order traversal, rather than an
in order or pre order traversal?
6. A template parameter can be used as
A. A return type
B. The data type of a parameter
C. The data type of a local variable in the function
D. All of the above are valid uses for a template variable
7. Which of the following operators can not be overloaded?
A. *, the multiplication operator
B. >>, the input operator
C. ::, the scope resolution operator
D. All of the above can be overloaded.
8. The number of children that a node in a binary tree may have is:
A. 0
B. 1
C. 2
D. A node in a binary tree may have between 0 to 2 children.
9. When printing a binary tree using a pre order traversal the first node to be printed is:
A. The root node
B. The leftmost leaf node
C. The rightmost leaf node
D. None of the above.
10. When printing a binary tree using a in order traversal the first node to be printed is;
A. The root node
B. The leftmost leaf node
C. The rightmost leaf node
D. None of the above.
11. The is a relationship, such as “a circle is an ellipse” implies …
A. that the circle class can be written as a derived class of the base class ellipse.
B. that the ellipse class can be written as a derived class of the base class circle
C. that the class circle contains a data member that is an ellipse.
D. that the class ellipse contains a data member that is a circle.
12. A template parameter list can contain more than one template variable.
A. True
B. False
13. When overloading the arithmetic operator + the method must be a const method.
A. True
B. False
14. It is possible to define a new operator, such as ^, to raise a value to a power, by writing and
operator method.
A. True
B. False
15. When overloading an operator for a class with a member function the left hand operand must
be a member of the class.
A. True
B. False
16. When overloading the arithmetic operator + with a member function for a class c, the
parameter declared in the member function must be a member of the class.
A. True
B. False
17. To print an object of a user defined class, the function used to overload the output operator
<< must be a member function.
A. True
B. False
18. To write a friend function you include the keyword friend in both the function prototype and
the function header.
A. True
B. False
19. Inheritence is the term for defining a new class based on an existing class by adding data or
operations or overloading existing operations in the class.
A. True
B. False
20. The UML symbol for showing that one class is a derived class of a base class is a solid
diamond headed arrow going from the derived class toward the base class.
A. True
B. False
21. The first statement in the constructor of a derived class, must be an explicit invocation of the
constructor of the base class.
A. True
B. False
22. The keyword protected when used in the definition of a class means that the data members
and methods declared to be protected is only available the class itself and any of its derived
classes.
A. True
B. False
23. Assuming that the print method is not a virtual method in neither the rectangle nor the cube
class, and using the following statements the statement ri.print(cout); will use the print
function in class cube.
Point p1(2,1);
Cube c1(p1,4);
Rectangle r1;
r1 = c1;
r1.print(cout);
A. True
B. False
24. It is possible to assign a variable of the base class type to a variable of the derived class type.
A. True
B. False
25. Assuming that the print method is a virtual method in both the rectangle and cube classes,
and using the following statements the statement r.print(cout); will use the print function in
class cube.
Point p1(2,1);
Cube c1(p1,4);
Rectangle *r;
r = &c1;
r.print(cout);
A. True
B. False
26. Generic programming supports the implementation of a type independent algorithm.
A. True
B. False
27. C++ allows the programmer to define new operators.
Engineering Problem Solving with C++, 3e Chapter 10 Test Bank
A. True
B. False
28. All operators may be overloaded in C++.
A. True
B. False
29. Overloading operators allows the programmer to redefine the behavior of existing operators
to work with programmer-designed types.
A. True
B. False