C++ Programming Questions and Answers – STL Algorithms

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “STL Algorithms”.

1. Which of the header file is used to implement algorithms provided by C++ STL?
a) <algorithm>
b) <header>
c) <algos>
d) <Algorithm>
View Answer

Answer: a
Explanation: <algorithm> header is provided by the C++ to use STL algorithms.

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

#include <vector> 
#include <algorithm> 
#include <iostream> 
 
using namespace std;
 
int main() 
{ 
	vector<int> v(10, 2); 
	if (all_of(v.cbegin(), v.cend(), [](int i){ return i % 2 == 0; })) 
	{ 
		cout << "All numbers are even\n"; 
	} 
	else
        {
		cout << "All numbers are not even\n"; 
	}
	return 0;
}

a) All numbers are even
b) All numbers are not even
c) Error
d) Segmentation fault
View Answer

Answer: a
Explanation: In this program, we are using all_of() function to check whether all the members of the vector are even or not. As all the numbers in a vector are 10, therefore, the program output “All numbers are even”.
Output:

$ ./a.out 
All numbers are even
advertisement
advertisement

3. How many types of sequence operations are provided by the C++ algorithm STL?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: b
Explanation: There are two main types of sequence operations are provided by the C++ algorithm STL namely Non-modifying sequence operations and Modifying sequence operations.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

4. Which of the following is a Modifying Sequence Operation?
a) all_of()
b) any_of()
c) equal()
d) swap()
View Answer

Answer: d
Explanation: swap() is a Modifying sequence operation. One can observe from the name itself as equal, all_of and any_of are by name states that they will be used for comparison whereas swap is trying to modify the sequence by changing locations of a sequence.

5. Which of the following is a Non-modifying Sequence Operation?
a) swap()
b) transform()
c) remove()
d) search()
View Answer

Answer: d
Explanation: search() is Non-modifying sequence operation because while searching we never change anything whereas swapping, transforming and removing involves modifying the sequence in some way.
advertisement

6. What is the use of random_shuffle() function of STL algorithm?
a) To generate the random sequence in a range
b) To generate a sequence in a given range and arrange them in random order
c) To rearrange given sequence randomly
d) To select any random number from the given sequence.
View Answer

Answer: c
Explanation: random_shuffle() function is used to re-arrange a given sequence of elements in a range.

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

advertisement
#include <vector> 
#include <algorithm> 
#include <iostream> 
 
using namespace std;
 
int main() 
{ 
	vector<int> v; 
	for(int i=0;i<10;i++)
		v.push_back(i+1);
	for(int i=0;i<10;i++)
		cout<<v[i]<<" ";
	cout<<endl;
	random_shuffle(v.begin(), v.end());
	for(int i=0;i<10;i++)
		cout<<v[i]<<" ";
	return 0;
}

a)

1 2 3 4 5 6 7 8 9 10 
5 4 8 9 1 6 3 2 7 10

b)

5 4 8 9 1 6 3 2 7 10
1 2 3 4 5 6 7 8 9 10 

c)

1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 

d)

5 4 8 9 1 6 3 2 7 10
5 4 8 9 1 6 3 2 7 10
View Answer
Answer: a
Explanation: In this program we have made a vector og numbers from 1-10 and then applied random_shuffle() function on that sequence of numbers, hence after after applying the function the sequence is arranged in a random sequence which is printed. The order of numbers in second sequence may vary when executed several times.
Output:

$ ./a.out 
1 2 3 4 5 6 7 8 9 10 
5 4 8 9 1 6 3 2 7 10
 
 

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

#include <iostream>  
#include <algorithm> 
#include <vector> 
 
using namespace std;
 
bool IsOdd (int i) 
{ 
	return (i%2)==1; 
}
 
int main () 
{
  vector<int> v;
 
  for (int i=1; i<=10; ++i) 
  	v.push_back(i);
 
  vector<int>::iterator bound;
  bound = partition (v.begin(), v.end(), IsOdd);
 
  for (vector<int>::iterator it=v.begin(); it!=bound; ++it)
    cout << ' ' << *it;
  cout << '\n';
 
  for (vector<int>::iterator it=bound; it!=v.end(); ++it)
    cout << ' ' << *it;
  cout << '\n';
 
  return 0;
}

a)

1 9 3 7 5
6 4 8 2 10

b)

6 4 8 2 10
1 9 3 7 5

c)

6 4 8 2 10
6 4 8 2 10

d)

1 9 3 7 5
1 9 3 7 5
View Answer
Answer: a
Explanation: partition() function is used to separate a given list into two parts one part is stored in the original container and other is stored in the newly passed container. So, in this case, we are using IsOdd() function to separate elements from each other which will separate odd and even numbers and as partition function is taking odd function and returning value to bound container, therefore, the odd number sequence will be stored in the bound container and even number sequence will be stored in the original v container.
Output:

$ ./a.out 
1 9 3 7 5
6 4 8 2 10
 
 

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

#include <iostream>  
#include <algorithm> 
#include <vector> 
 
using namespace std;
 
bool IsOdd (int i) 
{ 	
	return (i%2)==1; 
}
 
int main () 
{
  vector<int> v = {4,2,10,5,1,8};
  for(int i=0;i<v.size();i++)
  	cout<<v[i]<<" ";
  cout<<endl;
  sort(v.begin(), v.end());
  for(int i=0;i<v.size();i++)
  	cout<<v[i]<<" ";
  return 0;
}

a)

4 2 10 5 1 8 
1 2 4 5 8 10

b)

1 2 4 5 8 10
4 2 10 5 1 8 

c)

4 2 10 5 1 8 
4 2 10 5 1 8 

d)

1 2 4 5 8 10
1 2 4 5 8 10
View Answer
Answer: a
Explanation: sort() function is used to sort a given sequence of numbers hence the program takes the vecto and sorts the numbers of the vector.
Output:

$ ./a.out 
4 2 10 5 1 8 
1 2 4 5 8 10
 
 

10. What is the property of stable sort function provided by the STL algorithm?
a) sorts the elements of a sequence in ascending order preserving the relative order of equivalent elements
b) sorts the elements of a sequence in descending order preserving the relative order of equivalent elements
c) arranges the sequence randomly preserving the relative order of equivalent elements
d) same as sort function of STL algorithm
View Answer

Answer: a
Explanation: stable sort is used to sort the elements of a sequence also preserving the relative order of the equivalent elements.

11. What is the property of partial sort function provided by the STL algorithm?
a) sorts the elements before the middle element in ascending order and remaining elements are left without any specific order
b) sorts the elements before the middle element in descending order and remaining elements are left without any specific order
c) sorts the elements after the middle element in ascending order and remaining elements are left without any specific order
d) sorts the elements after the middle element in descending order and remaining elements are left without any specific order
View Answer

Answer: a
Explanation: partial sort of STL algorithm is used to sort the given elements before the middle element in ascending order without any specific order of elements after the middle element.

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

#include <iostream>  
#include <algorithm> 
#include <vector> 
 
using namespace std;
 
int main () 
{
  vector<int> v = {4,2,10,5,1,8};
  sort(v.begin(), v.end());
  if (binary_search(v.begin(), v.end(), 4))
    cout << "found.\n"; 
  else 
  	cout << "not found.\n";
  return 0;
}

a) found.
b) not found.
c) Error
d) Segmentation fault
View Answer

Answer: a
Explanation: In this program, we are using trying to search element 4 in the given vector using binary search and as element 4 is present in the vector, therefore, the function prints “found!”.
Output:

$ ./a.out 
found!

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

#include <iostream>  
#include <algorithm> 
#include <vector> 
 
using namespace std;
 
int main () 
{
  vector<int> v = {4,2,10,5,1,8};
  if (binary_search(v.begin(), v.end(), 4))
    cout << "found.\n"; 
  else 
  	cout << "not found.\n";
  return 0;
}

a) found.
b) not found.
c) Error
d) Segmentation fault
View Answer

Answer: b
Explanation: As the searching function binary_search() uses binary search to find an element in a sequence which need a sequence to be in ascending order but as the vector in the program is not in ascending order, therefore, the binary search fails to search element 4 in the vector hence output is “not found.”
Output:

$ ./a.out
not found.

14. Which function can be used to find the sum of a vector container?
a) findsum()
b) accumulate()
c) calcsum()
d) checksum()
View Answer

Answer: b
Explanation: STL provides accumulate() function to find the sum of a vector.

15. Which header file is required to use accumulate() function?
a) <algorithm>
b) <numeric>
c) <vector>
d) <iostream>
View Answer

Answer: b
Explanation: <numeric> header file is required to use accumulate function in a program.

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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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.