Here is a listing of online C++ test questions on “Standard Library Algorithms” along with answers, explanations and/or solutions:
1. What is the header file used for declaring the standard library algorithms?
a) container
b) algorithm
c) library
d) iterator
Answer: b
Clarification: C++ Standard Library, algorithms are components that perform algorithmic operations on containers and other sequences. For this operation, We have to use
2. Pick out the correct method in the c++ standard library algorithm.
a) mismatch
b) maximum
c) minimum
d) maxmatch
Answer: a
Clarification: It is a method in the search operation in standard library algorithms.
3. What is the use of make_heap in the heap operation?
a) Rearrange a heap
b) Deform a heap
c) Form a heap
d) Delete a heap
Answer: c
Clarification: It is used to rearranges a range so that it becomes a heap.
4. What will be the output of the following C++ code?
-
#include
-
#include
-
#include
-
using namespace std;
-
int main ()
-
{
-
int first[] = {5, 10, 15, 20, 25};
-
int second[] = {50, 40, 30, 20, 10};
-
vector<int> v(10);
-
vector<int> :: iterator it;
-
sort (first, first + 5);
-
sort (second, second + 5);
-
it = set_union (first, first + 5, second, second + 5, v.begin());
-
cout << int(it - v.begin());
-
return 0;
-
}
a) 6
b) 7
c) 8
d) 9
Answer: c
Clarification: In this program, We used the union function to find the number of elements.
Output:
5. What will be the output of the following C++ code?
-
#include
-
#include
-
#include
-
using namespace std;
-
int main ()
-
{
-
vector<int> myvector (4);
-
fill (myvector.begin(), myvector.begin() + 2, 3);
-
fill (myvector.begin() + 1, myvector.end() - 1, 4);
-
for (vector<int> :: iterator it = myvector.begin(); it != myvector.end(); ++it)
-
cout << ' ' << *it;
-
return 0;
-
}
a) 3 4
b) 3 4 4
c) 3 4 & 3 4 4
d) 3 4 4 0
Answer: d
Clarification: In this program, We filled out the vector values by using criteria in the for loop.
Output:
$ g++ sla1.cpp $ a.out 3 4 4 0