1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. What does dynamic_cast<Derived*>(basePtr) return when the…

Added 30 Nov -0001

What does dynamic_cast&lt;Derived*&gt;(basePtr) return when the object is not actually a Derived?

  1. nullptr for both pointer and reference casts
  2. A pointer to the nearest base sub-object instead
  3. It causes undefined behaviour because the cast cannot be checked
  4. nullptr for a pointer cast, while the reference form throws std::bad_cast Correct answer

Correct answer

D. nullptr for a pointer cast, while the reference form throws std::bad_cast

14 views

MCQSEXAM

Explanation

dynamic_cast performs a run-time checked conversion using RTTI and needs a polymorphic source type, meaning at least one virtual function. Pointer failure is reported as nullptr so it can be tested, while reference failure throws std::bad_cast because a reference cannot be null. static_cast performs no check and is cheaper when the type is already known.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C++ and Object-Oriented Programming MCQs

Practise all C++ and Object-Oriented Programming MCQs

Similar MCQs