- Home
- Subjects
- Computer Science MCQs
- C++ and Object-Oriented Programming
C++ and Object-Oriented Programming MCQs · Page 2
115 multiple choice questions with answers in Computer Science MCQs.
-
A
References and pointers to elements stay valid, but the iterators are invalidated
Correct answer
-
B
Nothing survives: all iterators, pointers and references are invalidated at once
-
C
Only iterators survive, because buckets are relinked while the nodes are reallocated
-
D
Everything survives, since rehashing only changes the size of the bucket array
-
A
It fails to compile, because delete cannot be applied to an array new expression
-
B
The form of delete does not match the form of new, so the behaviour is undefined
Correct answer
-
C
Only the first element is destroyed and the remaining nine are leaked silently
-
D
The delete is acceptable, but the ten ints stay allocated until the program exits
-
A
Cast away const, which is well defined even for an actually const object
-
B
Mark the member function constexpr, since constant evaluation permits such writes
-
C
Mark the member mutable, so that a const member function can still modify it
Correct answer
-
D
Mark the member static, because const-qualification then no longer applies to it
-
A
The capture is moved to the heap once and shared between copies, making copies cheap
-
B
std::function rejects capturing lambdas, so the code fails to compile
-
C
The closure object lives inside f, so copying f copies the whole captured array
Correct answer
-
D
The lambda decays to a plain function pointer, so the captured array is not kept alive
-
A
It depends on the compiler, since merging of virtual bases is left implementation-defined
-
B
Exactly one, shared by both paths, because virtual bases are merged into one subobject
Correct answer
-
C
Two, one reached through B1 and one through B2, addressed by distinct base offsets
-
D
Zero, because virtual inheritance removes the base class entirely from the layout
-
A
shared_ptr detects cycles and defers destruction to the next garbage collection
-
B
The control blocks are leaked first, so the destructors run but the memory does not
-
C
Each object's reference count stays at one because of the mutual strong references
Correct answer
-
D
The reference counts are merged into a single counter, so they never reach zero
-
A
Value semantics and a size() member, while the elements stay contiguous in memory
Correct answer
-
B
Heap allocation, so very large arrays do not consume space in the caller's stack frame
-
C
Automatic bounds checking on every operator[] access in all build configurations
-
D
Dynamic resizing, so extra elements can be appended after the object is constructed
-
A
std::sort needs a function pointer, so a lambda cannot be used as the predicate
-
B
The predicate is fine, but it makes std::sort fall back to a stable bubble sort
-
C
The predicate is not a strict weak ordering, so the sort behaviour is undefined
Correct answer
-
D
The predicate sorts descending, which std::sort rejects with a compile error
-
A
int and int, because parentheses are ignored entirely by the decltype specifier
-
B
int& and int&, because decltype always adds a reference to any named variable
-
C
int and int&, because a parenthesised lvalue expression produces a reference type
Correct answer
-
D
int& and int, because the extra pair of parentheses strips the reference from the type
-
A
It becomes a reference to a default-constructed int placed in the new buffer
-
B
It dangles, because growth allocates a new buffer and destroys the old elements
Correct answer
-
C
It remains valid, since vector guarantees reference stability across insertions
-
D
It is rebound automatically to the new buffer at the same logical index position
-
A
emplace_back is only valid for trivially copyable types such as int and double values
-
B
The two calls are identical in every respect, including the number of constructions
-
C
emplace_back builds the string in place from the literal, push_back moves a temporary in
Correct answer
-
D
emplace_back copies an already-built string object, while push_back builds one from the literal
-
A
sv keeps the temporary alive by extending its lifetime, exactly like const& binding
-
B
sv refers to a temporary destroyed at the end of the full expression, so it dangles
Correct answer
-
C
sv owns its own copy of the characters, so the member wastes memory but stays valid
-
D
sv is rejected at compile time, because a view cannot bind to a temporary string
-
A
vector then moves elements instead of copying them, which keeps the strong guarantee
Correct answer
-
B
vector refuses to grow the buffer at all once the move constructor is declared noexcept
-
C
vector starts using memcpy on the elements, since noexcept proves they are trivial
-
D
vector falls back to copying, because noexcept disables the move constructor entirely
-
A
The move constructor, because std::move casts its argument to an rvalue reference
-
B
A compile-time error, because std::move is deleted for const-qualified arguments by design
-
C
The move constructor, because const objects are always treated as expiring values here
-
D
The copy constructor, because std::move yields a const rvalue that cannot bind to T&&
Correct answer
-
A
a is const int& and b is const int&, because auto copies the initialiser's type exactly
-
B
a is int and b is int, because auto& also drops the const qualifier from the referent
-
C
a is const int and b is const int&, because auto keeps const but removes the reference
-
D
a is int and b is const int&, because plain auto strips references and top-level const
Correct answer
-
A
The call is ambiguous and fails to compile, because the two declarations disagree about the default argument values used
-
B
20, because the dynamic type of the object is Derived and its own default arguments apply to the call in full
-
C
10, because default arguments are bound statically from the pointer's type while the override is chosen dynamically
Correct answer
-
D
10 or 20 depending on the compiler, since the standard leaves the choice of default arguments unspecified here
-
A
A compile error is guaranteed on every conforming compiler, because const_cast is rejected there
-
B
Undefined behaviour, because modifying an object that is genuinely const is not allowed
Correct answer
-
C
It works and c becomes the new value, because the pointer overrides the const qualifier
-
D
It is well defined, but the write is silently discarded, so c keeps its original value of 10
-
A
Their destructors run in reverse order of construction as the stack unwinds
Correct answer
-
B
They are leaked, because destructors only run at normal return
-
C
They are destroyed only if their class has a virtual destructor
-
D
std::terminate is called before any destructor runs
-
A
operator++ and operator--, in prefix or postfix form, cannot be overloaded
-
B
The conditional operator ?: together with ::, . (member access), .* and sizeof
Correct answer
-
C
operator[] and operator(), although both are member functions with arguments and return values
-
D
operator new and operator delete, in either their throwing or their non-throwing forms
-
A
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
-
B
Both keep the keys ordered, differing only in hash quality, so iteration order is identical for both of the two containers
-
C
unordered_map iterates in insertion order, unlike map, which re-sorts the keys at every single insertion
-
D
map gives O(1) lookup while unordered_map is O(log n), because a balanced tree always beats a hash table
-
A
A second declaration inside main, because the initialiser was not visible outside the class scope
-
B
An out-of-class definition, because the in-class initialiser served only as a declaration for such uses
Correct answer
-
C
A definition in every translation unit that included the header, to satisfy the one-definition rule
-
D
Nothing, since in-class initialisers always provided a full definition that the linker was normally able to use
-
A
It returns memory aligned to at least 4096 bytes, which malloc is not required to guarantee for any type
-
B
It zero-initialises the object unless a constructor sets fields, which malloc never does for any type
-
C
It can allocate arrays, whereas malloc cannot, because malloc knows nothing about element counts
-
D
It runs T's constructor and reports failure by throwing std::bad_alloc instead of returning null
Correct answer
-
A
It is copyable, and copying it duplicates the object with a fresh allocation
-
B
It is move-only: copying is deleted, and ownership is transferred with std::move
Correct answer
-
C
It may be copied, but both pointers then share ownership through a hidden reference count
-
D
It requires a reference count stored in a control block allocated beside the managed object
-
A
The program fails to compile, because virtual calls in constructors are illegal and are always rejected by every conforming compiler
-
B
Derived's version, because the dynamic type of the object is already Derived from the moment the allocation completes
-
C
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
-
D
It is undefined behaviour, so either version may run depending on the compiler and on the optimisation level used
-
A
It turns a would-be silent non-override, such as a missing const or a wrong parameter type, into a compile error
Correct answer
-
B
It allows the function to be inlined by the compiler, because the exact target is known at compile time
-
C
It forces the base function to become virtual for the first time, even when the base declares it non-virtual
-
D
It makes the function callable through a base pointer, which is otherwise impossible for a derived member function