1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. std::lower_bound is called on a range that is not…

Added 30 Nov -0001

std::lower_bound is called on a range that is not partitioned with respect to the searched value. What is the outcome?

  1. The first element equal to the value, found by a linear scan fallback
  2. A compilation error, since the predicate cannot be checked in advance
  3. Undefined behaviour, because the range must be partitioned for the search Correct answer
  4. Unspecified behaviour; the algorithm still returns some element of the range

Correct answer

C. Undefined behaviour, because the range must be partitioned for the search

11 views

MCQSEXAM

Explanation

The binary-search family requires the range to be partitioned with respect to the value and predicate: every element for which the predicate holds must precede every element for which it does not. Violating that precondition is undefined behaviour rather than a defined but disappointing result, so lower_bound may return any position at all. A range sorted by the same comparison does satisfy the condition for the default less-than predicate.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is C? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C++ and Object-Oriented Programming MCQs

Practise all C++ and Object-Oriented Programming MCQs

Similar MCQs