Cisco Networking Academy 5
Cisco Networking Academy 5
7.1 Module 7
Completion –
Module Test
Scroll to begin
Question 1
What will happen when you attempt to compile and run the following code?
1 #include <vector>
2
3 #include <iostream>
4
5 #include <algorithm>
6
7 #include <functional>
8
9 using namespace std;
10
11 void
12 printer (int i)
13 {
14
15 cout << i << ", ";
16
17 }
18
19 int
20 main ()
21 {
22
23 int mynumbers1[] = { 3, 9, 0, 2 };
24
25 int mynumbers2[] = { 6, 1, 4, 5 };
26
27 vector < int >v1 (4);
28
29 transform (mynumbers1, mynumbers1 + 4, mynumbers2, v1.rbegin (), pl
30
31 for_each (v1.rbegin (), v1.rend (), printer); //LINE II
32
33 return 0;
34
35 }
36
signal_cellular_4_bar
close program outputs: 6, 9, 4, 5,
program outputs: 3, 9, 0, 2, 6, 1, 4, 5,
The correct answer is "program outputs: 9, 10, 4, 7,". The code initializes two integer
arrays mynumbers1 and mynumbers2.
It creates a vector v1 of integers with 4 elements. It uses the transform function to
apply the binary operation plus() on each pair of corresponding elements from
mynumbers1 and mynumbers2, storing the result in v1 in reverse order (LINE I). This
means v1[0] = mynumbers1[3] + mynumbers2[0], v1[1] = mynumbers1[2] +
mynumbers2[1], and so on. It iterates over the elements of v1 in reverse order using
for_each and prints each element to the console (LINE II).
"compilation error in LINE I" and "runtime error at LINE I" are incorrect because there
are no compilation or runtime errors in the provided code.
"program outputs: 4, 10, 1, 3, 7, 2, 5, 6 ," is incorrect because the elements are not
in the order specified. The correct order is "9, 10, 4, 7,".
"program outputs: 6, 9, 4, 5," is incorrect for the same reason as the first incorrect
answer.
Question 2
What will happen when you attempt to compile and run the following code?
1 #include <vector>
2
3 #include <iostream>
4
5 #include <algorithm>
6
7 #include <functional>
8
9 using namespace std;
10
11 void
12 printer (int i)
13 {
14
15 cout << i << ", ";
16
17 }
18
19 int
20 main ()
21 {
22
https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 5/59
4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test
23 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
24
25 vector < int >v1 (mynumbers, mynumbers + 7);
26
27 for_each (v1.begin (), v1.end (), bind2nd (plus < int >(), 1));
28
29 for_each (v1.rbegin (), v1.rend (), printer); //LINE II
30
31 return 0;
32
33 }
34
program outputs: 3, 9, 0, 2, 1, 4, 5,
program outputs: 5, 4, 1, 2, 0, 9, 3,
"compilation error in LINE I" and "runtime error at LINE I" are incorrect because there
are no compilation or runtime errors in the provided code.
"runtime error at LINE II" is incorrect because there are no runtime errors in the
provided code.
Question 3
What will happen when you attempt to compile and run the following code?
1 #include <vector>
2
3 #include <iostream>
4
5 #include <algorithm>
6
7 #include <functional>
8
9 using namespace std;
10
11 void
12 printer (int i)
13 {
14
https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 7/59
4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test
program outputs: 0, 5, 4, 1, 2, 0, 9, 3,
program outputs: 3, 9, 0, 2, 1, 4, 5,
program outputs: 7, 5, 4, 1, 2, 0, 9, 3,
"runtime error at LINE I" and "compilation error in LINE I" are incorrect because there
are no compilation or runtime errors in the provided code.
"runtime error at LINE II" is incorrect because there are no runtime errors in the
provided code.
"program outputs: 4, 10, 1, 3, 2, 5, 6," is incorrect because the output should include
the count counter at the end, not the elements of v1.
Question 4
What will happen when you attempt to compile and run the following code?
1 #include <vector>
2
3 #include <iostream>
4
5 #include <algorithm>
6
7 #include <functional>
8
9 using namespace std;
10
11 void
12 printer (int i)
13 {
14
15 cout << i << ", ";
16
17 }
18
19 int
20 main ()
21 {
22
23 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
24
25 vector < int >v1 (mynumbers, mynumbers + 7);
26
27 int counter = count_if (v1.begin (), v1.end (), bind1st (less_equal
28
29 v1.push_back (counter); //LINE II
30
31 for_each (v1.rbegin (), v1.rend (), printer);
32
33 return 0;
34
35 }
36
program outputs: 4, 5, 4, 1, 2, 0, 9, 3,
program outputs: 7, 3, 9, 0, 2, 1, 4, 5,
program outputs: 7, 5, 4, 1, 2, 0, 9, 3,
program outputs: 3, 5, 4, 1, 2, 0, 9, 3,
The correct answer is: "program outputs: 3, 5, 4, 1, 2, 0, 9, 3,". The code initializes
an integer array mynumbers. It creates a vector v1 and initializes it with the elements
of mynumbers. It uses the count_if function to count the number of elements in v1 that
satisfy the condition of being less than or equal to 4 (LINE I). It binds the first argument
of the less_equal() binary function to 4, effectively checking if each element is less
than or equal to 4. The count is stored in the variable counter. It adds counter to the
end of the vector v1 using push_back() (LINE II). It iterates over the elements of v1 in
reverse order using for_each and prints each element to the console.
"runtime error at LINE II" is incorrect because there are no runtime errors in the
provided code.
"compilation error in LINE I" and "runtime error at LINE I" are incorrect because there
are no compilation or runtime errors in the provided code.
Question 5
What will happen when you attempt to compile and run the following code?
1 #include <vector>
2
3 #include <iostream>
4
5 #include <algorithm>
6
7 #include <functional>
8
9 using namespace std;
10
11 void
12 printer (int i)
13 {
14
15 cout << i << ", ";
16
17 }
18
19 int
20 main ()
21 {
22
23 int mynumbers1[] = { 3, 9, 0, 2, 1, 4, 5 };
24
25 int mynumbers2[] = { 9, 0, 2, 1, 4, 5, 3 };
26
27 vector < int >v1 (mynumbers1, mynumbers1 + 7);
28
29 vector < int >v2 (mynumbers1, mynumbers1 + 7);
30
31 vector < int >v3 (mynumbers2, mynumbers2 + 7); //LINE I
32
33 transform (v1.begin (), v1.end (), v2.rbegin (), v3.begin (), minus
34
35 for_each (v3.rbegin (), v3.rend (), printer);
36
37 return 0;
38
39 }
40
program outputs: 2, 5, 1, 0, 1, 5, 2,
The correct answer is: "program outputs: 2, -5, 1, 0, -1, 5, -2,". The code initializes
two integer arrays, mynumbers1 and mynumbers2, with different orderings of the
same set of numbers. It creates three vectors v1, v2, and v3 initialized with the
elements of mynumbers1 and mynumbers2. It applies the transform function to v1 and
v2, subtracting each corresponding element of v2 from the corresponding element of
v1. The result is stored in v3 (LINE II). It iterates over the elements of v3 in reverse
order using for_each and prints each element to the console.
"compilation error in LINE I" is incorrect because there are no compilation errors in the
provided code.
"runtime error at LINE II" and "runtime error at LINE I" are incorrect because there are
no runtime errors in the provided code.
"program outputs: 6, -9, 2, -1, 3, 1, -2," is incorrect because the output should be
the result of subtracting corresponding elements of v2 from v1, not the elements
themselves.
Question 6
What will happen when you attempt to compile and run the following code?
1 #include <vector>
2
3 #include <iostream>
4
5 #include <algorithm>
6
7 #include <functional>
8
The correct answer is: "program outputs: 4, 3, 0, 1, -1, 8, 2,". The code defines a
struct Add that inherits from binary_function (LINE I). This struct provides the
operator() function to perform addition between two integers. It initializes an array
mynumbers with some integer values and creates two vectors v1 and v2, where v2 is
initialized with the size of 7 but all elements are default-initialized to 0. It applies the
transform function to v1 and assigns the result to v2. The transformation adds -1 to
each element of v1 (LINE II). It iterates over the elements of v2 in reverse order using
for_each and prints each element to the console.
"compilation error in LINE I" is incorrect because there are no compilation errors in the
provided code.
"runtime error at LINE II" and "runtime error at LINE I" are incorrect because there are
no runtime errors in the provided code.
Question 7
What will happen when you attempt to compile and run the following code?
1 #include <vector>
2
3 #include <iostream>
4
5 #include <algorithm>
6
7 #include <functional>
8
9 using namespace std;
10
11 struct Add:public binary_function < int, int, int >
12 {
13
14 int operator () (int &_Left, const int &_Right) const //LINE I
15 {
16 return _Left + _Right;
17 }
18
19 };
20
21 void
22 printer (int i)
23 {
24
25 cout << i << ", ";
26
27 }
28
29 int
30 main ()
31 {
32
33 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
34
35 vector < int >v1 (mynumbers, mynumbers + 7);
36
37 vector < int >v2 (7);
38
39 transform (v1.begin (), v1.end (), v2.begin (), bind1st (Add (), -1
40
41 for_each (v2.rbegin (), v2.rend (), printer);
42
43 return 0;
44
45 }
46
The correct answer is: "compilation error in LINE I" and "compilation error in LINE II".
In LINE I, the operator() function of the Add struct is declared to take a non-const
reference to int (int &_Left) as its first argument. However, when using transform and
bind1st, the elements of the input range (v1) are passed as const references, which
cannot be bound to non-const references. This results in a compilation error. In LINE
II, the bind1st function tries to bind the first argument of the Add functor to -1, but
since LINE I results in a compilation error, the Add functor cannot be instantiated,
causing another compilation error.
"runtime error at LINE II" is incorrect because there are compilation errors, not runtime
errors, in the code.
"program outputs: 4, 3, 0, 1, -1, 8, 2," is incorrect because the code fails to compile
due to errors in LINE I and LINE II. The program does not produce any output.
Question 8
What will happen when you attempt to compile and run the following code?
1 #include <vector>
2
3 #include <iostream>
4
5 #include <algorithm>
6
7 #include <functional>
8
9 using namespace std;
10
11 struct Add:public binary_function < int, int, int >
12 {
13
14 int operator () (const int &_Left, const int &_Right) const //LIN
15 {
16 return _Left + _Right;
17 }
18
19 };
20
21 void
22 printer (int i)
23 {
24
25 cout << i << ", ";
26
27 }
28
29 int
30 main ()
31 {
32
33 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
34
35 vector < int >v1 (mynumbers, mynumbers + 7);
36
37 vector < int >v2 (7);
38
39 transform (v1.begin (), v1.end (), v2.begin (), bind1st (Add, -1));
40
41 for_each (v2.rbegin (), v2.rend (), printer);
42
43 return 0;
44
45 }
46
The correct answer is: "compilation error in LINE II". In LINE II, when using bind1st,
the function object Add should be passed as an object, but it's missing the
parentheses to create an object of type Add. It should be bind1st(Add(), -1) instead of
bind1st(Add, -1). The code fails to compile due to this syntax error in LINE II.
"runtime error at LINE II" is incorrect because there is no runtime error. The issue is
with the compilation due to a syntax error.
"compilation error in LINE I" is incorrect because the issue is in LINE II, not LINE I.
Question 9
What will happen when you attempt to compile and run the following code?
1 #include <deque>
2
3 #include <iostream>
4
5 #include <algorithm>
6
7 #include <functional>
8
9 using namespace std;
10
11 void
12 printer (int i)
13 {
14
15 cout << i << ", ";
16
17 }
18
19 struct MultiAdd:public binary_function < int, int, int >
20 {
21
22 int operator () (const int &_Left, const int &_Right) const
23 {
24 return 2 * (_Left + _Right);
25 }
26
27 };
28
29 int
30 main ()
31 {
32
33 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
34
35 deque < int >d1 (mynumbers, mynumbers + 7);
36
37 deque < int >d2 (7); //LINE I
38
39 transform (d1.begin (), d1.end (), d2.begin (), bind2nd (MultiAdd (
40
41 for_each (d2.begin (), d2.end (), printer);
42
43 return 0;
44
45 }
46
The correct answer is: "program outputs: 8, 20, 2, 6, 4, 10, 12,". The transform
function applies the MultiAdd functor to each element in d1.
MultiAdd doubles the sum of each element of d1 and the value passed as the second
argument (which is 1 in this case). So, for each element x in d1, MultiAdd(x, 1)
computes 2 * (x + 1). The resulting values are stored in d2. The program then prints
the elements of d2, resulting in the output: 8, 20, 2, 6, 4, 10, 12.
"program outputs: 12, 10, 4, 6, 2, 20, 8," is incorrect because the expected output is
the result of applying MultiAdd to each element of d1.
"compilation error in LINE II" is incorrect because there is no compilation error in LINE
II. The code compiles successfully.
"compilation error in LINE I" is incorrect because there is no compilation error in LINE
I. The code compiles successfully.
Question 10
What will happen when you attempt to compile and run the following code?
1 #include <vector>
2
3 #include <iostream>
4
5 #include <algorithm>
6
https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 24/59
4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test
7 #include <functional>
8
9 using namespace std;
10
11 void
12 printer (int i)
13 {
14
15 cout << i << ", ";
16
17 }
18
19 struct Add
20 {
21
22 int operator () (const int &_Left, const int &_Right) const //LI
23 {
24 return _Left + _Right;
25 }
26
27 };
28
29 int
30 main ()
31 {
32
33 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
34
35 vector < int >v1 (mynumbers, mynumbers + 7);
36
37 vector < int >v2 (7);
38
39 transform (v1.begin (), v1.end (), v2.begin (), bind1st (ptr_fun (A
40
41 for_each (v2.begin (), v2.end (), printer);
42
43 return 0;
44
45 }
46
program outputs: 3, 9, 0, 2, 1, 4, 5,
The correct answer is: "compilation error in LINE II". ptr_fun is used to adapt a
function pointer to a function object. However, Add is not a function pointer; it's a
function object. ptr_fun expects a function pointer as its argument, not a function
object like Add. Therefore, the compilation fails at LINE II because ptr_fun cannot
adapt Add to a function pointer.
"compilation error in LINE I" is incorrect because there is no compilation error in LINE
I. The code for Add is correctly defined.
Question 11
What will happen when you attempt to compile and run the following code?
1 #include <vector>
2
3 #include <iostream>
4
5 #include <algorithm>
6
7 #include <functional>
8
9 using namespace std;
10
11 int
12 Mul (int &_Left)
13 {
14 return 2 * _Left;
15 }
16
17 int
18 main ()
19 {
20
21 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
22
23 vector < int >v1 (mynumbers, mynumbers + 7);
24
25 vector < int >v2 (7);
26
27 transform (v1.begin (), v1.end (), v2.begin (), ptr_fun (Mul));
28
29 vector < int >::iterator it = find_if (v2.begin (), v2.end (), bind
30
31 cout << *it << endl; //LINE III
32
33 return 0;
34
35 }
36
program outputs: 3
program outputs: 0
program outputs: 6
The correct answer is: "undefined behavior at LINE III could be a runtime error or 0".
The transform function is applied to each element of v1, and it attempts to call the Mul
function for each element. However, Mul takes its argument by non-const reference,
but transform passes arguments by const reference. Passing a non-const reference to
a function expecting a const reference can lead to undefined behavior. This means
that the behavior of the program at LINE III, where it attempts to print the result of
find_if, is uncertain. It could result in a runtime error or some unspecified value.
"program outputs: 3" is incorrect because there is no certainty about the output due to
undefined behavior.
"compilation error in LINE I" is incorrect because there is no compilation error in LINE
I.
"program outputs: 6" is incorrect because there is no certainty about the output due to
undefined behavior.
"runtime error at LINE II" is incorrect because there is no runtime error at LINE II.
"program outputs: 0" is incorrect because there is no certainty about the output due to
undefined behavior.
Question 12
What will happen when you attempt to compile and run the following code?
1 #include <vector>
2
3 #include <iostream>
4
5 #include <algorithm>
6
7 using namespace std;
8
9 int main()
10 {
11 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
12 vector<int> v1(mynumbers, mynumbers + 7);
13 vector<int> v2(7);
14
https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 29/59
4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test
program outputs: 3
program outputs: 6 6
program outputs: 6
program outputs: 0
The correct answer is: "program outputs: 6". The code initializes a vector v1 with the
elements from mynumbers. It then creates an empty vector v2 of size 7. Using
transform, it applies a lambda function to each element of v1 and stores the result in
v2. The lambda function doubles each element of v1, except when the element is 3, in
which case it replaces it with 6. After the transformation, it searches for the first
occurrence of 6 in v2 using the find function. If it finds 6, it prints it, otherwise, it
prints "6 not found". The lambda function replaces 3 with 6 and doubles all other
elements. Since there is at least one occurrence of 3 in the v1 vector, the lambda
function will return 6 for that element. Therefore, the program outputs 6.
"compilation error in LINE I" is incorrect because there is no compilation error in LINE
I.
"runtime error at LINE I" is incorrect because there is no runtime error at LINE I.
"program outputs: 3" is incorrect because the lambda function replaces 3 with 6.
"program outputs: 0" is incorrect because the program does find 6 in the vector v2.
"program outputs: 6 6" is incorrect because the program only prints the first
occurrence of 6.
"runtime error at LINE II" is incorrect because there is no runtime error at LINE II.
Question 13
What will happen when you attempt to compile and run the following code?
1 #include <vector>
2
3 #include <functional>
4
5 #include <iostream>
6
7 #include <algorithm>
8
9 using namespace std;
10
11 class Pocket
12 {
13
14 int value;
15
16 public:
17
18 Pocket (int value):value (value)
19 {
20 }
21
22 int getValue () const
23 {
24 return value;
25 }
26
27 bool operator < (const Pocket & _Right) const
28 {
29 return value < _Right.value;
30 }
31
32 operator int () const
33 {
34 return value;
35 }
36
37 };
38
39 ostream & operator << (ostream & stream, const Pocket & pocket)
40 {
https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 32/59
4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test
41
42 stream << pocket.getValue (); //LINE I
43
44 return stream;
45
46 }
47
48 void
49 printer (Pocket i)
50 {
51
52 cout << i << ", ";
53
54 }
55
56 int
57 main ()
58 {
59
60 Pocket mynumbers1[] = { 3, 9, 0, 2 };
61
62 Pocket mynumbers2[] = { 2, 1, 4, 5 };
63
64 vector < Pocket > v1 (5, 0);
65
66 transform (mynumbers1, mynumbers1 + 4, mynumbers2, v1.rbegin (), pl
67
68 for_each (v1.rbegin (), v1.rend (), printer);
69
70 return 0;
71
72 }
73
The correct answer is: "program outputs: 5, 10, 4, 7, 0". The code defines a class
Pocket with a constructor that initializes its value. It overloads the < operator and the
stream insertion operator << for Pocket. In the main function, it initializes two arrays of
Pocket objects, mynumbers1 and mynumbers2. It creates a vector v1 of size 5,
initialized with default-constructed Pocket objects. It then uses transform to apply the
binary plus function to corresponding elements of mynumbers1 and mynumbers2,
storing the results in v1 in reverse order (v1.rbegin()). Finally, it prints the elements of
v1 using for_each and the printer function. The transform function applies the plus
function to each pair of Pocket objects from mynumbers1 and mynumbers2, storing
the result in v1 in reverse order. So, the output is: 5 (3 + 2), 10 (9 + 1), 4 (0 + 4), 7 (2
+ 5), and 0 (default-constructed).
"program outputs: 7, 4, 10, 5, 0" is incorrect because the elements are output in
reverse order, as v1 was initialized with 5, 0 and the for_each loop traverses it in
reverse.
"compilation error in LINE I" is incorrect because there is no compilation error in LINE
I.
"runtime error at LINE I" is incorrect because there is no runtime error at LINE I.
"runtime error at LINE II" is incorrect because there is no runtime error at LINE II.
"program outputs: 5, 10, 4, 7," is incorrect because it's missing the last element,
which is 0.
Question 14
What will happen when you attempt to compile and run the following code?
1 #include <vector>
2
3 #include <iostream>
4
5 #include <algorithm>
6
7 #include <functional>
8
9 using namespace std;
10
11 class Pocket
12 {
13
14 int value;
15
16 public:
17
18 Pocket (int value):value (value)
19 {
20 }
21
22 int getValue () const
23 {
24 return value;
25 }
26
27 operator int () const
28 {
29 return value;
30 }
31
32 bool operator < (const Pocket & _Right) const
33 {
34 return value < _Right.value;
35 }
36
37 };
38
39 ostream & operator << (ostream & stream, const Pocket & pocket)
40 {
41
42 stream << pocket.getValue ();
43
44 return stream;
45
46 }
47
48 void
49 printer (Pocket i)
50 { //LINE I
51
52 cout << i << ", ";
53
54 }
55
56 int
57 main ()
58 {
59
60 Pocket mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
61
62 vector < Pocket > v1 (mynumbers, mynumbers + 7);
63
64 transform (v1.begin (), v1.end (), v1.begin (), bind2nd (plus < Poc
65
66 for_each (v1.rbegin (), v1.rend (), printer);
67
68 return 0;
69
70 }
71
The correct answer is "program outputs: 6, 5, 2, 3, 1, 10, 4,". The code defines a
class Pocket with a constructor that initializes its value. It overloads the < operator and
the stream insertion operator << for Pocket. In the main function, it initializes an array
of Pocket objects, mynumbers, and creates a vector v1 from this array. It uses
transform to apply the binary plus function to each element of v1, adding 1 to each
element and storing the result back in v1. Finally, it prints the elements of v1 in
reverse order using for_each and the printer function. The transform function applies
the plus function to each element of v1, adding 1 to each element. So, the output is: 6
(3 + 1), 5 (9 + 1), 2 (0 + 1), 3 (2 + 1), 1 (1 + 1), 10 (4 + 1), and 4 (5 + 1).
"program outputs: 4, 10, 1, 3, 2, 5, 6," is incorrect because the elements are output
in the wrong order.
"runtime error at LINE II" is incorrect because there is no runtime error at LINE II.
"program outputs: 6, 5, 2, 3, 1, 10, 4, 0," is incorrect because the code doesn't add
any additional elements to v1, so there is no trailing "0" in the output.
"compilation error in LINE II" is incorrect because there is no compilation error in LINE
II.
"compilation error in LINE I" is incorrect because there is no compilation error in LINE
I.
Question 15
What will happen when you attempt to compile and run the following code?
1 #include <vector>
2
3 #include <iostream>
4
5 #include <algorithm>
6
7 #include <functional>
8
9 using namespace std;
10
11 class Pocket {
12
13 int value;
14
15 public:
16
17 Pocket(int value):value(value){}
18
19 int getValue() const
https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 38/59
4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test
20
21 { return value; }
22
23 operator int() const
24
25 { return value; }
26
27 bool operator < (const Pocket & _Right) const
28
29 { return value < _Right.value; }
30
31 };
32
33 ostream & operator <<(ostream & stream, const Pocket & pocket)
34
35 {
36
37 stream << pocket.getValue();
38
39 return stream;
40
41 }
42
43 void printer(Pocket i) {//LINE I
44
45 cout << i << ", ";
46
47 }
48
49 int main() {
50
51 Pocket mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
52
53 vector<Pocket> v1(mynumbers, mynumbers+7);
54
55 for_each(v1.begin(), v1.end(), bind1st(plus<Pocket>(), 1));//LINE
56
57 for_each(v1.rbegin(), v1.rend(), printer);
58
59 return 0;
60
61 }
62
program outputs: 5, 4, 1, 2, 0, 9, 3,
"program outputs: 6, 5, 2, 3, 1, 10, 4," is incorrect because the elements are output
in the wrong order.
"runtime error at LINE II" is incorrect because there is no runtime error at LINE II.
"compilation error in LINE I" is incorrect because there is no compilation error in LINE
I.
"compilation error in LINE II" is incorrect because there is no compilation error in LINE
II.
"program outputs: 6, 5, 2, 3, 1, 10, 4, 0," is incorrect because the code doesn't add
any additional elements to v1, so there is no trailing "0" in the output.
Question 16
What will happen when you attempt to compile and run the following code?
1 #include <vector>
2
3 #include <iostream>
4
5 #include <algorithm>
6
7 #include <functional>
8
9 using namespace std;
10
11 class Pocket
12 {
13
14 int value;
15
16 public:
17
18 Pocket (int value):value (value)
19 {
20 }
21
62 Pocket mynumbers2[] = { 3, 8, 0, 1, 0, 2, 2 };
63
64 vector < Pocket > v1 (mynumbers1, mynumbers1 + 7);
65
66 vector < Pocket > v2 (mynumbers2, mynumbers2 + 7);
67
68 vector < Pocket > v3 (7, 0);
69
70 transform (v1.begin (), v1.end (), v2.begin (), v3.begin (), minus
71
72 for_each (v1.rbegin (), v1.rend (), printer);
73
74 return 0;
75
76 }
77
program outputs: 0, 1, 0, 1, 1, 2, 3,
program outputs: 5, 4, 1, 2, 0, 9, 3,
program outputs: 3, 2, 1, 1, 0, 1, 0,
the stream insertion operator << for Pocket. In the main function, it initializes two
arrays of Pocket objects, mynumbers1 and mynumbers2, and creates vectors v1 and
v2 from these arrays. It creates a third vector v3 of Pocket objects, initialized with 7
elements, all set to 0.It uses transform with minus() to subtract corresponding
elements of v2 from v1 and store the result in v3. Finally, it prints the elements of v1 in
reverse order using for_each and the printer function. The transform function with
minus() subtracts corresponding elements of v2 from v1 and stores the result in v3.
Since v1 and v2 have the same elements, the subtraction results in: 5-3, 4-9, 1-0, 2-
2, 0-1, 9-4, and 3-5, which are 2, -5, 1, 0, -1, 5, and -2 respectively.
"compilation error in LINE II" is incorrect because there is no compilation error in LINE
II.
"compilation error in LINE I" is incorrect because there is no compilation error in LINE
I.
"runtime error at LINE II" is incorrect because there is no runtime error at LINE II.
Question 17
What will happen when you attempt to compile and run the following code?
1 #include <vector>
2
3 #include <iostream>
4
5 #include <algorithm>
https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 44/59
4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test
6
7 #include <functional>
8
9 using namespace std;
10
11 class Pocket
12 {
13
14 int value;
15
16 public:
17
18 Pocket (int value):value (value)
19 {
20 }
21
22 int getValue () const
23 {
24 return value;
25 }
26
27 operator int () const
28 {
29 return value;
30 }
31
32 bool operator < (const Pocket & _Right) const
33 {
34 return value < _Right.value;
35 }
36
37 };
38
39 ostream & operator << (ostream & stream, const Pocket & pocket)
40 {
41
42 stream << pocket.getValue ();
43
44 return stream;
45
46 }
47
48 void
49 printer (Pocket i)
50 { //LINE I
51
52 cout << i << ", ";
53
54 }
55
56 int
57 main ()
58 {
59
60 Pocket mynumbers1[] = { 3, 9, 0, 2, 1, 4, 5 };
61
62 Pocket mynumbers2[] = { 3, 8, 0, 1, 0, 2, 2 };
63
64 vector < Pocket > v1 (mynumbers1, mynumbers1 + 7);
65
66 vector < Pocket > v2 (mynumbers2, mynumbers2 + 7);
67
68 vector < Pocket > v3 (7, 0);
69
70 transform (v1.begin (), v1.end (), v2.begin (), v3.begin (), minus
71
72 for_each (v1.rbegin (), v1.rend (), printer);
73
74 return 0;
75
76 }
77
program outputs: 3, 2, 1, 1, 0, 1, 0,
program outputs: 0, 1, 0, 1, 1, 2, 3,
program outputs: 5, 4, 1, 2, 0, 9, 3,
"runtime error at LINE II" is incorrect because there is no runtime error at LINE II.
"compilation error in LINE II" is incorrect because there is no compilation error in LINE
II.
"compilation error in LINE I" is incorrect because there is no compilation error in LINE
I.
Question 18
What will happen when you attempt to compile and run the following code?
1 #include <vector>
2
3 #include <iostream>
4
5 #include <algorithm>
6
7 #include <functional>
8
9 using namespace std;
10
11 class Pocket
12 {
13
14 int value;
15
16 public:
17
18 Pocket (int value):value (value)
19 {
20 }
21
22 int getValue () const
23 {
24 return value;
25 }
26
27 operator int () const
28 {
29 return value;
30 }
31
32 bool operator < (const Pocket & _Right) const
33 {
34 return value < _Right.value;
35 }
36
37 };
38
39 ostream & operator << (ostream & stream, const Pocket & pocket)
40 {
41
42 stream << pocket.getValue ();
43
44 return stream;
45
46 }
47
48 void
49 printer (Pocket i)
50 { //LINE I
51
52 cout << i << ", ";
53
54 }
55
56 struct Add:public binary_function < Pocket, Pocket, Pocket >
57 {
58
59 Pocket operator () (const Pocket & _Left, const Pocket & _Right) co
60 {
61 return _Left + _Right;
62 }
63
64 };
65
66 int
67 main ()
68 {
69
70 Pocket mynumbers1[] = { 3, 9, 0, 2, 1, 4, 5 };
71
72 vector < Pocket > v1 (mynumbers1, mynumbers1 + 7);
73
74 vector < Pocket > v2 (7, 0);
75
76 transform (v1.begin (), v1.end (), v2.begin (), bind2nd (Add (), 1)
77
78 for_each (v2.rbegin (), v2.rend (), printer);
79
80 return 0;
81
82 }
83
program outputs: 5, 4, 1, 2, 0, 9, 3,
The correct answer is "program outputs: 6, 5, 2, 3, 1, 10, 4,". The code defines a
class Pocket with a constructor that initializes its value. It overloads the < operator and
the stream insertion operator << for Pocket. In the main function, it initializes an array
of Pocket objects mynumbers1 and creates a vector v1 from these objects. It creates
another vector v2 of Pocket objects, initialized with 7 elements, all set to 0. It defines
a struct Add that overloads the function call operator () to perform addition of two
Pocket objects. It uses transform with bind2nd(Add(), 1) to add 1 to each element of
v1 and store the result in v2. Finally, it prints the elements of v2 in reverse order using
for_each and the printer function. The transform function with bind2nd(Add(), 1) adds
1 to each element of v1 and stores the result in v2. So, for example, if v1 contains
elements {3, 9, 0, 2, 1, 4, 5}, then v2 will contain elements {4, 10, 1, 3, 2, 5, 6} after
the transformation.
"compilation error in LINE I" is incorrect because there is no compilation error in LINE
I.
"compilation error in LINE II" is incorrect because there is no compilation error in LINE
II.
"runtime error at LINE II" is incorrect because there is no runtime error at LINE II.
Question 19
What will happen when you attempt to compile and run the following code?
1 #include <vector>
2
3 #include <iostream>
4
https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 51/59
4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test
5 #include <algorithm>
6
7 #include <functional>
8
9 using namespace std;
10
11 class Pocket
12 {
13
14 int value;
15
16 public:
17
18 Pocket (int value):value (value)
19 {
20 }
21
22 int getValue () const
23 {
24 return value;
25 }
26
27 operator int () const
28 {
29 return value;
30 }
31
32 bool operator < (const Pocket & _Right) const
33 {
34 return value < _Right.value;
35 }
36
37 };
38
39 ostream & operator << (ostream & stream, const Pocket & pocket)
40 {
41
42 stream << pocket.getValue ();
43
44 return stream;
45
46 }
47
48 void
49 printer (Pocket i)
50 { //LINE I
51
52 cout << i << ", ";
53
54 }
55
56 template < typename T > struct Add:public binary_function < T, T, T >
57 { //LINE I
58
59 T operator () (const T & _Left, const T & _Right) const
60 {
61 return _Left + _Right;
62 }
63
64 };
65
66 int
67 main ()
68 {
69
70 Pocket mynumbers1[] = { 3, 9, 0, 2, 1, 4, 5 };
71
72 vector < Pocket > v1 (mynumbers1, mynumbers1 + 7);
73
74 vector < Pocket > v2 (7, 0);
75
76 transform (v1.begin (), v1.end (), v2.begin (), bind2nd (Add < Pock
77
78 for_each (v2.rbegin (), v2.rend (), printer);
79
80 return 0;
81
82 }
83
program outputs: 5, 4, 1, 2, 0, 9, 3,
"runtime error at LINE II" is incorrect because there is no runtime error at LINE II.
"compilation error in LINE I" is incorrect because there is no compilation error in LINE
I.
Question 20
What will happen when you attempt to compile and run the following code?
1 #include <vector>
2
3 #include <iostream>
4
5 #include <algorithm>
6
7 #include <functional>
8
9 using namespace std;
10
11 class Pocket
12 {
13
14 int value;
15
16 public:
17
18 Pocket (int value):value (value)
19 {
20 }
21
22 int getValue () const
https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 55/59
4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test
23 {
24 return value;
25 }
26
27 operator int () const
28 {
29 return value;
30 }
31
32 bool operator < (const Pocket & _Right) const
33 {
34 return value < _Right.value;
35 }
36
37 };
38
39 ostream & operator << (ostream & stream, const Pocket & pocket)
40 {
41
42 stream << pocket.getValue ();
43
44 return stream;
45
46 }
47
48 void
49 printer (Pocket i)
50 { //LINE I
51
52 cout << i << ", ";
53
54 }
55
56 template < typename T > struct Add:public binary_function < T, T, T >
57 { //LINE I
58
59 T operator () (const T & _Left, const T & _Right) const
60 {
61 return _Left + _Right;
62 }
63
64 };
65
66 int
67 main ()
68 {
69
70 Pocket mynumbers1[] = { 3, 9, 0, 2, 1, 4, 5 };
71
72 vector < Pocket > v1 (mynumbers1, mynumbers1 + 7);
73
74 vector < Pocket > v2 (7, 0);
75
76 transform (v1.begin (), v1.end (), v2.begin (), bind1st (ptr_fun (A
77
78 for_each (v2.rbegin (), v2.rend (), printer);
79
80 return 0;
81
82 }
83
program outputs: 5, 4, 1, 2, 0, 9, 3,
The correct answer is: "compilation error in LINE II". The code attempts to use bind1st
with ptr_fun to bind the first argument of the Add functor to 1. However, ptr_fun
expects a pointer to a function, not a function object like Add. This mismatch causes a
compilation error because ptr_fun cannot adapt the function object Add.
"compilation error in LINE I" is incorrect because there is no compilation error in LINE
I. LINE I defines a template struct Add, which is syntactically correct.
Reset
close
Review Assessment
0%
You have not passed the quiz this time. Select Reset to retake it.
Remember that you can return to the module for reference or to retake the quiz at any
time.