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 23 STL Algorithms


23.1  Types of STL algorithms are ________.
A. nonmodifying algorithms
B. modifying algorithms
C. numeric algorithms
D. heap algorithms

23.2  Are the STL algorithms defined in a container class such as vector, list, or set?
A. Yes
B. No

23.3  All the STL algorithms except the numeric algorithms are defined in the ___________ header.
A. <numeric>
B. <algorithm>
C. <stl>
D. <vector>
E. <list>

23.4  Numeric algorithms are defined in the ___________ header.
A. <numeric>
B. <algorithm>
C. <stl>
D. <vector>
E. <list>

23.5  What will be displayed by the following code?

int values[] = {12345};
vector<int> intVector(5);

vector<int>::iterator last =
  copy(values, values + 3, intVector.begin());

ostream_iterator<int> output(cout, " ");
cout << "intVector: ";
copy(intVector.begin(), last, output);
A. intVector: 2 3 4
B. intVector: 1 2 3 4
C. intVector: 1 2 3
D. intVector: 3 4 5
E. intVector: 2 3 4 5

23.6  What will be displayed by the following code?

int values[] = {12345};
fill_n(values + 229);

ostream_iterator<int> output(cout, " ");
cout << "values: ";
copy(values, values + 5, output);
A. values: 1 2 3 9 9
B. values: 9 9 9 9 5
C. values: 1 9 9 9 5
D. values: 1 2 9 9 5

23.7  What will be displayed by the following code?

int nextNum()
{
  static int n = 20;
  return n++;
}

int main()
{
  int values[] = {1, 2, 3, 4, 5};
  generate_n(values + 1, 2, nextNum);

  ostream_iterator<int> output(cout, " ");
  cout << "values: ";
  copy(values, values + 5, output);

  return 0;
}

cout << "values: ";
copy(values, values + 5, output);
A. values: 20 21 3 4 5
B. values: 1 20 21 4 5
C. values: 1 20 21 22 5
D. values: 1 20 21 22 23

23.8  What will be displayed by the following code?

bool greaterThan4(int value)
{
  return value > 4;
}

int main()
{
  int values[] = {1, 2, 3, 4, 5, 1, 1};
  remove_if(values, values + 7, greaterThan4);
  
  ostream_iterator<int> output(cout, " ");
  cout << "values: ";
  copy(values, values + 7, output);

  return 0;
}
A. values: 1 2 3 4 5 1 1
B. values: 1 2 3 4 1 1 0
C. values: 1 2 3 4 0 1 1
D. values: 1 2 3 4 1 1 1

23.9  What will be displayed by the following code?

bool greaterThan4(int value)
{
  return value > 4;
}

int main()
{
  int values[] = {1, 2, 3, 4, 5, 1, 1};
  replace_if(values, values + 7, greaterThan4, 999);
  
  ostream_iterator<int> output(cout, " ");
  cout << "values: ";
  copy(values, values + 7, output);

  return 0;
}
A. values: 1 2 3 4 999 1 1
B. values: 1 2 3 999 999 1 1
C. values: 1 2 3 4 999 999 1
D. values: 1 2 3 4 999 999 999

23.10  What will be displayed by the following code?

int values[] = {12344511};
int *p = adjacent_find(values, values + 8);

ostream_iterator<int> output(cout, " ");
cout << "values: ";
copy(p, values + 8, output);
A. values: 3 4 4 5 1 1
B. values: 4 5 1 1
C. values: 5 1 1
D. values: 4 4 5 1 1

23.11  What will be displayed by the following code?

int values[] = {12344511};
rotate(values, values + 5, values + 8);

ostream_iterator<int> output(cout, " ");
cout << "values: ";
copy(values, values + 8, output);
A. values: 4 5 1 1 1 2 3 4
B. values: 5 1 1 1 2 3 4 4
C. values: 1 1 1 2 3 4 4 5
D. values: 1 1 2 3 4 4 5 1

23.12  Suppose array1 is {1, 2, 3, 4, 5}. What is the accumulate of array1?
A. 3
B. 6
C. 10
D. 15

23.13  Suppose array1 is {1, 2, 3, 4, 5}. What is the adjacent_difference of array1?
A. {1, 2, 3, 4, 5}
B. {1, 1, 3, 4, 5}
C. {1, 1, 1, 1, 1}
D. {1, 2, 3, 1, 1}

23.14  Suppose array1 is {1, 2, 3, 4, 5}. What is the partial_sum of array1?
A. {1, 2, 3, 4, 5}
B. {1, 3, 6, 10, 15}
C. {1, 3, 5, 7, 9}
D. {1, 2, 3, 4, 5}

23.15  Suppose array1 is {2, 2, 2, 2, 2} and array2 is {1, 1, 1, 1, 1}. What is the inner product of array1 and array2?
A. 10
B. 5
C. 6
D. 7