Here is a listing of C++ 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
View Answer
Explanation: C++ Standard Library, algorithms are components that perform algorithmic operations on containers and other sequences. For this operation, We have to use <algorithm> header file.
2. Pick out the correct method in the c++ standard library algorithm.
a) mismatch
b) maximum
c) minimum
d) maxmatch
View Answer
Explanation: 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
View Answer
Explanation: 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 <iostream>
#include <algorithm>
#include <vector>
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
View Answer
Explanation: In this program, We used the union function to find the number of elements.
Output:
$ g++ sla.cpp $ a.out 8
5. What will be the output of the following C++ code?
#include <iostream>
#include <algorithm>
#include <vector>
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
View Answer
Explanation: 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
6. What will be the output of the following C++ code?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main ()
{
vector<int> myvector;
for (int i = 1; i < 6; ++i)
myvector.push_back(i);
reverse(myvector.begin(), myvector.end());
for (vector<int> :: iterator it = myvector.begin(); it != myvector.end(); ++it)
cout << ' ' << *it;
return 0;
}
a) 1 2 3 4 5
b) 5 4 3 2 1
c) 0 1 2 3 4
d) 5 4 1 2 3
View Answer
Explanation: In this program, We reversed the vector values by using the reverse function.
Output:
$ g++ sla2.cpp $ a.out 5 4 3 2 1
7. What will be the output of the following C++ code?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main ()
{
int myints[] = {10, 20, 30, 30, 20, 10, 10, 20};
int mycount = count (myints, myints + 8, 10);
cout << "10 appears " << mycount << " times.\n";
vector<int> myvector (myints, myints+8);
mycount = count (myvector.begin(), myvector.end(), 20);
cout << "20 appears " << mycount << " times.\n";
return 0;
}
a) 3 3
b) 3 1
c) 8
d) 9
View Answer
Explanation: In this program, We are counting the number of 10’s and 20’s in the myints.
Output:
$ g++ sla3.cpp $ a.out 10 appears 3 times 20 appears 3 times
8. What will be the output of the following C++ code?
#include <iostream>
#include <algorithm>
using namespace std;
int main ()
{
int myints[] = {10, 20, 30, 30, 20, 10, 10, 20};
int* pbegin = myints;
int* pend = myints + sizeof(myints) / sizeof(int);
pend = remove (pbegin, pend, 20);
for (int* p = pbegin; p != pend; ++p)
cout << ' ' << *p;
return 0;
}
a) 10, 20, 30, 30, 20, 10, 10, 20
b) 10, 30, 30, 10, 10
c) 10, 20, 20, 10, 10, 10, 20
d) 10, 20, 20, 10, 30, 10, 15
View Answer
Explanation: In this program, We are removing all the 20’s and then we are
printing the remaining.
Output:
$ g++ sla4.cpp $ a.out 10, 30, 30, 10, 10
9. What is the type of the first item in the heap?
a) Bigger than others
b) Lower than others
c) Mean value of the heap
d) Equal to others
View Answer
Explanation: In C++, when we say heap we mean max heap and first element of max is bigger than others.
10. Pick out the correct library in the following choices.
a) Search
b) Generate
c) Numeric
d) All of the mentioned
View Answer
Explanation: These are the available libraries in C++.
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Apply for C++ Internship
- Check Computer Science Books
- Practice Computer Science MCQs
- Check C++ Books
- Practice Programming MCQs