C++ Programming Questions and Answers – Standard Library Algorithms

This section on C++ interview questions and answers focuses on “Standard Library Algorithms”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in 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 C++ interview questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.

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

Answer: b
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

Answer: a
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

Answer: c
Explanation: It is used to rearranges a range so that it becomes a heap.
advertisement
advertisement

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

  1.     #include <iostream>
  2.     #include <algorithm>
  3.     #include <vector>
  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
View Answer

Answer: c
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?

advertisement
  1.     #include <iostream>
  2.     #include <algorithm>
  3.     #include <vector>
  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
View Answer

Answer: d
Explanation: In this program, We filled out the vector values by using criteria in the for loop.
Output:

advertisement
$ g++ sla1.cpp
$ a.out
3 4 4 0

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

  1.     #include <iostream>
  2.     #include <algorithm>
  3.     #include <vector>
  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
View Answer

Answer: b
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?

  1.     #include <iostream>
  2.     #include <algorithm>
  3.     #include <vector>
  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
View Answer

Answer: a
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?

  1.     #include <iostream> 
  2.     #include <algorithm>
  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
View Answer

Answer: b
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

Answer: a
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

Answer: d
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.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.