🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: STL Algorithms: all 10 questions

Quiz: STL Algorithms · Question 6 of 10

What does this print? (code)

What does this print?

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
    vector<int> v = {10, 20, 20, 20, 30, 40};
    auto lo = lower_bound(v.begin(), v.end(), 20);
    auto hi = upper_bound(v.begin(), v.end(), 20);
    cout << (lo - v.begin()) << " " << (hi - v.begin()) << " " << (hi - lo) << "\n";
    return 0;
}
  1. A.1 3 2
  2. B.1 4 3
  3. C.0 4 4
  4. D.2 4 2
Answer & explanation

Correct answer: 1 4 3

lower_bound finds the first 20 (index 1), upper_bound the first value above 20 (index 4); their difference counts the 20s.

Can you get the rest right without peeking?

Take the timed Quiz: STL Algorithms quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →