🎙 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 7 of 10

What does this print? (code)

What does this print?

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
    vector<string> names = {"Ravi", "Asha", "Om", "Meena"};
    sort(names.begin(), names.end(), [](const string &a, const string &b) {
        return a.size() < b.size();
    });
    cout << names[0] << " " << names[3] << "\n";
    return 0;
}
  1. A.Asha Ravi
  2. B.Om Meena
  3. C.Meena Om
  4. D.Asha Meena
Answer & explanation

Correct answer: Om Meena

The lambda compares lengths, so the shortest name (Om, 2 letters) comes first and the longest (Meena, 5) last.

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 →