LearnFrenzy
Sign in Take a free test

STL Algorithms and Iterators · Exercise 1 of 4

STL Algorithms and Iterators STL Algorithms MCQ

  • 20 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Twenty multiple-choice questions on C++ STL algorithms and iterators: iterator categories, find and count, sort variants, binary search, remove-erase, numeric algorithms and ranges. Pick an option to check your answer instantly.

Question 11 Medium

v is a vector of doubles {0.5, 0.5, 0.5}. What does std::accumulate(v.begin(), v.end(), 0) return?

A 1.5
B 0 – the sum is accumulated in int (the type of 0), truncating each step
C 1
D 2
Question page & discussion
Question 12 Hard

Which task is std::nth_element best for?

A Sorting the whole array
B Finding the median or k-th smallest element in O(n) average
C Removing duplicates
D Reversing
Question page & discussion
Question 13 Hard

Why is [](int a, int b) { return a <= b; } wrong as a std::sort comparator?

A It sorts descending
B It is not a strict weak ordering (cmp(a, a) is true), which is undefined behaviour and can even crash
C Lambdas are not allowed
D It is just slower
Question page & discussion
Question 14 Medium

What category are std::list iterators?

A Random access
B Bidirectional
C Input only
D Contiguous
Question page & discussion
Question 15 Easy

What does std::transform(a.begin(), a.end(), out, f) do?

A Sorts a
B Writes f(x) for every element x of a to the output range
C Removes elements
D Counts elements
Question page & discussion
Question 16 Medium

In std::ranges::sort(v, {}, &Student::marks), what is &Student::marks?

A A comparator
B A projection: each element is mapped to its marks before comparing
C A pointer to the vector
D A filter
Question page & discussion
Question 17 Medium

When are the elements of v | std::views::filter(f) | std::views::transform(g) computed?

A Immediately, into a new vector
B Lazily, one at a time as the view is iterated
C At compile time
D Never
Question page & discussion
Question 18 Hard

What is the key difference between std::accumulate and std::reduce?

A reduce only works on ints
B reduce may reorder and group operations (enabling parallelism), so the operation must be associative and commutative
C accumulate is parallel
D There is none
Question page & discussion
Question 19 Medium

To print every permutation of a string with std::next_permutation, what must you do first?

A Reverse it
B Sort it, so the loop starts from the smallest permutation
C Remove duplicates
D Nothing
Question page & discussion
Question 20 Hard

What is the complexity of std::distance(first, last)?

A Always O(1)
B O(1) for random-access iterators, O(n) for others (it steps one by one)
C Always O(n)
D O(log n)
Question page & discussion
Up next · Exercise 2Predict the Output20 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links