- Home
- Subjects
- Computer Science MCQs
- C++ and Object-Oriented Programming
C++ and Object-Oriented Programming MCQs · Page 3
115 multiple choice questions with answers in Computer Science MCQs.
-
A
Declaring at least one pure virtual function, such as virtual void f() = 0;
Correct answer
-
B
Declaring the constructor protected, so only derived classes can create objects
-
C
Making every member function virtual, so the class cannot be instantiated on its own
-
D
Declaring all data members private, so no outside code can construct it
-
A
nullptr for both pointer and reference casts
-
B
A pointer to the nearest base sub-object instead
-
C
It causes undefined behaviour because the cast cannot be checked
-
D
nullptr for a pointer cast, while the reference form throws std::bad_cast
Correct answer
-
A
A new element with a value-initialised int of 0 is inserted, so a lookup silently grows the map
Correct answer
-
B
std::out_of_range is thrown, exactly as counts.at('alpha') would throw for a missing key
-
C
A reference to a shared static zero is returned and the map itself is left unmodified
-
D
An iterator to the end of the map is returned, and dereferencing it is undefined behaviour
-
A
Base::print, because object slicing copies only the Base sub-object into the parameter
Correct answer
-
B
It is undefined behaviour, so either version may run depending on the optimisation level
-
C
Derived::print, because the argument's dynamic type stays Derived inside the function
-
D
Derived::print if Base's destructor is virtual, else Base::print, since dispatch depends on it
-
A
Nothing; all special members are still generated normally
-
B
The move constructor and move assignment operator
Correct answer
-
C
The default constructor and the copy constructor
-
D
Only the destructor, which must then be defined by the user
-
A
Copy-initialisation uses the copy constructor; assignment to an existing object uses the copy assignment operator
Correct answer
-
B
Both use the copy constructor, since the equals sign always means construction even for a live object
-
C
Both use the copy assignment operator, since the equals sign always means assignment whatever the surrounding context
-
D
T b = a; uses the default constructor followed by assignment, because b is an already constructed object
-
A
Both set only capacity, differing solely in the exception guarantees offered when a reallocation fails
-
B
reserve sets capacity to 100 with size still 0; resize sets size and capacity to 100 with value-initialised elements
Correct answer
-
C
Both set size to 100, but reserve leaves the new elements uninitialised until each element is written
-
D
reserve sets size to 100 and resize only sets capacity, so writing v[0] directly after reserve is perfectly well defined
-
A
m2, m1, B, A for initialisation, matching the initialiser list
-
B
m1, m2, A, B for initialisation and A, B, m1, m2 for destruction
-
C
A, B, m1, m2 for initialisation and the same order for destruction
-
D
A, B, m1, m2 for initialisation and m2, m1, B, A for destruction
Correct answer
-
A
0, because the class has no members
-
B
4, because every object needs a vtable pointer
-
C
8 on 64-bit platforms, matching the pointer size
-
D
1, because distinct objects must have distinct addresses
Correct answer
-
A
Only D's destructor runs, because the dynamic type is D
-
B
Undefined behaviour: only B's destructor runs, because B's destructor is not virtual
Correct answer
-
C
Both destructors run, since destructors are always virtual
-
D
A compile error, because a base pointer cannot delete a derived object
-
A
Marks x as const to prevent further modification
-
B
Deallocates x and returns its old storage
-
C
Casts x to an rvalue reference so it can be moved from
Correct answer
-
D
Transfers the bits of x immediately into the target object
-
A
Through a hash table keyed on the mangled function name
-
B
Through a per-object vptr into the class's vtable
Correct answer
-
C
By scanning the inheritance tree on every call
-
D
By storing the function body inside each object
-
A
Exactly one owner, with ownership transferred by move
Correct answer
-
B
Multiple owners, freed when the last owner releases its hold
-
C
No owner, with the pointer never freed
-
D
Shared ownership counted by a control block
-
A
A compile error reports the missing base-class conversion
-
B
The derived destructor runs before the copy
-
C
The derived part is sliced off, leaving a base copy
Correct answer
-
D
The whole derived object is copied intact
-
A
An exception of type std::out_of_range is thrown at once
-
B
A null reference is returned to the caller
-
C
The call fails to compile for missing keys
-
D
A default-constructed value is inserted for that key
Correct answer
-
A
Both return the same value after every operation
-
B
capacity counts only elements added with push_back
-
C
size is elements stored; capacity is allocated slots
Correct answer
-
D
size is allocated slots; capacity is elements stored
-
A
One per object, created with each constructor
-
B
One per translation unit that includes the header
-
C
One per derived class that inherits it
-
D
Exactly one, shared by every object of the class
Correct answer
-
A
The function returns a constant value
-
B
The function may only be called once per object
-
C
The object is copied before the call is made
-
D
The function cannot modify the object's members
Correct answer
-
A
It allows overriding functions declared final
-
B
It makes a signature mismatch a compile-time error
Correct answer
-
C
It forces the function to be inlined at call sites
-
D
It makes the base function non-virtual at run time
-
A
Widget(Widget other);
-
B
Widget& operator=(Widget other);
-
C
void Widget(const Widget& other);
-
D
Widget(const Widget& other);
Correct answer
-
A
So delete can free the object without a type check
-
B
So the derived destructor also runs during delete
Correct answer
-
C
So the destructor can be called twice safely
-
D
So the base object is never actually destroyed
-
A
<streambuf>
-
B
<string>
-
C
<iomanip>
-
D
<iostream>
Correct answer
-
A
Immediately after the constructor finishes
-
B
Whenever the object is passed to a function
-
C
When the object lifetime ends, such as scope exit
Correct answer
-
D
Only when the programmer calls delete explicitly on a pointer
-
A
A pointer to the function's own stack frame
-
B
A pointer to the object on which the member was called
Correct answer
-
C
A pointer to the class's static data area
-
D
A reference to the base class subobject of the current object
-
A
A uses-a relationship, where the base is a local variable
-
B
A copies-a relationship, duplicating the base layout
-
C
An is-a relationship, where the derived type specialises the base
Correct answer
-
D
A has-a relationship, where the base object is held as a member of the class