Introduction to C++ Programming, Y. Daniel Liang

Chapter 20 STL Algorithms


1  Types of STL algorithms are ________.

A. nonmodifying algorithms
B. modifying algorithms
C. numeric algorithms
D. heap algorithms

2  Are the STL algorithms defined in a container class such as vector, list, or set?

A. Yes
B. No

3  All the STL algorithms except the numeric algorithms are defined in the ___________ header.

A. <numeric>
B. <algorithm>
C. <stl>
D. <vector>
E. <list>

4  Numeric algorithms are defined in the ___________ header.

A. <numeric>
B. <algorithm>
C. <stl>
D. <vector>
E. <list>

5  What is the printout of the following code?

int values[] = {1, 2, 3, 4, 5};
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

6  What is the printout of the following code?

int values[] = {1, 2, 3, 4, 5};
fill_n(values + 2, 2, 9);

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

7  What is the printout of 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

8  What is the printout of 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

9  What is the printout of 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

10  What is the printout of the following code?

int values[] = {1, 2, 3, 4, 4, 5, 1, 1};
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

11  What is the printout of the following code?

int values[] = {1, 2, 3, 4, 4, 5, 1, 1};
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

12  Suppose array1 is {1, 2, 3, 4, 5}. What is the accumulate of array1?

A. 3
B. 6
C. 10
D. 15

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}

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}

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