01. Two sorted vectors are merged into a third, which is default-constructed and therefore empty.
std::vector<int> out;
std::merge(a.begin(), a.end(), b.begin(), b.end(), out.begin());
What is wrong with this call?
a) The destination has no elements, and the algorithm never grows one, so it writes past the end
b) The destination must be sorted before the merge, and an empty vector cannot be sorted
c) The two input ranges must be of the same length, and nothing here guarantees that they are
d) The algorithm returns the end of the output, so its result must be assigned to keep the vector valid
02. One range must be located inside another. Two candidate algorithms are considered.
How do std::search and std::find_first_of differ?
a) The first requires both ranges to be sorted; the second works on any range
b) The first looks for any one element of the second range; the second looks for the whole run as written
c) The first looks for the whole second range as a consecutive run; the second for any one element
d) The first returns a bool; the second returns an iterator to the match
03. After std::lower_bound returns a dereferenceable iterator it, membership must still be confirmed.
Why is *it == x the required extra test?
a) The bound is the first element greater than x, so the test corrects an off-by-one
b) The iterator may be invalid, and comparing the element is a way of checking that it is not
c) The algorithm compares with < only, and equality is not derivable from a comparison
d) The bound is the first element not less than x, which may be greater than x rather than equal
04. Records are keyed in a std::set by a comparator that examines only the surname.
Two records share a surname but differ in every other member. What happens on the second insertion?
a) It is rejected as a duplicate, because a comparator that ignores a member always merges records
b) It succeeds, since the two records differ in members the comparator does not examine
c) It succeeds, and the container stores both under one node
d) It succeeds, and the container throws only if the two records also compare equal
05. The same expression is written for a vector iterator and for a list iterator.
it + 3
What is the outcome in each case?
a) Valid for the list; rejected for the vector, whose iterators are raw pointers
b) Valid for the vector; rejected for the list, whose iterators support only increment and decrement
c) Rejected for both, since iterator arithmetic must be written with std::advance
d) Valid for both, but linear in the list case rather than constant
06. Both the smallest and the largest element of a range are needed, using only classic-STL facilities.
Which approach expresses this?
a) Call std::minmax_element, which returns both positions together as a pair
b) Sort the range and read its first and last elements
c) Call std::max_element twice, once through reverse iterators
d) Call std::min_element and std::max_element separately
07. Every element of a std::vector<Widget> must have its reset() member called.
Which classic-STL adapter expresses this?
a) std::mem_fun_ref(&Widget::reset)
b) std::mem_fun(&Widget::reset) on the same range
c) std::ptr_fun(&Widget::reset)
d) std::bind1st(&Widget::reset)
08. A vector holds Rec * and must be ordered by the score member of the pointed-to records.
std::sort(v.begin(), v.end());
What does this call actually order the vector by?
a) The score members, because the algorithm dereferences pointer elements automatically
b) Nothing — the call is rejected, because < is not defined for pointer types
c) The pointer values themselves, which is an unspecified order with respect to score
d) The order in which the records were allocated, since addresses rise with each allocation
09. Elements are to be shifted two positions to the right within one vector, and the source and destination overlap.
Why is std::copy_backward the correct choice?
a) Copying forward would leave the destination in reverse order, which the backward form corrects
b) Copying forward would overwrite elements it has not yet read; working from the end avoids that
c) std::copy rejects overlapping ranges at compile time, so the backward form is the only option
d) std::copy_backward is faster whenever the ranges overlap, whatever the direction of the shift
10. std::max_element is applied to an unsorted vector of one million elements.
How many comparisons does the algorithm make?
a) As many as the number of elements squared, since every pair must be compared
b) About twenty, since the algorithm halves the range on each step
c) One fewer than the number of elements, since each element after the first is compared once
d) The number of elements, since the first is compared against itself to initialise the result