Introduction to C++ Programming, Y. Daniel Liang

Chapter 15 Templates


Section 15.2 Templates Basics
1  Which of the following statements is correct?

A. Templates provide the capability to parameterize types in functions and classes.
B. With templates, you can define one function or one class with a generic type that can be substituted for a concrete type by the compiler.
C. Templates facilitates developing reusable software.
D. Templates improves performance.

2  The template prefix may be defined as _________.

A. template<typename T>
B. template<class T>

3  A template prefix for two parameters may be defined as _____________.

A. template<typename T1, typename T2>
B. template<class T1, class T2>
C. template<typename T1, T2>
D. template<class T1, T2>

4  Suppose a template function is defined as follows:

template<typename T>
T maxValue(const T &value1, const T &value2)
{
   if (value1 > value2)
     return value1;
   else
     return value2;
}
Which of the following statements are correct?


A. cout << maxValue(1, 2)
B. cout << maxValue(1.5, 2.5)
C. cout << maxValue('A', 'B')
D. cout << maxValue("AB", "AB")
E. cout << maxValue(1.5, 2)

5  Suppose a template function is defined as follows:

template<typename T1, typename T2>
T1 maxValue(const T1 &value1, const T2 &value2)
{
   if (value1 > value2)
     return value1;
   else
     return value2;
}

Which of the following statements are correct?


A. cout << maxValue(1, 2)
B. cout << maxValue(1.5, 2.5)
C. cout << maxValue('A', 'B')
D. cout << maxValue("AB", "AB")
E. cout << maxValue(1.5, 2)

6  If you define the swap function as follows:

template<typename T>
void swap(T &var1, T &var2)
{
   T temp = var1;
   var1 = var2;
   var2 = temp;
}

You can invoke swap using ______.


A. swap(1, 2)
B. int v1 = 1; int v2 = 2; swap(v1, v2);
C. int v1 = 1; int v2 = 2; swap(&v1, &v2);
D. int v1 = 1; double v2 = 2; swap(v1, v2);

Section 15.3 Example: A Generic Sort
7  Suppose a template function is defined as follows:

template<typename T>
void printArray(T list[], int arraySize)
{
   for (int i = 0; i < arraySize; i++)
   {
     cout << list[i] << " ";
   }
   cout << endl;
}
Which of the following statements are correct?


A. int list[] = {1, 2, 3, 4}; printArray(list, 4);
B. int list[] = {1, 2.5, 3, 4}; printArray(list, 4);
C. double list[] = {1, 2, 3, 4}; printArray(list, 4);
D. string list[] = {"Atlanta", "Dallas", "Houston", "Chicago"}; printArray(list, 4);

8  Suppose you declare

template<typename T = int>
class Stack
{
   Stack();
   ...
};

Which of the following statements are correct?


A. Stack<double> s;
B. Stack<int> s;
C. Stack<> s;
D. Stack s;
E. Stack<int, double> s;

9  Suppose you declared

template<typename T, int capacity>
class Stack
{
   Stack();
   ...
private:
   T elements[capacity];
   int size;
};

Which of the following statements are correct?


A. Stack<double, 40> s;
B. Stack<int, 50> s;
C. Stack<50> s;
D. Stack<int, double> s;

10  Which of the following statements are true?

A. A class template can be derived from a class template.
B. A class template can be derived from a nontemplate class.
C. A nontemplate class can be derived from a class template specialization.d. Stack<int, double> s;
D. Friends are used exactly the same for template and nontemplate classes.
E. You can define static members in a template class. Each template specialization has its own copy of a static data field.

11  In the implementation of ImprovedStack.h, which of the following are true?
A. size never reduces.
B. capacity never reduces.
C. Inside Stack, a regular array is used to store elements.
D. If the current capacity equals to size, capacity is doubled when a new element is added to Stack.