Test Series - cpp

Test Number 90/102

Q: Which of the header file is used to implement algorithms provided by C++ STL?
A. 
B. 
C. 
D. 
Solution:  header is provided by the C++ to use STL algorithms.
Q: How many types of sequence operations are provided by the C++ algorithm STL?
A. 1
B. 2
C. 3
D. 4
Solution: There are two main types of sequence operations are provided by the C++ algorithm STL namely Non-modifying sequence operations and Modifying sequence operations.
Q: Which of the following is a Modifying Sequence Operation?
A. all_of()
B. any_of()
C. equal()
D. swap()
Solution: 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.
Q: Which of the following is a Non-modifying Sequence Operation?
A. swap()
B. transform()
C. remove()
D. search()
Solution: 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.
Q: 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.
Solution: random_shuffle() function is used to re-arrange a given sequence of elements in a range.
Q: 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
Solution: stable sort is used to sort the elements of a sequence also preserving the relative order of the equivalent elements.
Q: 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
Solution: 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.
Q: Which function can be used to find the sum of a vector container?
A. findsum()
B. accumulate()
C. calcsum()
D. checksum()
Solution: STL provides accumulate() function to find the sum of a vector.
Q: Which header file is required to use accumulate() function?
A. 
B. 
C. 
D. 
Solution:  header file is required to use accumulate function in a program.
Q: What will be the output of the following C++ code?

#include  
#include  
#include  
 
using namespace std;
 
int main() 
{ 
	vector v(10, 2); 
	if (all_of(v.cbegin(), v.cend(), [](int i){ return i % 2 == 0; })) 
	{ 
		cout << "All numbers are even
"; 
	} 
	else
        {
		cout << "All numbers are not even
"; 
	}
	return 0;
}
A. All numbers are even
B. All numbers are not even
C. Error
D. Segmentation fault
Solution: 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

You Have Score    /10