0% found this document useful (0 votes)
30 views66 pages

Cisco Networking Academy 2

Module 3 of the CPPA course focuses on non-modifying STL algorithms in C++. It includes a completion test with 28 questions to assess understanding of topics such as for_each, find, and count. Students must achieve a minimum score of 70% to pass the module test.

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)
30 views66 pages

Cisco Networking Academy 2

Module 3 of the CPPA course focuses on non-modifying STL algorithms in C++. It includes a completion test with 28 questions to assess understanding of topics such as for_each, find, and count. Students must achieve a minimum score of 70% to pass the module test.

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/ 66

4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1.

Module 3 Completion – Module Test

3.1 Module 3
Completion –
Module Test

Scroll to begin

3.1.1 Congratulations! You have


completed Module 3

Well done! You've reached the end of


Module 3 and completed a major

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 1/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 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 3:

Non-modifying STL operations:


for_each
find, find_if
find_end, search
find_first_of
adjacent_find
count, count_if
mismatch
equal
search_n

You are now ready to attempt the final


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

3.1.2 Module 3 Test


This assessment comprises twenty eight 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
https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 2/66
4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

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

1 #include <iostream>
2 #include <algorithm>
3 #include <vector>
4 #include <deque>
5 #include <sstream>
6 #include <set>
7 using namespace std;
8
9 void myprint(inti) {
10 cout << i << ", "; // Line I
11 }
12
13 int main() {
14
15 int mynumbers[] = {3, 9, 0, 2, 1, 4, 5};
16 vector<int> v1(mynumbers, mynumbers + 7);
17 set<int> s1(mynumbers, mynumbers + 7);
18 deque<int> d1(mynumbers, mynumbers + 7);
19 d1.pop_front(); // Line II
20 for_each(v1.begin(), v1.end(), myprint); // Line III
21 for_each(s1.begin(), s1.end(), myprint);
22 for_each(d1.begin(), d1.end(), myprint);
23 cout << endl; // Print newline
24 return 0;
25 }
26

the exception will be thrown at LINE II

the exception will be thrown at LINE III

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 3/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

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


2, 1, 4, 5,

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


2, 1, 4,

compilation error in LINE II

signal_cellular_4_bar
the program outputs 3, 9, 0, 2, 1, 4, 5, 0, 1, 2, 3, 4, 5, 9, 9, 0, 2,
done 1, 4, 5,

compilation error in LINE I

The correct answer is "the program outputs 3, 9, 0, 2, 1, 4, 5, 0, 1, 2, 3, 4, 5, 9, 9,


0, 2, 1, 4, 5," because the code provided initializes arrays and containers with valid
data. It creates a vector v1, a set s1, and a deque d1 with the elements from the
mynumbers array, which is syntactically correct and adheres to the rules of C++
syntax. Then it removes the first element from deque d1 using pop_front(), which is
also valid syntax. The code iterates over the elements of v1, s1, and d1 using
for_each() with a custom function myprint, which correctly prints each element
followed by a comma. Hence, the program outputs the elements of v1, s1, and d1
concatenated together, separated by commas, followed by a newline character.

"the exception will be thrown at LINE III" is incorrect because Line III iterates over the
elements of v1, and there is no logical or syntactical error in this line that would cause
an exception.
"compilation error in LINE I" is incorrect because Line I is syntactically valid, defining a
function myprint() that prints an integer.
he exception will be thrown at LINE II" is incorrect because Line II removes the first
element from d1 using pop_front(), which is a valid operation and should not cause an
exception.
"compilation error in LINE II" is incorrect because Line II is also syntactically valid,
removing the first element from d1.
https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 4/66
4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

"the program outputs 3, 9, 0, 2, 1, 4, 5, 0, 1, 2, 3, 4, 5, 9, 3, 9, 0, 2, 1, 4," is


incorrect because the actual output includes all the elements of v1, s1, and d1, which
are concatenated together and separated by commas.

Question 2

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

1 #include <iostream>
2 #include <algorithm>
3 #include <vector>
4 #include <deque>
5 #include <set>
6 using namespace std;
7
8 struct myprinter
9 {
10 void operator () (int i)
11 {
12 cout << i << ", ";
13 }
14 };
15
16 int
17 main ()
18 {
19 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
20 vector < int >v1 (mynumbers, mynumbers + 7);
21 deque < int >d1 (mynumbers, mynumbers + 7);
22 set < int >s1 (mynumbers, mynumbers + 7);
23 v1.pop_back (5); // Line I

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 5/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

24
25 for_each (s1.begin (), s1.end (), myprinter ()); // Line II
26
27 for_each (d1.begin (), d1.end (), *(new myprinter ())); // Line
28
29 for_each (v1.begin (), v1.end (), myprinter); //Line IV
30
31 return 0;
32
33 }
34

signal_cellular_4_bar
done compilation error in LINE IV

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


1, 4, 5, 3, 9, 0, 2, 1, 4,

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


1, 4, 5, 3, 9, 0, 2, 1, 4, 5, 5

compilation error in LINE III

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


1, 4, 5, 3, 9, 0, 2, 1, 4, 5, signal_cellular_4_bar
done compilation error in LINE I

compilation error in LINE II

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 6/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

"compilation error in LINE I" is correct because Line I attempts to use pop_back() with
an argument, but pop_back() does not accept arguments. This results in a compilation
error due to incorrect usage of the function. "compilation error in LINE IV'' is correct
because Line IV tries to pass the function myprinter to for_each() without instantiating
an object of the myprinter struct. This results in a compilation error because myprinter
is a struct, not a function.The incorrect answers are:

"compilation error in LINE III" because Line III correctly creates a temporary object of
myprinter using new, and then dereferences it to use the function call operator
operator(). This syntax is valid.
"compilation error in LINE II" because Line II correctly iterates over the elements of set
s1 and applies the function object myprinter to each element. This usage is
syntactically correct.
"the program outputs 3, 9, 0, 2, 1, 4, 5, 0, 1, 2, 3, 4, 5, 9, 9, 0, 2, 1, 4, 5, 3, 9, 0, 2,
1, 4, 5, 5" because the actual behavior is not defined due to compilation errors in
LINE I and LINE IV.
"the program outputs 3, 9, 0, 2, 1, 4, 5, 0, 1, 2, 3, 4, 5, 9, 9, 0, 2, 1, 4, 5, 3, 9, 0, 2,
1, 4, 5," because the actual behavior is not defined due to compilation errors in LINE I
and LINE IV.
"the program outputs 3, 9, 0, 2, 1, 4, 5, 0, 1, 2, 3, 4, 5, 9, 9, 0, 2, 1, 4, 5, 3, 9, 0, 2,
1, 4," because the actual behavior is not defined due to compilation errors in LINE I
and LINE IV.

Question 3

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

1 #include <iostream>
2 #include <algorithm>
3 #include <vector>
https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 7/66
4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

4 #include <deque>
5 #include <set>
6 using namespace std;
7
8 struct myprinter {
9 void operator () (int i) {
10 cout << i << ", ";
11 }
12 };
13
14 int main () {
15 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
16 vector < int >v1 (mynumbers, mynumbers + 7);
17 deque<int> d1(mynumbers, mynumbers + 7);
18 set<int> s1(mynumbers, mynumbers + 7);
19
20 d1.pop_front(); // Line I
21
22 for_each(d1.begin(), d1.end(), myprinter()); // Line III
23
24 for_each (d1.begin (), d1.end (), *(new myprinter ())); // Lin
25
26 for_each(v1.begin(), v1.end(), myprinter()); // Line IV
27
28 cout << endl; // Print newline
29 return 0;
30 }
31

compilation error in LINE III

compilation error in LINE II

the program outputs 6, 18, 0, 4, 2, 8, 10, 6, 18, 0, 4, 2, 8, 10,

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 8/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

signal_cellular_4_bar
close the exception will be thrown at LINE I

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


1, 4, 5,

the program outputs 6, 18, 0, 4, 2, 8, 10, 0, 2, 4, 6, 8, 10, 18,

"compilation error in LINE I" is correct because Line I attempts to use pop_back() with
an argument, but pop_back() does not accept arguments. This results in a compilation
error due to incorrect usage of the function. "compilation error in LINE IV'' is correct
because Line IV tries to pass the function myprinter to for_each() without instantiating
an object of the myprinter struct. This results in a compilation error because myprinter
is a struct, not a function.The incorrect answers are:

"compilation error in LINE III" because Line III correctly creates a temporary object of
myprinter using new, and then dereferences it to use the function call operator
operator(). This syntax is valid.
"compilation error in LINE II" because Line II correctly iterates over the elements of set
s1 and applies the function object myprinter to each element. This usage is
syntactically correct.
"the program outputs 3, 9, 0, 2, 1, 4, 5, 0, 1, 2, 3, 4, 5, 9, 9, 0, 2, 1, 4, 5, 3, 9, 0, 2,
1, 4, 5, 5" because the actual behavior is not defined due to compilation errors in
LINE I and LINE IV.
"the program outputs 3, 9, 0, 2, 1, 4, 5, 0, 1, 2, 3, 4, 5, 9, 9, 0, 2, 1, 4, 5, 3, 9, 0, 2,
1, 4, 5," because the actual behavior is not defined due to compilation errors in LINE I
and LINE IV.
"the program outputs 3, 9, 0, 2, 1, 4, 5, 0, 1, 2, 3, 4, 5, 9, 9, 0, 2, 1, 4, 5, 3, 9, 0, 2,
1, 4," because the actual behavior is not defined due to compilation errors in LINE I
and LINE IV.

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 9/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

Question 4

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

1 #include <iostream>
2 #include <algorithm>
3 #include <vector>
4 #include <deque>
5 #include <set>
6 using namespace std;
7
8 int
9 main ()
10 {
11
12 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
13
14 vector < int >v1 (mynumbers, mynumbers + 7);
15
16 deque < int >d1 (mynumbers, mynumbers + 7);
17
18 set < int >s1 (mynumbers, mynumbers + 7);
19
20 vector < int >::iterator found = find (v1.begin (), v1.end (), 3);
21
22 if (found != v1.end ()) //LINE II
23
24 cout << "found" << ", ";
25
26 cout << find (d1.begin (), d1.end (), 9) << ", "; //LINE III
27

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 10/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

28 cout << find (s1.begin (), s1.end (), 6); //LINE IV


29
30 return 0;
31
32 }
33

program outputs: 3, 9, 6

the exception will be thrown at LINE I

signal_cellular_4_bar
done compilation error in LINE III

program outputs: found, 9, 6

signal_cellular_4_bar
done compilation error in LINE IV

compilation error in LINE II

compilation error in LINE I

program outputs: found, 9,

The correct answers are "compilation error in LINE IV" and "compilation error in LINE
III". "compilation error in LINE IV" is correct because find() returns an iterator, and
printing it directly as done in LINE IV is not valid. It should be dereferenced to get the
value it points to before printing, or an appropriate check should be performed to
ensure it's not end() before dereferencing. "compilation error in LINE III" is correct
because find() returns an iterator, and printing it directly as done in LINE III is not valid.
https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 11/66
4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

It should be dereferenced to get the value it points to before printing, or an


appropriate check should be performed to ensure it's not end() before dereferencing.

"program outputs: found, 9," is incorrect because LINE III does not correctly print the
result of find() on d1. Additionally, there is no guarantee that the value 6 exists in set
s1, so the output "6" is not guaranteed.
"program outputs: found, 9, 6" is incorrect because there is no guarantee that the
value 6 exists in set s1, so the output "6" is not guaranteed.
"compilation error in LINE II" is incorrect because LINE II correctly checks if the value
3 is found in vector v1 using find(). The syntax is correct, and it should not cause any
compilation errors.
"program outputs: 3, 9, 6" is incorrect because there is no guarantee that the value 6
exists in set s1, so the output "6" is not guaranteed.
"compilation error in LINE I" is incorrect because LINE I correctly initializes the iterator
found with the result of find() applied to vector v1. The syntax is correct, and it should
not cause any compilation errors.
"the exception will be thrown at LINE I" is incorrect because LINE I correctly initializes
the iterator found with the result of find() applied to vector v1. There is no operation
that would cause an exception at this line.

Question 5

Which sentences are true about the code below?

1 #include <iostream>
2 #include <algorithm>
3 #include <vector>
4 using namespace std;
5
6 class A {
7 int a;

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 12/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

8 public:
9 A(int a) : a(a) {}
10 int getA() const { return a; }
11 void setA(int a) { this->a = a; }
12 bool operator ==(const A & b) const {return (this->a != b.a); }//
13 };
14
15 struct doubler { void operator()(A & a) { a.setA(a.getA() * 2); } };/
16
17 int main() {
18
19 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
20
21 vector<A> v1(mynumbers, mynumbers + 7);
22
23 for_each(v1.begin(), v1.end(), doubler());
24
25 vector<A>::iterator it = find(v1.begin(), v1.end(), A(7));
26
27 cout << it->getA() << endl;//LINE III
28
29 return 0;
30
31 }
32

it will compile and print 3

it will compile and print 14

it will compile and print 7

compilation error in LINE I

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 13/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

signal_cellular_4_bar
done it will compile and print 6

compilation error in LINE II

compilation error in LINE III

"it will compile and print 6" is correct because the code compiles successfully and
executes as expected. It doubles the values of elements in vector v1 using the doubler
functor. Then, it searches for an element with value 7 in vector v1 using find(), which
doesn't find any match and returns v1.end(). When dereferenced, v1.end() usually
prints 6, which is the result of doubling 3.

"compilation error in LINE I" is incorrect because LINE I correctly defines the equality
operator for class A, comparing the values of the 'a' member variables.
"compilation error in LINE III" is incorrect because LINE III correctly prints the value of
'a' for the element found by find(). However, it might not print 6 as expected due to
undefined behavior when dereferencing v1.end().
"it will compile and print 3" is incorrect because the code does not find an element
with value 7 in vector v1, so it won't print 3.
"compilation error in LINE II" is incorrect because LINE II correctly checks if the value
3 is found in vector v1 using find(). The syntax is correct, and it should not cause any
compilation errors.
"it will compile and print 14" is incorrect because the code does not find an element
with value 7 in vector v1, so it won't print 14.
"it will compile and print 7" is incorrect because the code does not find an element
with value 7 in vector v1, so it won't print 7.
"compilation error in LINE II" is incorrect because LINE II correctly defines a functor
doubler that doubles the value of the 'a' member variable for elements of class A in
vector v1.

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 14/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

Question 6

Which sentences are true about the code below? Choose all that apply.

1 #include <iostream>
2 #include <algorithm>
3 #include <vector>
4 using namespace std;
5
6 class Founder
7 {
8 public:
9 int val;
10 Founder (int v):val (v)
11 {
12 }
13 bool operator () (int v)
14 {
15 return (v == val);
16 } //LINE I
17 };
18
19 int
20 main ()
21 {
22 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
23 vector < int >v1 (mynumbers, mynumbers + 7);
24 if (find (v1.begin (), v1.end (), 7) ==
25 find (v1.begin (), v1.end (), Founder (7).val))
26 { //LINE II
27 cout << "Found!n";

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 15/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

28 }
29 else
30 {
31 cout << "Not found!n";
32 }
33 return 0;
34 }
35

signal_cellular_4_bar
done it will compile successfully

it will display Not found!

signal_cellular_4_bar
done it will display Found!

compilation error in LINE II

compilation error in LINE I

"it will compile successfully" is true because the code does not contain any syntax
errors and should compile without any issues."it will display Found!" is true because
the code searches for the value 7 in vector v1 using two different methods. The first
method uses find() directly with the value 7, and the second method uses a functor
Founder initialized with the value 7. Both methods should find the value 7 in the
vector, so the program will print "Found!".

"it will display Not found!" is incorrect because the code correctly finds the value 7 in
vector v1, so it will print "Found!" instead of "Not found!"
"compilation error in LINE I" is incorrect because LINE I defines the functor Founder
and its operator() correctly to compare the given value with the value stored in the val
member variable.
"compilation error in LINE II" is incorrect because LINE II correctly compares the
results of two find() operations, one directly with the value 7 and the other using the

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 16/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

functor Founder initialized with the value 7. The comparison is syntactically correct
and should not cause any compilation errors.

Question 7

What will happen when you attempt to compile and run the following code? Choose all
that apply.

1 #include <iostream>
2 #include <algorithm>
3 #include <vector>
4 #include <set>
5 using namespace std;
6
7 class A
8 {
9 int a;
10 public:
11 A (int a):a (a)
12 {
13 }
14 int getA () const
15 this->a = a;
16 }
17 bool operator < (const A & b) const
18 {
19 return a < b.a;
20 }
21 };
22
23 class Founder

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 17/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

24 {
25 }
26 bool operator () (A & v)
27 {
28 return (v.getA () == val.getA ());
29 }
30 };
31
32 int
33 main ()
34 {
35 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
36 vector < A > v1 (mynumbers, mynumbers + 7); //LINE I
37 set < A > s1 (mynumbers, mynumbers + 7);
38 A a (5);
39 Founder f (a);
40 find_if (s1.begin (), s1.end (), f.val); //LINE II
41 if (find_if (v1.begin (), v1.end (), f) != v1.end ())
42 { //LINE III
43 cout << "Found!n";
44 }
45 else
46 {
47 cout << "Not found!n";
48 }
49
50 return 0;
51 }
52

compilation error in LINE I

it will display Found!

compilation error in LINE II

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 18/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

signal_cellular_4_bar
done it will not compile successfully

it will compile successfully

it will display Not found!

compilation error in LINE III

compilation error in LINE I

The correct answer is "it will not compile successfully" because the code will not
compile successfully due to the following specific reasons. Invalid Argument to
find_if(): The compilation error arises from the incorrect usage of find_if() at LINE II.
The find_if() function expects a unary predicate as its third argument, but f.val is not a
valid unary predicate. It attempts to use a member variable directly as a predicate,
which is syntactically incorrect. Type Mismatch in find_if() Call: Even if the usage of
find_if() were corrected, the Founder functor's operator() expects an argument of type
A&, but find_if() operates on a set of A objects. Hence, there would be a type
mismatch when trying to pass elements from the set s1 to the Founder functor. These
specific issues result in a failure to compile the code successfully.

"compilation error in LINE I" is incorrect because there's no compilation error at LINE I.
The vector v1 is correctly initialized with elements of type A using iterators.
"it will display Found!" is incorrect because the code does not compile successfully,
so it won't produce any output.
"it will compile successfully" is incorrect because the code contains errors and won't
compile.
"compilation error in LINE II" is incorrect because the compilation error occurs due to
the incorrect usage of the find_if() function, but it's not specifically at LINE II.
The issue is with the usage of f.val inside find_if()."it will display Not found!" is
incorrect because the code does not compile successfully, so it won't produce any
output.

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 19/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

"compilation error in LINE III" is incorrect because there's no compilation error at LINE
III. The issue causing compilation failure is elsewhere in the code.

Question 8

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

1 #include <iostream>
2 #include <algorithm>
3 #include <vector>
4 using namespace std;
5
6 int
7 main ()
8 {
9 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
10 vector < int >v (mynumbers, mynumbers + 7);
11 vector < int >::iterator it;
12 int m1[] = { 9, 0, 2 };
13 it = find_end (v.begin (), v.end (), m1, m1 + 3); //LINE I
14 if (it != v.end ())
15 cout << "Found at position: " << it - v.begin () << endl;
16 return 0;
17 }
18

program outputs: Found at position: 9

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 20/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

compilation error in LINE I

signal_cellular_4_bar
done program outputs: Found at position: 1

program outputs: Found at position: 2

no output

"program outputs: Found at position: 1" because the code correctly searches for the
last occurrence of the subsequence {9, 0, 2} within the vector v. The subsequence is
found starting at index 1 of v, so it prints "Found at position: 1".

"no output" is incorrect because the code successfully finds the subsequence {9, 0, 2}
in vector v and prints the corresponding position.
"program outputs: Found at position: 9" is incorrect because the position of the found
subsequence is 1, not 9.
"program outputs: Found at position: 2" is incorrect because the position of the found
subsequence is 1, not
"compilation error in LINE I" is incorrect because LINE I correctly uses the find_end()
function to search for the subsequence within the vector v. There are no compilation
errors in this line.

Question 9

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 21/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

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

1 #include <iostream>
2 #include <algorithm>
3 #include <vector>
4 using namespace std;
5
6 class A
7 {
8 int a;
9
10 int getA () const
11 {
12 return a;
13 }
14
15 public:
16 A (int a):a (a)
17 {
18 }
19 bool operator== (A & b)
20 {
21 return a == b.a;
22 }
23 };
24
25 struct Comparer
26 {
27 bool operator () (const A & a, const A & b)
28 {
29 return a.getA () == b.getA ();
30 }; //LINE I
31 };
32
33 int
34 main ()
35 {

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 22/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

36 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
37 vector < A > v (mynumbers, mynumbers + 7);
38 vector < A >::iterator it;
39 A m1[] = { A (2), A (3), A (4) };
40 it = find_end (v.begin (), v.end (), m1, m1 + 3, Comparer ()); /
41 cout << "Found at position: " << it + v.begin () << endl; //LINE
42
43 return 0;
44 }
45

compilation error in LINE II

program outputs: Found at position: 2

program outputs: Found at position: 1

program outputs: Found at position: 0

program outputs: Found at position: 7

done compilation error in LINE III


signal_cellular_4_bar signal_cellular_4_bar
done compilation error in LINE I

The correct answer is "compilation error in LINE III" because LINE III tries to print the
result of adding an iterator it to v.begin(), which is not valid. The addition operation
between an iterator and the result of v.begin() is not defined. "compilation error in
LINE I" is correct because LINE I defines the operator() for the functor Comparer with
incorrect syntax. The semicolon after the function definition should be removed.
https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 23/66
4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

"program outputs: Found at position: 0" is incorrect because there will be a


compilation error, and the program won't produce any output.
"program outputs: Found at position: 2" is incorrect because there will be a
compilation error, and the program won't produce any output
"compilation error in LINE II" is incorrect because there is no compilation error at LINE
II. The usage of find_end() with the Comparer functor is syntactically correct.
"program outputs: Found at position: 1" is incorrect because there will be a
compilation error, and the program won't produce any output.
"program outputs: Found at position: 7" is incorrect because there will be a
compilation error, and the program won't produce any output.

Question 10

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

1 #include <iostream>
2 #include <algorithm>
3 #include <vector>
4 using namespace std;
5
6 int
7 main ()
8 {
9
10 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
11 vector < int >v (mynumbers, mynumbers + 7);
12 vector < int >::iterator it;
13 int m1[] = { 9, 0, 2 };
14 it = find_first_of (v.begin (), v.end (), m1, m1 + 3); //LINE I
15 cout << "First found at position: " << it - v.begin () << endl;
16 return 0;

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 24/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

17 }
18

the program outputs: First found at position: 0

the program outputs: First found at position: 2

compilation error in LINE I

the program outputs: First found at position: 9

compilation error in LINE II

signal_cellular_4_bar
done the program outputs: First found at position: 1

the exception will be thrown at line LINE II

The correct answer is "the program outputs: First found at position: 1" because the
find_first_of() function searches for the first occurrence of any of the elements in the
m1 array within the range defined by v.begin() and v.end(). In this case, the first
occurrence of an element from m1 within the vector v is the element 9 at position 1.
So, the program will output "First found at position: 1".

"compilation error in LINE I" is incorrect because there is no compilation error at LINE
I. The usage of find_first_of() with iterators and arrays is syntactically correct.
"the program outputs: First found at position: 0" is incorrect because the first
occurrence of an element from m1 within the vector v is the element 9 at position 1,
not 0.
"the program outputs: First found at position: 9" is incorrect because the position is
within the range of v, and the position index starts from 0.

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 25/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

"the program outputs: First found at position: 2" is incorrect because the first
occurrence of an element from m1 within the vector v is the element 9 at position 1,
not 2.
"the exception will be thrown at line LINE II" is incorrectbecause there is no reason for
an exception to be thrown at LINE II. The operation performed is valid and should
execute without any issues.
"compilation error in LINE II" is incorrect because there is nocompilation error at LINE
II. The subtraction operation between iterators is valid, and the output operation is
syntactically correct.

Question 11

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

1 #include <iostream>
2 #include <algorithm>
3 #include <vector>
4 using namespace std;
5
6 bool compare (int a, int b)
7 {
8 return a == b;
9 }
10
11 int
12 main ()
13 {
14 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
15 vector < int >v (mynumbers, mynumbers + 7);
16 vector < int >::iterator it = v.begin ();
17 int m1[] = { 9, 0, 2 };

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 26/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

18
19 while ((it = find_first_of (it, v.end (), m1, m1 + 3)) != v.end ())
20 { //LINE I
21 cout << it - v.begin () << ", "; //LINE II
22 it++;
23 }
24 cout << endl;
25 return 0;
26 }
27

the exception will be thrown at LINE I

program will run forever

program outputs: 9, 0, 2,

compilation error in LINE II

signal_cellular_4_bar
done program outputs: 1, 2, 3,

compilation error in LINE I

The correct answer is "program outputs: 1, 2, 3," because the code correctly iterates
through the vector v using the find_first_of() function within a while loop. The
find_first_of() function searches for the first occurrence of any element from the m1
array within the range defined by the current iterator it and v.end(). Once an
occurrence is found, the iterator it is updated to point to thefoundelement, and its
position is printed. Then, the iterator it is incremented to continue searching forthe
next occurrence. This process repeats until the end of the vector v. The found
positions are printed as"1, 2, 3,".

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 27/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

"the exception will be thrown at LINE I" is incorrect because there is no reason foran
exception to be thrown at LINE I. The assignment and comparison operations within
the while loop arevalid.
"program will run forever" is incorrect because the loop will terminate when the
iterator itreaches the end of the vector v.
"compilation error in LINE II" is incorrect because there is nocompilation error at LINE
II. The subtraction operation between iterators and the output operation are both
syntactically correct.
"program outputs: 9, 0, 2," is incorrect because the found positions are 1, 2, and 3,
not 9, 0, and 2.
"compilation error in LINE I" is incorrect because there is no compilation error at LINE
I. The usage of find_first_of() with iterators and arrays is syntactically correct.

Question 12

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

1 #include <iostream>
2 #include <algorithm>
3 #include <vector>
4 using namespace std;
5
6 int
7 main ()
8 {
9
10 int mynumbers[] = { 3, 9, 0, 2, 2, 2, 5 };
11
12 vector < int >v (mynumbers, mynumbers + 7);
13 vector < int >::iterator it = v.begin ();

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 28/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

14
15 while ((it = adjacent_find (it, v.end ())) != v.end ())
16 { //LINE I
17 cout << it - v.begin () << ", ";
18 it--; //LINE II
19 }
20
21 cout << endl;
22 return 0;
23 }
24

program outputs: 2, 3,

compilation error in LINE II

program outputs: 3, 4,

compilation error in LINE I

signal_cellular_4_bar
done program will run forever

program outputs: 2, 2, 2,

program outputs: 5, 6,

The correct answer is "program will run forever" because the loop condition (it =
adjacent_find(it, v.end())) != v.end() searches for adjacent elements that are equal in
the vector v. Once found, it decrements the iterator it (LINE II) to print the index of the
first of the adjacent elements found. However, if there are adjacent elements that are

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 29/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

equal at the end of the vector v, the loop will continuously find them, and
decrementing the iterator it will never reach the end of the vector. This causes the
loop to run indefinitely.

"program outputs: 2, 2, 2," is incorrect because the program will run forever, and it
won't produce any output.
"program outputs: 5, 6," is incorrect because the program will run forever, and it
won't produce any output.
"compilation error in LINE I" is incorrect because there is no compilation error at LINE
I. The usage of adjacent_find() with iterators is syntactically correct.
"compilation error in LINE II" is incorrect because there is no compilation error at LINE
II. The decrement operation on the iterator it is syntactically correct.
"program outputs: 3, 4," is incorrect because the program will run forever, and it
won't produce any output.
"program outputs: 2, 3," is incorrect because the program will run forever, and it
won't produce any output.

Question 13

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

1 #include <iostream>
2 #include <algorithm>
3 #include <vector>
4 using namespace std;
5
6 class A
7 {
8 int a;
9
10 public:
11
https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 30/66
4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

12 A (int a):a (a)


13 {
14 }
15 while ((it = adjacent_find (it, v.end ())) != v.end ())
16 int getA () const
17 {
18 return a;
19 }
20 void setA (int a)
21 {
22 this->a = a;
23 }
24 bool operator== (const A & b) const
25 {
26 return a == b.a;
27 }
28 };
29
30 bool
31 compare (const A & a, const A & b)
32 {
33 return a == b;
34 }
35
36 int
37 main ()
38 {
39
40 int mynumbers[] = { 3, 9, 0, 2, 2, 2, 5 };
41 vector < A > v (mynumbers, mynumbers + 7);
42 vector < A >::iterator it = v.begin ();
43
44 while ((it = adjacent_find (it, v.end (), compare)) != v.end ())
45 { //LINE I
46 cout << it - v.begin () << ", ";
47 it++; //LINE II
48 }
49
50 cout << endl;
51 return 0;

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 31/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

52 }
53

the program outputs 2, 2, 2,

compilation error in LINE II

signal_cellular_4_bar
done the program outputs 3, 4,

program will run forever

the exception will be thrown at LINE II

the program outputs 4, 5,

compilation error in LINE I

the program outputs 2, 3,

The correct answer is "the program outputs 3, 4," because the loop condition (it =
adjacent_find(it, v.end(), compare)) != v.end() searches for adjacent elements in the
vector v that are equal according to the custom comparison function compare. When
it finds such adjacent elements, it prints the index of the first of these elements and
increments the iterator it. This process continues until no more adjacent equal
elements are found.

"the program outputs 2, 3," is incorrect because the output will be "3, 4," as
explained in the correct answer.
"the program outputs 4, 5," is incorrect because the output will be "3, 4," as
explained in the correct answer.
https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 32/66
4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

"the exception will be thrown at LINE II" is incorrect because there is no reason for an
exception to be thrown at LINE II. The operation performed is valid and should execute
without any issues.
"compilation error in LINE I" is incorrect because there is no compilation error at LINE
I.
The usage of adjacent_find() with iterators and a custom comparison function is
syntactically correct.
"compilation error in LINE II" is incorrect because there is no compilation error at LINE
II. The increment operation on the iterator it is syntactically correct.
"the program outputs 2, 2, 2," is incorrect because the output will be "3, 4," as
explained in the correct answer.
"program will run forever" is incorrect because the loop will terminate after printing
"3, 4," and it won't run forever.

Question 14

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

1 #include <iostream>
2 #include <algorithm>
3 #include <vector>
4 using namespace std;
5
6 int
7 main ()
8 {
9 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };
10 vector < int >v (mynumbers, mynumbers + 12);
11
12 int found = count (v.begin (), v.end (), 6); //LINE I
13 cout << found << endl;
https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 33/66
4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

14 return 0;
15 }
16

the program outputs 1

the exception will be thrown at LINE I

compilation error in LINE I

signal_cellular_4_bar
done the program outputs 2

program executes and outputs nothing

the program outputs 3

the program outputs 4

The correct answer is "the program outputs 2" because the count() function counts
the occurrences of the value 6 in the vector v. As there are two occurrences of 6 in the
vector, the program will output "2".

"the program outputs 1" is incorrect because there are actually two occurrences of
the value 6 in the vector v.
"the program outputs 3" is incorrect because there are only two occurrences of the
value 6 in the vector v.
"the exception will be thrown at LINE I" is incorrect because there is no reason for an
exception to be thrown at LINE I. The usage of count() function with iterators is
syntactically correct.
"compilation error in LINE I" is incorrect because there is no compilation error at LINE
I. The usage of count() function with iterators is syntactically correct.
https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 34/66
4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

"program executes and outputs nothing" is incorrect because the program will output
the count of occurrences of the value 6, which is "2".
"the program outputs 4" is incorrect because there are only two occurrences of the
value 6 in the vector v.

Question 15

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

1 #include <iostream>
2 #include <algorithm>
3 #include <deque>
4 using namespace std;
5
6 class A
7 {
8 int a;
9 public:
10
11 A (int a):a (a)
12 {
13 }
14
15 int getA () const
16 public:
17
18 A (int a):a (a)
19 {
20 }
21

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 35/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

22 int getA () const


23 {
24 this->a = a;
25 }
26 };
27
28 int
29 main ()
30 {
31 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };
32 deque < int >d (mynumbers, mynumbers + 12);
33 int count = count (d.begin (), d.end (), 6); //LINE I
34 cout << count << endl;
35 return 0;
36 }
37

signal_cellular_4_bar
done compilation error in LINE I

the program outputs 3

the program outputs 4

the exception will be thrown at LINE I

the program outputs 1

program executes and outputs nothing

the program outputs 2

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 36/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

The correct answer is "compilation error in LINE I" because there is a naming conflict
in the code. The variable count is declared both as a variable name and as a function
name (std::count), resulting in a compilation error.

"the program outputs 1", "the program outputs 2", "the program outputs 3", and "the
program outputs 4" are all incorrect because none of these outputs will occur due to
the compilation error.
"the exception will be thrown at LINE I" is incorrect because the issue is a compilation
error, not a runtime exception.
"program executes and outputs nothing" is also incorrect because the compilation
error prevents the program from executing.

Question 16

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

1 #include <iostream>
2 #include <algorithm>
3 #include <vector>
4 using namespace std;
5
6 struct Compare
7 {
8 bool operator () (int a)
9 {
10 return (a > 0);
11 } //LINE I
12 };
13
14 int
https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 37/66
4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

15 main ()
16 {
17 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };
18 vector < int >v (mynumbers, mynumbers + 12);
19
20 int count = std::count (v.begin (), v.end (), Compare ()); //LIN
21 cout << count << endl;
22 return 0;
23 }
24

the exception will be thrown at LINE I

the program outputs 1

the program outputs 2

signal_cellular_4_bar
done compilation error in LINE II

the program outputs 4

the program outputs 3

program executes and outputs nothing

compilation error in LINE I

The correct answer is "compilation error in LINE II" because there is no overload of
std::count that accepts a unary predicate functor (Compare). The count algorithm
https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 38/66
4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

expects a value to search for, not a predicate. Hence, this line causes a compilation
error.

"compilation error in LINE I" is incorrect because there is no compilation error at LINE
I. The definition of the functor Compare is syntactically correct.
"the program outputs 3", "the program outputs 2", "the program outputs 4", and "the
program outputs 1" are incorrect because none of these outputs will occur due to the
compilation error.
"the exception will be thrown at LINE I" is incorrect because the issue is a compilation
error, not a runtime exception.
"program executes and outputs nothing" is incorrect because the compilation error
prevents the program from executing.

Question 17

What happens if you try to compile and run this program?

1 #include <iostream>
2 #include <algorithm>
3 #include <set>
4 using namespace std;
5
6 class A
7 {
8 int a;
9 public:
10 A (int a):a (a)
11 {
12 }
13

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 39/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

14 int getA () const


15 {
16 return a;
17 }
18 void setA (int a)
19 {
20 this->a = a;
21 }
22
23 bool operator < (const A & b) const
24 {
25 return a < b.a;
26 } //LINE I
27 };
28
29 struct Compare
30 {
31 bool operator () (A a)
32 {
33 return (a.getA () < 6);
34 }
35 };
36 main ()
37 {
38
39 int
40 main ()
41 {
42 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };
43 set < A > d (mynumbers, mynumbers + 12);
44 int count = count_if (d.begin (), d.end (), Compare ()); //LINE
45 cout << count << endl;
46 return 0;
47 }
48

the program outputs 2

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 40/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

compilation error in LINE II

signal_cellular_4_bar
done the program outputs 6

the program outputs 1

the program outputs 4

compilation error in LINE I

the exception will be thrown at LINE I

The correct answer is "the program outputs 6" because it counts the number of
elements in the set d for which the condition specified in the Compare functor holds
true. The Compare functor checks if the value of a obtained from the set element
using getA() is less than 6. Since there are six elements in the set with values less than
6, the output is 6.

"the program outputs 2" and "the program outputs 1" are incorrect because the
correct output is 6, not 2 or 1.
"compilation error in LINE I" and "the exception will be thrown at LINE I" are incorrect
because there are no issues with the implementation at LINE I. The comparison
operator overload is correctly defined.
"compilation error in LINE II" is incorrect because there is no compilation error at LINE
II. The usage of count_if() function with iterators and a custom functor is syntactically
correct.
"the program outputs 4" is incorrect because the correct output is 6,not 4.

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 41/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

Question 18

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

1 #include <iostream>
2 #include <algorithm>
3 #include <set>
4 using namespace std;
5
6 struct Pair
7 {
8 bool operator () (int a)
9 {
10 return (a % 2) != 0; //LINE I
11 }
12 };
13
14 int
15 main ()
16 {
17 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };
18 set < int >s (mynumbers, mynumbers + 12);
19
20 int count = count_if (s.begin (), s.end (), Pair ()); //LINE II
21 cout << count << endl;
22 return 0;
23 }
24

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 42/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

signal_cellular_4_bar
done the program outputs 4

the program outputs 2

compilation error in LINE I

the exception will be thrown at LINE II

the program outputs 3

the program outputs 1

compilation error in LINE II

"the program outputs 4" is correct because the program counts the number of
elements in the set s that satisfy the condition specified in the Pair functor. The Pair
functor checks if the element is odd (not divisible by 2). Since there are four odd
numbers in the set (3, 9, 1, and 5), the output is indeed 4.

"the exception will be thrown at LINE II" is incorrect because there are no exceptions
thrown at LINE II. The count_if() function with iterators and a custom functor is
syntactically correct, and there is no reason for an exception to be thrown here.
"the program outputs 2" is incorrect because the program actually outputs 4. The Pair
functor checks if each element in the set is odd (not divisible by 2). There are four odd
numbers in the set (3, 9, 1, and 5), so the correct output is 4.
"the program outputs 1" is incorrect because the program actually outputs 4. The Pair
functor checks if each element in the set is odd (not divisible by 2). There are four odd
numbers in the set (3, 9, 1, and 5), so the correct output is 4.
"the program outputs 3" is incorrect because the program actually outputs 4. The Pair
functor checks if each element in the set is odd (not divisible by 2). There are four odd

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 43/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

numbers in the set (3, 9, 1, and 5), so the correct output is 4.


"compilation error in LINE II" is incorrect because the usage of count_if() function with
iterators and a custom functor is syntactically correct. There is no reason for a
compilation error at LINE II.
"compilation error in LINE I" is incorrect because the functor Pair is correctly defined
with the operator() overload. The logic within the functor checks if a number is odd
(not divisible by 2), which is correctly implemented. There is no reason for a
compilation error at LINE I.

Question 19

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

1 #include <iostream>
2 #include <algorithm>
3 #include <vector>
4 #include <set>
5 using namespace std;
6
7 int
8 main ()
9 {
10 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };
11 vector < int >v1 (mynumbers, mynumbers + 12);
12 set < int >s1 (mynumbers, mynumbers + 12);
13 v1.push_back (10); //LINE I
14 pair < set < int >::iterator, vector < int >::iterator > resultSet
15 cout << *resultSet.first << ", " << *resultSet.second << endl; /
16
17 return 0;
18 }

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 44/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

19

compilation error in LINE I

the program outputs 0, 10

compilation error in LINE II

the program outputs 0, 0

compilation error in LINE III

the program outputs 10, 10

the exception will be thrown at LINE II

the program outputs 0, 3

"the program outputs 0, 3": This is correct because the mismatch function compares
elements in the s1 set with elements in the v1 vector until the first mismatch is found.
Since the elements in both containers match until the index where 0 is present, the
output will be the value at the iterator position pointing to the mismatch in the set (0)
and the value at the iterator position pointing to the mismatch in the vector
(3).Incorrect answers:

"compilation error in LINE III": This is incorrect because there is no compilation error
at LINE III. The code correctly dereferences the iterators and prints the values pointed
to by them.
"compilation error in LINE II": This is incorrect because there is no compilation error at

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 45/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

LINE II. The usage of the mismatch function with iterators from both the set and the
vector is syntactically correct.
"the exception will be thrown at LINE II": This is incorrect. The mismatch function will
not throw an exception in this scenario. It's a standard algorithm that compares
elements from two sequences until they no longer match or until one of the sequences
ends.
"the program outputs 0, 10": This is incorrect. The mismatch function returns iterators
to the first mismatching elements, and in this case, the mismatch occurs at 0 in the
set and 3 in the vector, not at 0 and 10.
"compilation error in LINE I": This is incorrect. There is no compilation error at LINE I.
The push_back function is used correctly to add an element to the end of the vector.
"the program outputs 0, 0": This is incorrect because the output will be 0, 3. There is
a mismatch between the set and the vector at these positions.
"the program outputs 10, 10": This is incorrect because the output will be 0, 3. There
is a mismatch between the set and the vector at these positions.

Question 20

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

1 #include <iostream>
2 #include <algorithm>
3 #include <deque>
4 #include <vector>
5 using namespace std;
6
7 bool
8 identical (int a, int b)
9 {
10 return b == a; //LINE I
11 }

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 46/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

12
13 int
14 main ()
15 {
16 {
17 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };
18 int othernumbers[] = { 3, 9, 0, 3, 1, 4, 3, 6, 6, 9, 8, 3 };
19 vector < int >v1 (mynumbers, mynumbers + 12);
20 deque < int >d1 (othernumbers, othernumbers + 12);
21
22 pair < deque < int >::iterator, vector < int >::iterator > result;
23 result = mismatch (d1.begin (), d1.end (), v1.begin (), identical);
24 if (result.first == d1.end () && result.second == v1.end ())
25 cout << "Identical";
26 else
27 cout << "Not identical";
28 return 0;
29 }
30

the exception will be thrown at LINE III

the program outputs Identical

the program outputs Not identical

compilation error in LINE I

compilation error in LINE III

compilation error in LINE II

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 47/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

"the program outputs Not identical" is correct because the mismatch function
compares elements from the deque d1 and the vector v1 until the first pair of
elements that are not identical according to the identical function is found. Since the
elements in the two containers differ at some positions, the program outputs "Not
identical".

"compilation error in LINE III" is correct because there is no compilation error at LINE
III. The mismatch function is used correctly with the provided custom comparison
function.
"compilation error in LINE I": This is incorrect. There is no compilation error at LINE I.
The identical function is defined correctly.
he program outputs Identical" is correct because the mismatch function finds a
mismatch between the elements of the deque and the vector, the output will be "Not
identical".
"the exception will be thrown at LINE III"is correct because there will be no exception
thrown at LINE III. The mismatch function will execute without any issues as long as
the iterators passed to it are valid.
"compilation error in LINE II" is correct because there is no compilation error at LINE II.
The declaration of the pair of iterators is syntactically correct.

Question 21

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

1 #include <iostream>
2 #include <algorithm>
3 #include <vector>
4 #include <set>
5 using namespace std;
6
https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 48/66
4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

7 int
8 main ()
9 {
10 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };
11 vector < int >v1 (mynumbers, mynumbers + 12);
12 set < int >s1 (mynumbers, mynumbers + 12);
13 v1.push_back (10);
14 pair < set < int >::iterator, vector < int >::iterator > resultSet
15 cout << *resultSet.first << ", " << *resultSet.second << endl; /
16
17 return 0;
18 }
19

the program outputs 4

the program outputs 1

the program outputs 2

compilation error in LINE I

compilation error in LINE II

the exception will be thrown at LINE I

the program outputs 6

"compilation error in LINE I" is correct because the std::equal algorithm does not
return a pair of iterators like std::mismatch. Instead, it returns a boolean value
https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 49/66
4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

indicating whether the two ranges are equal. Therefore, trying to assign the result to a
pair::iterator, vector::iterator> will result in a compilation error because the types do
not match.Incorrect answers:

"the exception will be thrown at LINE I" is incorrect because the program fails to
compile due to a type mismatch in the assignment of the std::equal result. The error
occurs during compilation, not during program execution, so no exception is thrown.
"compilation error in LINE I" is incorrect because the compilation error actually occurs
in LINE I, not LINE II. The error arises because std::equal does not return a
pair::iterator, vector::iterator>, causing a type mismatch in the assignment.
"the program outputs 6" is incorrect because the program fails to compile. Since
there's a compilation error, the program does not produce any output.
"compilation error in LINE II" is incorrect because there's no compilation error in LINE
II. The compilation error occurs in LINE I due to the incorrect usage of std::equal.
"the program outputs 1", "the program outputs 2", "the program outputs 4" are
incorrect because the program fails to compile. Without successful compilation, there
is no executable produced, and thus no output is generated.

Question 22

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

1 #include <iostream>
2 #include <algorithm>
3 #include <set>
4 using namespace std;
5
6 class A
7 {
8 int a;
9 public:
https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 50/66
4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

10 A (int a):a (a)


11 {
12 }
13
14 operator int () const
15 {
16 return a;
17 } //LINE I
18
19 int
20 main ()
21 {
22 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };
23 set < A > s (mynumbers, mynumbers + 12);
24 cout << equal (s.begin (), s.end (), s.begin ()) << endl; //LINE
25
26 return 0;
27 }
28

the program outputs 0

compilation error in LINE II

compilation error in LINE I

the program outputs 1

the program outputs true

the exception will be thrown at LINE I

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 51/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

The correct answer is "the program outputs 1" because the code successfully
compiles and executes without any errors. Here's a breakdown. LINE I: Defines a
conversion operator operator int(), which allows objects of class A to be implicitly
converted to int. This conversion operator is defined correctly and does not cause any
compilation errors. LINE II: Invokes the std::equal algorithm to compare the elements
of the set s with itself. The std::equal algorithm returns true if the elements in the
range [s.begin(), s.end()) are pairwise equal. In this case, since all elements of the set
are compared with themselves, the result will be true, which is represented as 1 when
output. Therefore, the code executes successfully, and the output "1" indicates that
the elements of the set s are equal to itself, as expected.

"compilation error in LINE I" is incorrect because LINE I defines a conversion operator,
which is syntactically valid and does not result in a compilation error.
"the program outputs true" is incorrect because the output of the program will be the
integer 1, not the boolean true. The std::equal algorithm returns an integer value of 1
to represent true.
"compilation error in LINE II" is incorrect because LINE II invokes the std::equal
algorithm, which is a valid use of the algorithm and does not result in a compilation
error.
"the program outputs 0" is incorrect because the program outputs 1, not 0, indicating
that the elements of the set s are equal to itself.
"the exception will be thrown at LINE I" is incorrect because there is no reason for an
exception to be thrown at LINE I. The conversion operator is defined correctly and
does not cause any runtime exceptions.

Question 23

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

1 #include <iostream>
https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 52/66
4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

2 #include <algorithm>
3 #include <vector>
4 using namespace std;
5
6 int
7 main ()
8 {
9 public:
10 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
11 vector < int >v (mynumbers, mynumbers + 7);
12 vector < int >::iterator it;
13 int m1[] = { 9, 0, 2 };
14 it = search (v.begin (), v.end (), m1, m1 + 3); //LINE I
15 cout << "found at position: " << it - v.begin () << endl; //LINE
16 return 0;
17 }
18

the program outputs 2

the exception will be thrown at LINE I

compilation error in LINE II

compilation error in LINE I

the program outputs 1

the program outputs 0

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 53/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

The correct answer is "the program outputs 1" because the code successfully
compiles and executes without any errors. LINE I: Invokes the std::search algorithm to
search for the sequence defined by m1 within the range [v.begin(), v.end()). Since the
sequence {9, 0, 2} is found starting at position 1 within the vector v, the iterator points
to the beginning of this sequence. LINE II: Prints the position of the found sequence by
subtracting the iterator it from v.begin(), which results in 1. Therefore, the output
"found at position: 1" indicates that the sequence {9, 0, 2} is found starting at position
1 within the vector v.

"the exception will be thrown at LINE I" is incorrect because there is no reason for an
exception to be thrown at LINE I. The std::search algorithm is used correctly, and
there are no errors in its usage.
"compilation error in LINE II" is incorrect because LINE II is syntactically correct and
prints the position of the found sequence. There are no compilation errors in this line.
"compilation error in LINE I" is incorrect because LINE I is also syntactically correct. It
invokes the std::search algorithm with the correct arguments, and there are no
compilation errors in this line.
"the program outputs 0" is incorrect because the program actually outputs 1,
indicating that the sequence {9, 0, 2} is found starting at position 1 within the vector v.
"the program outputs 2" is incorrect because the program actually outputs 1, not 2,
indicating that the sequence {9, 0, 2} is found starting at position 1 within the vector v.

Question 24

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

1 #include <iostream>
2 #include <algorithm>
3 #include <vector>
4 using namespace std;
https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 54/66
4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

5
6 class A
7 {
8 int a;
9 public:
10 A (int a):a (a)
11 {
12 }
13 int getA () const
14 {
15 return a;
16 }
17 void setA (int a)
18 {
19 this->a = a;
20 }
21
22 bool operator== (A & b)
23 {
24 return a == b.a;
25 }
26 };
27
28 }
29 struct Compare
30 {
31 bool operator () (const A & a, const A & b)
32 {
33 return a.getA () == b.getA ();
34 }
35 };
36
37 int
38 main ()
39 {
40 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
41 vector < A > v (mynumbers, mynumbers + 7);
42 vector < A >::iterator it;
43 A m1[] = { A (2), A (3), A (4) };
44 it = search (v.begin (), v.end (), m1, m1 + 3, Compare ()); //LI

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 55/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

45 cout << "First found at position: " << it - v.begin () << endl;
46 return 0;
47 }
48

the program outputs 6

the program outputs 5

the program outputs 7

compilation error in LINE I

compilation error in LINE II

the program outputs 8

The correct answer is "the program outputs 7" because the code successfully
compiles and executes without any errors. LINE I: Invokes the std::search algorithm to
search for the sequence defined by m1 within the range [v.begin(), v.end()) using the
custom comparator Compare. The comparator Compare compares A objects based
on their getA() values. LINE II: Prints the position of the first occurrence of the found
sequence by subtracting the iterator it from v.begin(), which results in 7. Therefore,
the output "First found at position: 7" indicates that the sequence {A(2), A(3), A(4)} is
found starting at position 7 within the vector v.

"the program outputs 6" is incorrect because the program actually outputs 7,
indicating that the sequence {A(2), A(3), A(4)} is found starting at position 7 within the
vector v.
"compilation error in LINE II" is incorrect because LINE II is syntactically correct and
prints the position of the found sequence. There are no compilation errors in this line.

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 56/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

"the program outputs 8" is incorrect because the program actually outputs 7, not 8,
indicating that the sequence {A(2), A(3), A(4)} is found starting at position 7 within the
vector v.
"the program outputs 5" is incorrect because the program actually outputs 7, not 5,
indicating that the sequence {A(2), A(3), A(4)} is found starting at position 7 within the
vector v.
"compilation error in LINE I" is incorrect because LINE I is also syntactically correct. It
invokes the std::search algorithm with the correct arguments and a custom
comparator Compare, and there are no compilation errors in this line.

Question 25

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

1 #include <iostream>
2 #include <algorithm>
3 #include <vector>
4 using namespace std;
5
6 int
7 main ()
8 {
9 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };
10 vector < int >v (mynumbers, mynumbers + 12);
11
12 vector < int >::iterator it = search_n (v.begin (), v.end (), 2, 1)
13 cout << it - v.begin () << endl; //LINE II
14 return 0;
15 }
16

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 57/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

the program outputs 5

the program outputs 3

the exception will be thrown at LINE I

the program outputs 4

the program outputs 12

compilation error in LINE II

compilation error in LINE I

The correct answer is "the program outputs 12" because the code successfully
compiles and executes without any errors. LINE I: Invokes the std::search_n algorithm
to search for a subsequence of 2 consecutive occurrences of the value 1 within the
range [v.begin(), v.end()). In this case, the entire vector v contains two consecutive
occurrences of 1, so it returns an iterator pointing to the position of the first
occurrence. LINE II: Prints the position of the iterator relative to the beginning of the
vector v. Since it points to the end of the vector, subtracting v.begin() from it yields the
size of the vector, which is 12.

"the program outputs 3" is incorrect because the program actually outputs 12,
indicating that the iterator it points to the end of the vector v.
"compilation error in LINE I" is incorrect because LINE I is syntactically correct. It
invokes the std::search_n algorithm with the correct arguments, and there are no
compilation errors in this line.
"compilation error in LINE II" is incorrect because LINE II is also syntactically correct. It
prints the position of the iterator relative to the beginning of the vector v, and there are

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 58/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

no compilation errors in this line.


"the exception will be thrown at LINE I" is incorrect because there are no exceptional
conditions in the code that would cause an exception to be thrown at LINE I.
"the program outputs 5" and "the program outputs 4" are incorrect because the
program actually outputs 12, indicating that the iterator it points to the end of the
vector v.

Question 26

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

1 #include <iostream>
2 #include <algorithm>
3 #include <deque>
4 using namespace std;
5
6 class A
7 {
8 int a;
9 public:
10 A (int a):a (a)
11 {
12 }
13 int getA () const
14 {
15 return a;
16 }
17 void setA (int a)
18 {
19 this->a = a;
20 }

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 59/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

21 };
22
23 struct Equals
24 {
25 bool operator () (const A & a, const A & b)
26 {
27 return (a.getA () == b.getA ());
28 } //LINE I
29 };
30
31 }
32 int
33 main ()
34 {
35 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };
36 deque < int >d (mynumbers, mynumbers + 12);;
37 deque < int >::iterator it = search_n (d.begin (), d.end (), 2, 1,
38 cout << it - d.begin () << endl; //LINE III
39 return 0;
40 }
41

compilation error in LINE II

the program outputs 11

compilation error in LINE III

the program outputs 3

the program outputs 12

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 60/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

compilation error in LINE I

the exception will be thrown at LINE I

the program outputs 4

The correct answer is "the program outputs 12" because the code successfully
compiles and executes without any errors. LINE I: Defines a functor Equals with an
overloaded operator() that compares two A objects based on their getA() values. LINE
II: Invokes the std::search_n algorithm to search for a subsequence of 2 consecutive
occurrences of the value 1 within the deque d. It uses the Equals functor to perform
the comparison. LINE III: Prints the position of the iterator it relative to the beginning of
the deque d. Since it points to the end of the deque, subtracting d.begin() from it
yields the size of the deque, which is 12.

"the program outputs 11" is incorrect because the program actually outputs 12,
indicating that the iterator it points to the end of the deque d.
"compilation error in LINE III" is incorrect because LINE III is syntactically correct. It
prints the position of the iterator relative to the beginning of the deque d, and there
are no compilation errors in this line.
"the program outputs 3" is incorrect because the program actually outputs 12,
indicating that the iterator it points to the end of the deque d.
"compilation error in LINE I" is incorrect because LINE I is syntactically correct. It
defines the operator() function for the Equals functor, and there are no compilation
errors in this line.
"the program outputs 4" is incorrect because the program actually outputs 12,
indicating that the iterator it points to the end of the deque d.
"compilation error in LINE II" is incorrect because LINE II is syntactically correct. It
invokes the std::search_n algorithm with the correct arguments, and there are no
compilation errors in this line.
"the exception will be thrown at LINE I" is incorrect because there are no exceptional
conditions in the code that would cause an exception to be thrown at LINE I.

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 61/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

Question 27

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

1 #include <iostream>
2 #include <algorithm>
3 #include <map>
4 using namespace std;
5
6 int
7 main ()
8 {
9 int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
10 map < int, int >m;
11 for (int i = 0; i < 7; i++)
12 {
13 m[i] = mynumbers[i];
14 }
15
16 pair < const int, int >p (4, 1);
17 map < int, int >::iterator it = find (m.begin (), m.end (), p);
18 if (it != m.end ())
19 cout << it->first << endl;
20 else
21 cout << "Not found!n";
22 return 0;
23 }
24

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 62/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

the program outputs Not found!

the exception will be thrown at LINE I

the program outputs 5

compilation error in LINE I

the program outputs 4

the program outputs 1

The correct answer is "the program outputs 4" because the code successfully finds
the pair (4, 1) in the map m. The for loop populates the map m with elements from the
mynumbers array, where the key is the index i and the value is mynumbers[i]. The
code then creates a pair p with key 4 and value 1. LINE I: Invokes the std::find
algorithm to search for the pair p within the map m. Since the find algorithm does not
directly support searching for pairs in maps, it will not compile successfully.

"the exception will be thrown at LINE I" is incorrect because there are no exceptional
conditions in the code that would cause an exception to be thrown at LINE I. The issue
here is a compilation error due to incorrect usage of std::find.
"compilation error in LINE I" is the correct answer. The find algorithm cannot be used
directly to search for pairs in maps. Instead, you should use m.find(p.first) to search
for the key p.first in the map m.
"the program outputs 5" is incorrect because the code would not compile successfully
due to the compilation error in LINE I.
"the program outputs 1" is incorrect because the code would not compile successfully
due to the compilation error in LINE I.

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 63/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

"the program outputs Not found!" is incorrect because the code would not compile
successfully due to the compilation error in LINE I.

Question 28

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

the program outputs 0, 1, 2, 3, 4, 5, 6,

the program outputs 0

size of the map is 7

the exception will be thrown at LINE I

compilation error in LINE II

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

size of the map is 8

the program outputs 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 64/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test

the exception will be thrown at LINE II

size of the map is 6

The correct answer is "the program outputs 0, 1, 2, 3, 4, 5, 6," and "size of the map
is 7." This is because the code successfully populates the map m with elements from
the mynumbers array, and then iterates over each element using for_each and myprint
function, printing each key of the map. The loop runs for each key-value pair in the
map, resulting in printing each key from 0 to 6.

"the exception will be thrown at LINE II" is incorrect because there are no exceptional
conditions in the code that would cause an exception to be thrown at LINE II.
"size of the map is 6" is incorrect because the map contains 7 elements, not 6. It
stores elements for keys 0 through 6, inclusive.
"the program outputs 0, 1, 2, 3, 4, 5, 6, 7, 8, 9," is incorrect because the code would
not output numbers beyond 6 since the map only contains elements for keys 0 through
6.
"the program outputs 0" is incorrect because the code would output multiple
numbers, not just 0.
"size of the map is 8" is incorrect because the map contains 7 elements, not 8.
"the program outputs 0, 1, 2, 3, 4, 5, 9," is incorrect because the code would not
output 9 since the map only contains elements for keys 0 through 6.
"compilation error in LINE II" is incorrect because the usage of for_each with myprint
is syntactically correct

You've submitted your answers!

Reset
close

https://w w w .netacad.com/launch?id=e84392f3-61ce-476b-83df-cc923cd33bba&tab=curriculum&view =99214201-fc90-5073-bc04-c7698ae0b27c 65/66


4/3/25, 9:30 AM CPPA Module 3: Non-modifying STL Algorithms | 3.1. Module 3 Completion – Module Test
Review Assessment

61%

You've scored 61%.

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 =99214201-fc90-5073-bc04-c7698ae0b27c 66/66

You might also like