C++ STL

Last Updated :
Discuss
Comments

Question 1

What is the primary purpose of the C++ Standard Template Library (STL)?

  • To provide low-level access to hardware

  • To offer a collection of pre-built, generic data structures and algorithms.

  • To replace the need for manual memory management

  • To enable object-oriented programming in C++.

Question 2

Which of the following is not a component of the Standard Template Library (STL) in C++?

  • Containers

  • Iterators

  • Classes

  • Algorithms

Question 3

How is the std::map class implemented in C++?

  • As a linked list

  • As an array

  • As a red-black tree

  • As a hash table

Question 4

What will happen when using at() with non-existing key?

C++
map<int,int> m;
cout << m.at(1);
  • Returns 0

  • Inserts new element

  • Returns garbage

  • Throws exception

Question 5

What is the complexity of the size() member function of the queue STL in C++?

  • O(1)

  • O(n)

  • O(log n)

  • O(n log n)

Question 6

What will be the output?

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

int main() {
    set<int> s;

    s.insert(3);
    s.insert(1);
    s.insert(2);

    cout << *s.begin();
}
  • 3

  • 1

  • 2

  • Error

Question 7

What is the output of the following code?

C++
#include <iostream>
#include <set>

int main() {
    std::multiset<int> s {1, 2, 3, 3, 3, 4, 4, 5};
    s.erase(3); // removes all 3s
    for (auto it = s.begin(); it != s.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;
    return 0;
}
  • 1 2 3 4 5

  • 1 2 4 4 5

  • 1 2 4 5

  • 2 4 5

Question 8

Which operation removes the top element of a stack?

  • push()

  • pop()

  • top()

  • erase()

Question 9

What is the primary use of std:: pair in C++ STL?

  • To store multiple values of the same type

  • To store a pair of values, possibly of different types

  • To create dynamic arrays

  • To sort elements automatically

Question 10

In C++ STL terms, lower_bound(v.begin(), v.end(), x) returns:

  • First position > x

  • First position ≥ x

  • First position < x

  • Last position ≤ x

There are 14 questions to complete.

Take a part in the ongoing discussion