1. Home
  2. Subjects
  3. Computer Science MCQs
  4. C++ and Object-Oriented Programming

C++ and Object-Oriented Programming MCQs

115 multiple choice questions with answers in Computer Science MCQs.

Practise C++ and Object-Oriented Programming MCQs with answers on classes, objects, inheritance, polymorphism, templates and the STL. Each question comes with a short explanation, so you understand why an option is correct. Suitable for students, job tests and technical interviews.

Showing 1–25 of 115

An empty std::function<void()> is invoked as f(). What occurs at run time?

  1. A silent no-op, because the call operator checks for an empty target
  2. A segmentation fault, since the internal pointer is null and dereferenced
  3. std::bad_function_call is thrown, because no callable target is stored Correct answer
  4. Undefined behaviour, because std::function cannot be empty when called
MCQSEXAM Read explanation

typeid(*basePtr) is evaluated where Base has no virtual functions and the object is really a Derived. What type is reported?

  1. The static type Base, because typeid ignores the dynamic type here Correct answer
  2. The dynamic type Derived, since typeid always inspects the vtable
  3. A std::bad_typeid exception, since the base class is not polymorphic
  4. The most-derived type, but only after a dynamic_cast has been performed
MCQSEXAM Read explanation

Why does the copy-and-swap assignment operator provide the strong exception guarantee?

  1. Swapping uses move assignment, which the standard requires to be noexcept
  2. The old state is replaced only after the copy has been fully constructed Correct answer
  3. The copy constructor cannot throw once the source object has been locked
  4. It avoids allocating memory, so no allocation failure can occur at all
MCQSEXAM Read explanation

A shared_ptr is built as std::shared_ptr<int>(new int[10]). What happens when the last reference to it goes away?

  1. Plain delete is called on the array pointer, which is undefined behaviour Correct answer
  2. delete[] is chosen automatically because the pointer type is an array
  3. The control block detects the array and calls the array deleter for you
  4. Nothing is freed, because shared_ptr never deletes array allocations at all
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

A destructor throws while the stack is already unwinding from another exception. What is the outcome in C++11 and later?

  1. Both exceptions are merged into a std::nested_exception and rethrown together
  2. The program continues normally, with the destructor's exception silently discarded
  3. std::terminate is called, because two active exceptions cannot propagate at once Correct answer
  4. The new exception replaces the old one and unwinding continues from that point
MCQSEXAM Read explanation

An iterator it points into a std::map. The statement it = m.erase(it); is executed. What does it hold afterwards?

  1. The value m.begin(), because erasing resets all stored iterators
  2. The iterator to the following element, which erase returned by value Correct answer
  3. An invalidated iterator, so incrementing it afterwards is undefined behaviour
  4. The iterator to the erased element, which remains allocated but detached
MCQSEXAM Read explanation

A single-argument constructor is marked explicit. What does that prevent?

  1. Direct-initialisation, so objects can only be created through a factory function
  2. Copy construction, so the type can no longer be stored inside standard containers
  3. Aggregate initialisation, since the class is no longer an aggregate after the change
  4. Implicit conversion from the argument type at call sites and in copy-initialisation Correct answer
MCQSEXAM Read explanation

A lambda captures a local int by reference with [&amp;] and is stored in a std::function that outlives the enclosing function. What happens when it is invoked later?

  1. It reads a valid value, because std::function extends the lifetime of captures
  2. It reads a dangling reference, because the local died with its stack frame Correct answer
  3. It reads the value the local held, because capture by reference copies on escape
  4. It throws std::bad_function_call, since the captured reference has expired
MCQSEXAM Read explanation

Why can a function template not be partially specialised in C++?

  1. Function templates are chosen by overload resolution, so partial ordering fills that role Correct answer
  2. Partial specialisation of function templates was removed by the C++11 standard revision
  3. A function template needs a class template wrapper before it can be specialised at all
  4. Partial specialisation is allowed, but only when every template parameter is a reference type
MCQSEXAM Read explanation

Teacher or blogger? Put this quiz on your website

Free C++ and Object-Oriented Programming MCQs quiz with answers for your school site, academy page or blog. Copy one line of code – works on WordPress and Blogger.

Get the quiz widget

All 115 C++ and Object-Oriented Programming questions

Show the full list of questions in this topic