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

C++ and Object-Oriented Programming MCQs · Page 2

115 multiple choice questions with answers in Computer Science MCQs.

Showing 26–50 of 115

A std::unordered_map is rehashed by an insertion. Which of its element references survive that operation?

  1. References and pointers to elements stay valid, but the iterators are invalidated Correct answer
  2. Nothing survives: all iterators, pointers and references are invalidated at once
  3. Only iterators survive, because buckets are relinked while the nodes are reallocated
  4. Everything survives, since rehashing only changes the size of the bucket array
MCQSEXAM Read explanation

int* p = new int[10]; delete p; is executed. What is the defect?

  1. It fails to compile, because delete cannot be applied to an array new expression
  2. The form of delete does not match the form of new, so the behaviour is undefined Correct answer
  3. Only the first element is destroyed and the remaining nine are leaked silently
  4. The delete is acceptable, but the ten ints stay allocated until the program exits
MCQSEXAM Read explanation

A const member function needs to update a cache counter. Which change is legitimate, and why?

  1. Cast away const, which is well defined even for an actually const object
  2. Mark the member function constexpr, since constant evaluation permits such writes
  3. Mark the member mutable, so that a const member function can still modify it Correct answer
  4. Mark the member static, because const-qualification then no longer applies to it
MCQSEXAM Read explanation

A std::function<void()> f wraps a lambda that captures a large array by value. What is the practical consequence?

  1. The capture is moved to the heap once and shared between copies, making copies cheap
  2. std::function rejects capturing lambdas, so the code fails to compile
  3. The closure object lives inside f, so copying f copies the whole captured array Correct answer
  4. The lambda decays to a plain function pointer, so the captured array is not kept alive
MCQSEXAM Read explanation

class D : public B1, public B2, where both B1 and B2 virtually inherit A. How many A subobjects does a D contain?

  1. It depends on the compiler, since merging of virtual bases is left implementation-defined
  2. Exactly one, shared by both paths, because virtual bases are merged into one subobject Correct answer
  3. Two, one reached through B1 and one through B2, addressed by distinct base offsets
  4. Zero, because virtual inheritance removes the base class entirely from the layout
MCQSEXAM Read explanation

Two objects hold std::shared_ptr to each other. Why is neither destructor called when the last external owner releases them?

  1. shared_ptr detects cycles and defers destruction to the next garbage collection
  2. The control blocks are leaked first, so the destructors run but the memory does not
  3. Each object's reference count stays at one because of the mutual strong references Correct answer
  4. The reference counts are merged into a single counter, so they never reach zero
MCQSEXAM Read explanation

What does std::array<int, 4> provide that a plain int[4] does not?

  1. Value semantics and a size() member, while the elements stay contiguous in memory Correct answer
  2. Heap allocation, so very large arrays do not consume space in the caller's stack frame
  3. Automatic bounds checking on every operator[] access in all build configurations
  4. Dynamic resizing, so extra elements can be appended after the object is constructed
MCQSEXAM Read explanation

std::sort(v.begin(), v.end(), [](int a, int b){ return a >= b; }); is called. What is the defect?

  1. std::sort needs a function pointer, so a lambda cannot be used as the predicate
  2. The predicate is fine, but it makes std::sort fall back to a stable bubble sort
  3. The predicate is not a strict weak ordering, so the sort behaviour is undefined Correct answer
  4. The predicate sorts descending, which std::sort rejects with a compile error
MCQSEXAM Read explanation

For int x = 0; what do decltype(x) and decltype((x)) yield?

  1. int and int, because parentheses are ignored entirely by the decltype specifier
  2. int& and int&, because decltype always adds a reference to any named variable
  3. int and int&, because a parenthesised lvalue expression produces a reference type Correct answer
  4. int& and int, because the extra pair of parentheses strips the reference from the type
MCQSEXAM Read explanation

A reference int& r = v[0]; is taken, then v.push_back(99) triggers a reallocation. What is the state of r?

  1. It becomes a reference to a default-constructed int placed in the new buffer
  2. It dangles, because growth allocates a new buffer and destroys the old elements Correct answer
  3. It remains valid, since vector guarantees reference stability across insertions
  4. It is rebound automatically to the new buffer at the same logical index position
MCQSEXAM Read explanation

For std::vector<std::string> v, how do v.push_back("abc") and v.emplace_back("abc") differ?

  1. emplace_back is only valid for trivially copyable types such as int and double values
  2. The two calls are identical in every respect, including the number of constructions
  3. emplace_back builds the string in place from the literal, push_back moves a temporary in Correct answer
  4. emplace_back copies an already-built string object, while push_back builds one from the literal
MCQSEXAM Read explanation

std::string_view sv = std::string("temp"); is stored as a class member. What is wrong with that?

  1. sv keeps the temporary alive by extending its lifetime, exactly like const& binding
  2. sv refers to a temporary destroyed at the end of the full expression, so it dangles Correct answer
  3. sv owns its own copy of the characters, so the member wastes memory but stays valid
  4. sv is rejected at compile time, because a view cannot bind to a temporary string
MCQSEXAM Read explanation

Why does declaring a move constructor noexcept change how std::vector relocates its elements when the buffer grows?

  1. vector then moves elements instead of copying them, which keeps the strong guarantee Correct answer
  2. vector refuses to grow the buffer at all once the move constructor is declared noexcept
  3. vector starts using memcpy on the elements, since noexcept proves they are trivial
  4. vector falls back to copying, because noexcept disables the move constructor entirely
MCQSEXAM Read explanation

A const std::string s is passed to a constructor call as std::move(s). Which constructor runs, and why?

  1. The move constructor, because std::move casts its argument to an rvalue reference
  2. A compile-time error, because std::move is deleted for const-qualified arguments by design
  3. The move constructor, because const objects are always treated as expiring values here
  4. The copy constructor, because std::move yields a const rvalue that cannot bind to T&& Correct answer
MCQSEXAM Read explanation

Given const int n = 5; const int& r = n; what types are deduced for auto a = r; and auto& b = r;?

  1. a is const int& and b is const int&, because auto copies the initialiser's type exactly
  2. a is int and b is int, because auto& also drops the const qualifier from the referent
  3. a is const int and b is const int&, because auto keeps const but removes the reference
  4. a is int and b is const int&, because plain auto strips references and top-level const Correct answer
MCQSEXAM Read explanation

Base declares virtual void f(int x = 10); and Derived overrides it as void f(int x = 20);. Through a Base* b pointing at a Derived object, b->f() passes which argument?

  1. The call is ambiguous and fails to compile, because the two declarations disagree about the default argument values used
  2. 20, because the dynamic type of the object is Derived and its own default arguments apply to the call in full
  3. 10, because default arguments are bound statically from the pointer's type while the override is chosen dynamically Correct answer
  4. 10 or 20 depending on the compiler, since the standard leaves the choice of default arguments unspecified here
MCQSEXAM Read explanation

An object is defined as const int c = 10; and const_cast is used to write through a pointer to it. What is the result?

  1. A compile error is guaranteed on every conforming compiler, because const_cast is rejected there
  2. Undefined behaviour, because modifying an object that is genuinely const is not allowed Correct answer
  3. It works and c becomes the new value, because the pointer overrides the const qualifier
  4. It is well defined, but the write is silently discarded, so c keeps its original value of 10
MCQSEXAM Read explanation

Which of these operators cannot be overloaded in C++?

  1. operator++ and operator--, in prefix or postfix form, cannot be overloaded
  2. The conditional operator ?: together with ::, . (member access), .* and sizeof Correct answer
  3. operator[] and operator(), although both are member functions with arguments and return values
  4. operator new and operator delete, in either their throwing or their non-throwing forms
MCQSEXAM Read explanation

Which is the accurate comparison of std::map and std::unordered_map?

  1. map keeps keys ordered with O(log n) operations; unordered_map gives average O(1) with no ordering and O(n) worst case Correct answer
  2. Both keep the keys ordered, differing only in hash quality, so iteration order is identical for both of the two containers
  3. unordered_map iterates in insertion order, unlike map, which re-sorts the keys at every single insertion
  4. map gives O(1) lookup while unordered_map is O(log n), because a balanced tree always beats a hash table
MCQSEXAM Read explanation

Before C++17, a static const int member with an in-class initialiser still required what, if it was odr-used?

  1. A second declaration inside main, because the initialiser was not visible outside the class scope
  2. An out-of-class definition, because the in-class initialiser served only as a declaration for such uses Correct answer
  3. A definition in every translation unit that included the header, to satisfy the one-definition rule
  4. Nothing, since in-class initialisers always provided a full definition that the linker was normally able to use
MCQSEXAM Read explanation

Which does new T provide that malloc(sizeof(T)) does not?

  1. It returns memory aligned to at least 4096 bytes, which malloc is not required to guarantee for any type
  2. It zero-initialises the object unless a constructor sets fields, which malloc never does for any type
  3. It can allocate arrays, whereas malloc cannot, because malloc knows nothing about element counts
  4. It runs T's constructor and reports failure by throwing std::bad_alloc instead of returning null Correct answer
MCQSEXAM Read explanation

Which statement about std::unique_ptr is correct?

  1. It is copyable, and copying it duplicates the object with a fresh allocation
  2. It is move-only: copying is deleted, and ownership is transferred with std::move Correct answer
  3. It may be copied, but both pointers then share ownership through a hidden reference count
  4. It requires a reference count stored in a control block allocated beside the managed object
MCQSEXAM Read explanation

Base's constructor calls a virtual function that Derived overrides, and a Derived object is being constructed. Which version runs?

  1. The program fails to compile, because virtual calls in constructors are illegal and are always rejected by every conforming compiler
  2. Derived's version, because the dynamic type of the object is already Derived from the moment the allocation completes
  3. Base's version, because during base construction the vptr still refers to Base's vtable and the Derived part does not yet exist Correct answer
  4. It is undefined behaviour, so either version may run depending on the compiler and on the optimisation level used
MCQSEXAM Read explanation

Why is the override specifier preferred on a derived virtual function?

  1. It turns a would-be silent non-override, such as a missing const or a wrong parameter type, into a compile error Correct answer
  2. It allows the function to be inlined by the compiler, because the exact target is known at compile time
  3. It forces the base function to become virtual for the first time, even when the base declares it non-virtual
  4. It makes the function callable through a base pointer, which is otherwise impossible for a derived member function
MCQSEXAM Read explanation