Introduction to Programming with C++, Third Edition, Y. Daniel Liang

This quiz is for students to practice. A large number of additional quiz is available for instructors using Quiz Generator from the Instructor's Resource Website. Videos for Java, Python, and C++ can be found at https://yongdanielliang.github.io/revelvideos.html.

Many questions in this edition have been updated in the new edition. Please check with the publisher on the newest edition.

Chapter 12 Templates, Vectors, and Stacks


Section 12.2 Templates Basics
12.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.

12.2  The template prefix may be defined as _________.
A. template<typename T>
B. template<class T>

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

12.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)

12.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)

12.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 12.3 Example: A Generic Sort
12.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);

Section 12.4 Class Templates
12.8  Suppose you define

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;

12.9  Suppose you define

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;

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

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

Section 12.6 The C++ vector Class
12.12  Which of the following statements are true?
A. The array size is fixed in the class declaration.
B. C++ provides the vector class and you can create vector objects.
C. A vector object is just like an array, but a vector?s size can grow automatically if needed.
D. A vector has a no-arg constructor.

12.13  To declare a vector for holding int values, use __________.
A. vector<int> v;
B. vector v;
C. vector v<int>;
D. vector<int> v();

12.14  To add an int value 5 to a vector v of integers, use _________.
A. v.add(5);
B. v.insert(5);
C. v.push_back(5);
D. v.append(5);

12.15  To obtain the size of the vector v, use _______.
A. v.getSize();
B. v.length();
C. v.getLength();
D. v.size();

12.16  To delete all the elements in a vector v, use _______.
A. v.deleteAll();
B. v.clear();
C. v.eraseAll();
D. v.delele();

12.17  To obtain the first element in a vector v, use _______.
A. v.at(0);
B. v[0];
C. v.at(1);
D. v[1];

12.18  What is wrong in the following code?

  #include <iostream>
  #include <vector>
  using namespace std;

  int main()
  {
    vector<int> v;
    cout << v[0];
    return 0;
  }
A. The program has a compile error on v[0].
B. The program has a runtime error on v[0], because the vector is empty.
C. The program has a compile error on vector<int> v.
D. The program has a runtime error on vector<int> v.

12.19  What is wrong in the following code?

  vector<string> v;
  v.push_back("Beijing");
  v.push_back("Tokyo");
  v.push_back("Shanghai");
  v[3] = "Hong Kong";
A. The last line in the code causes a runtime error because there is no element at index 3 in the vector.
B. The last line in the code has a compile error because there is no element at index 3 in the vector.
C. If you replace the last line by v[2] = "Hong Kong", the code will compile and run fine.
D. If you replace the last line by cout << v[3] << endl, the code will compile and run fine.
E. If you replace the last line by cout << v[2] << endl, the code will compile and run fine.