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

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

115 multiple choice questions with answers in Computer Science MCQs.

Showing 51–75 of 115

What makes a class abstract in C++?

  1. Declaring at least one pure virtual function, such as virtual void f() = 0; Correct answer
  2. Declaring the constructor protected, so only derived classes can create objects
  3. Making every member function virtual, so the class cannot be instantiated on its own
  4. Declaring all data members private, so no outside code can construct it
MCQSEXAM Read explanation

Given a std::map<std::string, int> named counts, what happens when counts['alpha'] is evaluated for a key that is not present?

  1. A new element with a value-initialised int of 0 is inserted, so a lookup silently grows the map Correct answer
  2. std::out_of_range is thrown, exactly as counts.at('alpha') would throw for a missing key
  3. A reference to a shared static zero is returned and the map itself is left unmodified
  4. An iterator to the end of the map is returned, and dereferencing it is undefined behaviour
MCQSEXAM Read explanation

void f(Base b); is called as f(derivedObject); where Derived publicly derives from Base and both override a virtual print(). What does b.print() call inside f?

  1. Base::print, because object slicing copies only the Base sub-object into the parameter Correct answer
  2. It is undefined behaviour, so either version may run depending on the optimisation level
  3. Derived::print, because the argument's dynamic type stays Derived inside the function
  4. Derived::print if Base's destructor is virtual, else Base::print, since dispatch depends on it
MCQSEXAM Read explanation

For objects a and b of class T, which special member is used by T b = a; and which by b = a; when b already exists?

  1. Copy-initialisation uses the copy constructor; assignment to an existing object uses the copy assignment operator Correct answer
  2. Both use the copy constructor, since the equals sign always means construction even for a live object
  3. Both use the copy assignment operator, since the equals sign always means assignment whatever the surrounding context
  4. T b = a; uses the default constructor followed by assignment, because b is an already constructed object
MCQSEXAM Read explanation

On an empty std::vector<int> v, what do v.reserve(100) and v.resize(100) each produce?

  1. Both set only capacity, differing solely in the exception guarantees offered when a reallocation fails
  2. reserve sets capacity to 100 with size still 0; resize sets size and capacity to 100 with value-initialised elements Correct answer
  3. Both set size to 100, but reserve leaves the new elements uninitialised until each element is written
  4. reserve sets size to 100 and resize only sets capacity, so writing v[0] directly after reserve is perfectly well defined
MCQSEXAM Read explanation

class D : public A, public B { Mem1 m1; Mem2 m2; public: D() : m2(2), m1(1), B(), A() {} }; In what order are bases and members initialised, and destroyed?

  1. m2, m1, B, A for initialisation, matching the initialiser list
  2. m1, m2, A, B for initialisation and A, B, m1, m2 for destruction
  3. A, B, m1, m2 for initialisation and the same order for destruction
  4. A, B, m1, m2 for initialisation and m2, m1, B, A for destruction Correct answer
MCQSEXAM Read explanation

class B { public: ~B() {} }; class D : public B { public: ~D() {} }; and then delete (B*)new D; is executed. What happens?

  1. Only D's destructor runs, because the dynamic type is D
  2. Undefined behaviour: only B's destructor runs, because B's destructor is not virtual Correct answer
  3. Both destructors run, since destructors are always virtual
  4. A compile error, because a base pointer cannot delete a derived object
MCQSEXAM Read explanation