0% found this document useful (0 votes)
10 views59 pages

Cisco Networking Academy 5

Module 7 of the CPPA course covers STL utilities and functional programming in C++, including arithmetic and comparison functional operators, and functions like bind1st and bind2nd. After completing the module, students are required to take a test consisting of twenty questions, with a passing score of 70%. The document also includes examples of code and expected outputs to help students understand the concepts better.

Uploaded by

Jr Cialana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views59 pages

Cisco Networking Academy 5

Module 7 of the CPPA course covers STL utilities and functional programming in C++, including arithmetic and comparison functional operators, and functions like bind1st and bind2nd. After completing the module, students are required to take a test consisting of twenty questions, with a passing score of 70%. The document also includes examples of code and expected outputs to help students understand the concepts better.

Uploaded by

Jr Cialana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1.

Module 7 Completion – Module Test

7.1 Module 7
Completion –
Module Test

Scroll to begin

7.1.1 Congratulations! You have


completed Module 7

Well done! You've reached the end of


Module 7 and completed a major

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 1/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

milestone in your programming education


and learning the fundamentals of C++.
Here's a short summary of the topics
you've covered in Module 7:

Arithmetic functional operators


Comparison operators
bind1st function
bind2nd function
ptr_fun function

You are now ready to attempt the final


challenge: Module 7 Test, which will help
you gauge what you've learned so far.

7.1.2 Module 7 Test


This assessment comprises twenty questions derived from the topics you've recently
studied. To succeed, you'll need to secure a minimum score of 70%.

All the best on your test!

Question 1

What will happen when you attempt to compile and run the following code?

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 2/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 3/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

signal_cellular_4_bar
close program outputs: 6, 9, 4, 5,

program outputs: 3, 9, 0, 2, 6, 1, 4, 5,

compilation error in LINE I

runtime error at LINE II

runtime error at LINE I

program outputs: 4, 10, 1, 3, 7, 2, 5, 6 ,

program outputs: 9, 10, 4, 7,

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

"program outputs: 3, 9, 0, 2, 6, 1, 4, 5," is incorrect because the elements of v1 are


not the concatenation of mynumbers1 and mynumbers2. They are the result of adding
corresponding elements from mynumbers1 and mynumbers2 in reverse order.

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

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 4/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

"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,

compilation error in LINE I

program outputs: 4, 10, 1, 3, 2, 5, 6,

program outputs: 5, 4, 1, 2, 0, 9, 3,

runtime error at LINE II

runtime error at LINE I

The correct answer is "program outputs: 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 for_each function to iterate over each element of v1 and
applies the binary operation plus() with the second argument fixed as 1 (LINE I). This
operation adds 1 to each element of v1. It iterates over the elements of v1 in reverse
order using for_each and prints each element to the console (LINE II).

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 6/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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

"program outputs: 4, 10, 1, 3, 2, 5, 6," is incorrect because the elements of v1 are


not the result of adding 1 to each element of mynumbers. The correct output is "5, 4,
1, 2, 0, 9, 3,".

"program outputs: 3, 9, 0, 2, 1, 4, 5," is incorrect because the elements of v1 are


modified by adding 1 to each element, not the original elements of mynumbers.

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

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 (plus < int
28
29 v1.push_back (counter); //LINE II
30
31 for_each (v1.rbegin (), v1.rend (), printer);
32
33 return 0;
34
35 }
36

runtime error at LINE I

program outputs: 4, 10, 1, 3, 2, 5, 6,

runtime error at LINE II

program outputs: 0, 5, 4, 1, 2, 0, 9, 3,

program outputs: 3, 9, 0, 2, 1, 4, 5,

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 8/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

program outputs: 7, 5, 4, 1, 2, 0, 9, 3,

compilation error in LINE I

The correct answer is "program outputs: 7, 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 greater than 4 (LINE I). It binds the first argument of the
plus() binary function to 4, effectively checking if each element is greater than 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 I" and "compilation error in LINE I" are incorrect because there
are no compilation or runtime errors in the provided code.

"program outputs: 3, 9, 0, 2, 1, 4, 5," is incorrect because the count of elements


greater than 4 (counter) is added to the end of v1, and the output is in reverse order.

"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?

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 9/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 10/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

compilation error in LINE I

runtime error at LINE I

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,

runtime error at LINE II

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.

"program outputs: 7, 5, 4, 1, 2, 0, 9, 3," is incorrect because the count counter is


added to the end of v1, and the output is in reverse order.

"runtime error at LINE II" is incorrect because there are no runtime errors in the
provided code.

"program outputs: 7, 3, 9, 0, 2, 1, 4, 5," is incorrect because the output should


include the count counter at the end, not the elements of v1.

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 11/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

"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

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 12/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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: 6, -9, 2, -1, 3, 1, -2,

runtime error at LINE II

runtime error at LINE I

program outputs: 2, -5, 1, 0, -1, 5, -2,

program outputs: 2, 5, 1, 0, 1, 5, 2,

compilation error in LINE I

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

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 13/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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.

"program outputs: 2, 5, 1, 0, 1, 5, 2," is incorrect because the subtraction operation


is performed between corresponding elements of v1 and v2, resulting in the correct
output provided in the correct answer.

"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

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 14/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

9 using namespace std;


10
11 struct Add:public binary_function < int, int, int >
12 { //LINE I
13
14 int operator () (const int &_Left, const int &_Right) const
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

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 15/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

program outputs: 4, 3, 0, 1, -1, 8, 2,

compilation error in LINE I

runtime error at LINE II

runtime error at LINE I

program outputs: 4, 10, 1, 3, 2, 5, 6,

program outputs: 6, 5, 2, 3, 1, 10, 4,

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.

"program outputs: 4, 10, 1, 3, 2, 5, 6," and "program outputs: 6, 5, 2, 3, 1, 10, 4,"


are incorrect because the correct output is the result of adding -1 to each element of
v1, as provided in the correct answer.

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 16/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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 }

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 17/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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

compilation error in LINE II

program outputs: 4, 10, 1, 3, 2, 5, 6,

program outputs: 6, 5, 2, 3, 1, 10, 4,

runtime error at LINE II

program outputs: 4, 3, 0, 1, -1, 8, 2,

compilation error in LINE I

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 18/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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.

"program outputs: 6, 5, 2, 3, 1, 10, 4," and "program outputs: 4, 10, 1, 3, 2, 5, 6,"


are incorrect because the code does not compile. Therefore, there is no program
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>

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 19/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 20/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

runtime error at LINE II

program outputs: 4, 3, 0, 1, -1, 8, 2,

compilation error in LINE II

compilation error in LINE I

program outputs: 4, 10, 1, 3, 2, 5, 6,

program outputs: 6, 5, 2, 3, 1, 10, 4,

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.

"program outputs: 4, 10, 1, 3, 2, 5, 6," is incorrect because there is a compilation


error, and the program does not produce any output.

"runtime error at LINE II" is incorrect because there is no runtime error. The issue is
with the compilation due to a syntax error.

"program outputs: 4, 3, 0, 1, -1, 8, 2," and "program outputs: 6, 5, 2, 3, 1, 10, 4,"


are incorrect because the code does not compile. Therefore, there is no program
output.

"compilation error in LINE I" is incorrect because the issue is in LINE II, not LINE I.

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 21/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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 };

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 22/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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

compilation error in LINE II

program outputs: 5, 11, 2, 4, 3, 6, 7,

runtime error at LINE II

compilation error in LINE I

program outputs: 12, 10, 4, 6, 2, 20, 8,

program outputs: 8, 20, 2, 6, 4, 10, 12,

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 23/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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.

"program outputs: 5, 11, 2, 4, 3, 6, 7," is incorrect because the expected output is


the result of applying MultiAdd to each element of d1.

"compilation error in LINE I" is incorrect because there is no compilation error in LINE
I. The code compiles successfully.

"runtime error at LINE II" is incorrect because there is no runtime error.

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

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 25/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

compilation error in LINE I

program outputs: 3, 9, 0, 2, 1, 4, 5,

program outputs: 6, 5, 2, 3, 1, 10, 4,

compilation error in LINE II

runtime error at LINE II

program outputs: 4, 10, 1, 3, 2, 5, 6,

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.

"program outputs: 3, 9, 0, 2, 1, 4, 5," is incorrect because there is a compilation


error.

"program outputs: 6, 5, 2, 3, 1, 10, 4," is incorrect because there is a compilation


error.

"compilation error in LINE I" is incorrect because there is no compilation error in LINE
I. The code for Add is correctly defined.

"program outputs: 4, 10, 1, 3, 2, 5, 6," is incorrect because there is a compilation


error.

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 26/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

"runtime error at LINE II" is incorrect because there is no runtime error.

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);

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 27/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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

undefined behavior at LINE III could be runtime error or 0

runtime error at LINE II

program outputs: 0

compilation error in LINE I

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.

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 28/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" 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

15 transform(v1.begin(), v1.end(), v2.begin(), [](int val) {


16 if (val == 3)
17 return 6;
18 else
19 return 2 * val;
20 }); // LINE I
21
22 vector<int>::iterator it = find(v2.begin(), v2.end(), 6); // Usin
23 if (it != v2.end()) {
24 cout << *it << endl; // LINE III
25 } else {
26 cout << "6 not found" << endl;
27 }
28
29 return 0;
30
31 }
32

program outputs: 3

program outputs: 6 6

program outputs: 6

runtime error at LINE I

compilation error in LINE I

program outputs: 0

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 30/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

runtime error at LINE II

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?

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 31/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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

program outputs: 5, 10, 4, 7, 0

runtime error at LINE II

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 33/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

program outputs: 5, 10, 4, 7,

runtime error at LINE I

compilation error in LINE I

program outputs: 7, 4, 10, 5, 0

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.

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 34/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 35/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 36/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

68 return 0;
69
70 }
71

program outputs: 4, 10, 1, 3, 2, 5, 6,

compilation error in LINE II

program outputs: 6, 5, 2, 3, 1, 10, 4, 0,

runtime error at LINE II

compilation error in LINE I

program outputs: 6, 5, 2, 3, 1, 10, 4,

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.

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 37/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

"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;

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 39/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

60
61 }
62

program outputs: 6, 5, 2, 3, 1, 10, 4, 0,

compilation error in LINE I

compilation error in LINE II

program outputs: 6, 5, 2, 3, 1, 10, 4,

program outputs: 5, 4, 1, 2, 0, 9, 3,

runtime error at LINE II

The correct answer is "program outputs: 5, 4, 1, 2, 0, 9, 3,". 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
for_each with bind1st(plus(), 1) to add 1 to each element of v1. Finally, it prints the
elements of v1 in reverse order using for_each and the printer function. The for_each
function with bind1st(plus(), 1) adds 1 to each element of v1. So, the output is: 5 (3 +
1), 4 (9 + 1), 1 (0 + 1), 2 (2 + 1), 0 (1 + 1), 9 (4 + 1), and 3 (5 + 1).

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

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 40/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

"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

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 41/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 42/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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

compilation error in LINE II

program outputs: 0, 1, 0, 1, 1, 2, 3,

program outputs: 5, 4, 1, 2, 0, 9, 3,

compilation error in LINE I

program outputs: 3, 2, 1, 1, 0, 1, 0,

runtime error at LINE II

The correct answer is "program outputs: 5, 4, 1, 2, 0, 9, 3,". The code defines a


class Pocket with a constructor that initializes its value. It overloads the < operator and

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 43/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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.

"program outputs: 0, 1, 0, 1, 1, 2, 3," is incorrect because it shows the incorrect


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.

"runtime error at LINE II" is incorrect because there is no runtime error at LINE II.

"program outputs: 3, 2, 1, 1, 0, 1, 0," is incorrect because it shows the incorrect


output.

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

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 45/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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,

runtime error at LINE II

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 46/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, 1, 0, 1, 1, 2, 3,

program outputs: 5, 4, 1, 2, 0, 9, 3,

compilation error in LINE I

compilation error in LINE II

The correct answer is "program outputs: 5, 4, 1, 2, 0, 9, 3,". 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, 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.

"program outputs: 0, 1, 0, 1, 1, 2, 3," is incorrect because it shows the incorrect


output.

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

"program outputs: 3, 2, 1, 1, 0, 1, 0," is incorrect because it shows the incorrect


output.

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 47/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 48/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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 ()

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 49/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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

compilation error in LINE II

program outputs: 5, 4, 1, 2, 0, 9, 3,

runtime error at LINE II

compilation error in LINE I

program outputs: 6, 5, 2, 3, 1, 10, 4,

program outputs: 6, 5, 2, 3, 1, 10, 4, 0,

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

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 50/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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.

"program outputs: 6, 5, 2, 3, 1, 10, 4, 0," is incorrect because it shows an additional


element, which is not present in the correct output.

"runtime error at LINE II" is incorrect because there is no runtime error at LINE II.

"program outputs: 5, 4, 1, 2, 0, 9, 3," is incorrect because it shows the incorrect


output.

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;

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 52/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 53/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

runtime error at LINE II

program outputs: 5, 4, 1, 2, 0, 9, 3,

compilation error in LINE I

program outputs: 6, 5, 2, 3, 1, 10, 4,

runtime error at LINE II

program outputs: 6, 5, 2, 3, 1, 10, 4, 0,

The correct answer is "program outputs: 5, 4, 1, 2, 0, 9, 3,". The code defines a


class Pocket with member functions and overloaded operators. It also defines an
overload for the << operator to print Pocket objects. In the main function, it creates a
vector v1 of Pocket objects initialized with elements from the array mynumbers1. It
creates another vector v2 of Pocket objects, initialized with 7 elements, all set to 0. It
defines a template struct Add that overloads the function call operator () to perform
addition of two objects of type T. It uses transform with bind2nd(Add(), 0) to add 0 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(), 0) adds 0 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 {3,
9, 0, 2, 1, 4, 5} after the transformation, as adding 0 doesn't change the value of each
element.

"program outputs: 6, 5, 2, 3, 1, 10, 4," is incorrect because it shows the incorrect


output.

"runtime error at LINE II" is incorrect because there is no runtime error at LINE II.

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 54/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

"compilation error in LINE I" is incorrect because there is no compilation error in LINE
I.

"program outputs: 6, 5, 2, 3, 1, 10, 4, 0," is incorrect because it shows an additional


element, which is not present in the correct output.

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 }

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 56/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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: 6, 5, 2, 3, 1, 10, 4,

program outputs: 6, 5, 2, 3, 1, 10, 4, 0,

compilation error in LINE I

compilation error in LINE II

program outputs: 5, 4, 1, 2, 0, 9, 3,

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 57/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

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.

"program outputs: 6, 5, 2, 3, 1, 10, 4," is incorrect because the code doesn't


compile, so there is no program output.

"program outputs: 6, 5, 2, 3, 1, 10, 4, 0," is incorrect because the code doesn't


compile, so there is no program output.

"program outputs: 5, 4, 1, 2, 0, 9, 3," is incorrect because the code doesn't compile,


so there is no program output.

You've submitted your answers!

Reset
close

Review Assessment

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 58/59


4/3/25, 10:04 PM CPPA Module 7: STL Utilities and Functional Library | 7.1. Module 7 Completion – Module Test

0%

You've scored 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.

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =5c4461d0-edef-5220-8a5d-d8fcaee346cf 59/59

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy