C++ STL Non-Mutating Algorithms

This quiz tests your knowledge of STL algorithms that do not affect the given container (non-mutating algorithms). It contains 10 MCQs.

Last Updated :
Discuss
Comments

Question 1

What is the output of the below program?

C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
    vector<int> v = {5, 10, 15};
    int res = accumulate(v.begin(), v.end(), 0, [](int a, int b) { return a * b; });
    cout << res;
    return 0;
}


  • 750

  • 0

  • 30

  • Compiler error

Question 2

Which of the following is TRUE about the binary_search() function in C++ STL?

  • It can search for an element in an unsorted container.

  • It has a time complexity of O(n).

  • It always returns an iterator pointing to the found element.

  • It requires the container to be sorted before searching.

Question 3

Binary search is internally implemented using lower_bound() as shown:

it = lower_bound(begin, end, x);
if (it != end && !(x < *it))
return true;
else
return false;

How to modify this logic to make it work with upper_bound()?

  • it = ++upper_bound(begin, end, x);

  • it = --upper_bound(begin, end, x);

  • it = upper_bound(begin, end, x);

  • Not Possible to do it with upper bound

Question 4

What will be the output of the following C++ program?

C++
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
    vector<int> v = {1, 1, 1, 1};
    auto it1 = max_element(v.begin(), v.end());
    auto it2 = min_element(v.begin(), v.end());
    cout << (*it1) - (*it2);
    return 0;
}


  • Compiler Error

  • 0

  • 1 1

  • 1

Question 5

What will be the output of the following C++ program?

C++
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<int> v1 = {1, 2, 3, 3, 4};
    vector<int> v2 = {3, 4, 3, 2, 1};
    if (is_permutation(v1.begin(), v1.end(), v2.begin()))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


  • No

  • Yes

  • Compilation Error

  • Undefined Behavior

Question 6

What does the 2nd parameter in rotate() specify?

  • The new first element after the rotation.

  • The end of the range to be rotated.

  • The number of positions to rotate the elements by.

  • The beginning of the range to be rotated.

Question 7

What is the time complexity of the std::count() function for multiset() in C++ STL?

  • O(1)




  • O(log n)

  • O(n)

  • O(n log n)

Question 8

Which of the following best describes the return value of the find() function when the search value is not found in the given range?

  • The iterator to the position one past the last element in the range.

  • A null pointer.

  • An iterator pointing to the last element of the range.

  • An iterator pointing to the beginning of the range.

Question 9

Consider the program:

C++
#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
    for (int i = 0; i < 3; i++)
        cout << rand() % 6 << " ";
    return 0;
}

Which of the following output is not possible?

  • 3 3 3

  • 1 4 6

  • 2 4 1

  • None

Question 10

What does the lower_bound() function return in C++ STL?

  • The position of the first occurrence of the given value in an unsorted range.

  • The position of the last occurrence of the given value in a sorted range.

  • An iterator to the first element greater than or equal to the given value in a sorted range.

  • An iterator pointing to the middle element of the range.

Tags:

There are 10 questions to complete.

Take a part in the ongoing discussion