250+ TOP MCQs on STL Algorithms and Answers

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

1. Which function can be used to find the sum of a vector container?

a) findsum()
b) accumulate()
c) calcsum()
d) checksum()

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

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

#include  
#include  
#include  
 
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 evenn"; 
	} 
	else
        {
		cout << "All numbers are not evenn"; 
	}
	return 0;
}

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

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

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

Answer: b
Clarification: There are two main types of sequence operations are provided by the C++ algorithm STL namely Non-modifying sequence operations and Modifying sequence operations.

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

Answer: d
Clarification: 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()

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

  250+ TOP MCQs on Operators and Answers

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.

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

#include  
#include  
#include  
 
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

Answer: a
Clarification: 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   
#include  
#include  
 
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

Answer: a
Clarification: 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   
#include  
#include  
 
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

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

  250+ TOP MCQs on Enumerations and Answers

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

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

Answer: a
Clarification: 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   
#include  
#include  
 
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

  250+ TOP MCQs on Array Type Manipulation and Answers

Answer: a
Clarification: 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   
#include  
#include  
 
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

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

Leave a Comment

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

Scroll to Top