1. Home
  2. All MCQs

All MCQs with Answers · Page 1407

45,334 multiple choice questions from every subject, newest first. Switch on practice mode to hide the answers and test yourself.

Newest first

A function is declared as decltype(auto) f() { return x; } where x is a global int. What does f return?

  1. int, because decltype of a bare name yields its declared type Correct answer
  2. int&, because decltype(auto) gives an lvalue reference
  3. int&&, because returning a global binds it to an rvalue reference
  4. const int, since the returned object counts as a temporary value
MCQSEXAM Read explanation

Inside a function template, what happens to the branch of an if constexpr whose condition is false for a given instantiation?

  1. It is removed before parsing, so even syntax errors in it are ignored
  2. It is compiled and executed at run time only if its condition is true
  3. It is discarded and not instantiated, so dependent code there may be invalid Correct answer
  4. It is still instantiated and type-checked, then removed later as a dead branch
MCQSEXAM Read explanation

A class holds a std::unique_ptr<Impl> member and defines its destructor only in the header as = default. Why does this fail to compile?

  1. The defaulted destructor needs Impl complete to invoke the deleter on it Correct answer
  2. unique_ptr can never hold an incomplete type in any context in C++
  3. The constructor must see Impl to size the pointer, so the definition is needed
  4. The compiler cannot generate a move constructor for an incomplete member
MCQSEXAM Read explanation

What is the lifetime of the backing array of a std::initializer_list bound to a local variable?

  1. It has static storage duration, so returning the list by value is safe
  2. It ends with the full-expression that created it, like any temporary array Correct answer
  3. It ends only with the enclosing block, exactly as a local const array does
  4. It lasts as long as the initializer_list object that was copied from it
MCQSEXAM Read explanation

double d = 3.9; int x{d}; What does a conforming C++11 compiler do with this declaration?

  1. Compiles and sets x to 3, since braced init truncates like a cast does
  2. Compiles and sets x to 4, since braced init rounds to the nearest integer
  3. Compiles with a warning, since the value is implementation-defined here
  4. Rejects it, because narrowing conversions are ill-formed in braced init Correct answer
MCQSEXAM Read explanation

In template<class T> void f(T&& x), what is T deduced as when the function is called with an lvalue int?

  1. int&, because the forwarding reference collapses to an lvalue reference Correct answer
  2. int, because the reference in the parameter is stripped before deduction
  3. int&&, because the parameter is declared as an rvalue reference to T
  4. const int&, because lvalues are deduced as const in template deduction
MCQSEXAM Read explanation

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
MCQSEXAM Read explanation

In std::map, what happens to the other iterators when one element is erased through an iterator?

  1. All iterators are invalidated, because the tree rebalances its nodes
  2. Iterators to later elements are invalidated, while earlier ones stay valid
  3. Iterators become valid again after the next insert into the container
  4. Only the erased iterator is invalidated; every other iterator stays valid Correct answer
MCQSEXAM Read explanation

const char s[] = "abcdef"; is written as two adjacent literals, "abc" and "def". How many characters does the array hold, including the terminator?

  1. 6, because the literals share one terminator between them
  2. 9, because a separator character is inserted between the two literals
  3. 7, because adjacent literals are concatenated before compilation Correct answer
  4. 8, because each literal keeps its own terminating null character
MCQSEXAM Read explanation

int a[n]; is declared inside a function, with n supplied at run time. When is sizeof(a) evaluated, and what does it yield?

  1. At run time, giving n times the element size, with no compile-time constant Correct answer
  2. At compile time, giving the maximum size the array could possibly take
  3. At run time, giving the address of the last element instead of a byte size value
  4. At compile time, giving the size of a pointer because a is an array name
MCQSEXAM Read explanation