250+ TOP MCQs on Standard Library Algorithms and Answers

This section on online C++ test on “Standard Library Algorithms”. One shall practice these test questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our online C++ test questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

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 header file.

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?

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     int main ()
  6.     {
  7.         int first[] = {5, 10, 15, 20, 25};
  8.         int second[] = {50, 40, 30, 20, 10};
  9.         vector<int> v(10);      
  10.         vector<int> :: iterator it;
  11.         sort (first, first + 5);   
  12.         sort (second, second + 5); 
  13.         it = set_union (first, first + 5, second, second + 5, v.begin());
  14.         cout << int(it - v.begin());
  15.         return 0;
  16.     }

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?

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     int main () 
  6.     {
  7.         vector<int> myvector (4);
  8.         fill (myvector.begin(), myvector.begin() + 2, 3);
  9.         fill (myvector.begin() + 1, myvector.end() - 1, 4);
  10.         for (vector<int> :: iterator it = myvector.begin(); it != myvector.end(); ++it)
  11.             cout << ' ' << *it;
  12.         return 0;
  13.     }

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

6. What will be the output of the following C++ code?

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     int main () 
  6.     {
  7.         vector<int> myvector;
  8.         for (int i = 1; i < 6; ++i) 
  9.             myvector.push_back(i);
  10.         reverse(myvector.begin(), myvector.end());
  11.         for (vector<int> :: iterator it = myvector.begin(); it != myvector.end(); ++it)
  12.             cout << ' ' << *it;
  13.         return 0;
  14.     }

a) 1 2 3 4 5
b) 5 4 3 2 1
c) 0 1 2 3 4
d) 5 4 1 2 3
Answer: b
Clarification: 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?

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     int main () 
  6.     {
  7.         int myints[] = {10, 20, 30, 30, 20, 10, 10, 20};
  8.         int mycount = count (myints, myints + 8, 10);
  9.         cout << "10 appears " << mycount << " times.n";
  10.         vector<int> myvector (myints, myints+8);
  11.         mycount = count (myvector.begin(), myvector.end(), 20);
  12.         cout << "20 appears " << mycount  << " times.n";
  13.         return 0;
  14.     }

a) 3 3
b) 3 1
c) 8
d) 9
Answer: a
Clarification: 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?

  1.     #include  
  2.     #include 
  3.     using namespace std;
  4.     int main () 
  5.     {
  6.         int myints[] = {10, 20, 30, 30, 20, 10, 10, 20};  
  7.         int* pbegin = myints;                      
  8.         int* pend = myints + sizeof(myints) / sizeof(int);
  9.         pend = remove (pbegin, pend, 20);      
  10.         for (int* p = pbegin; p != pend; ++p)
  11.             cout << ' ' << *p;
  12.         return 0;
  13.     }

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
Answer: b
Clarification: 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
Answer: a
Clarification: 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
Answer: d
Clarification: These are the available libraries in C++.

Leave a Reply

Your email address will not be published. Required fields are marked *