1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. Base declares virtual void f(int x = 10); and Derived…

Added 30 Nov -0001

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

Correct answer

C. 10, because default arguments are bound statically from the pointer's type while the override is chosen dynamically

13 views

MCQSEXAM

Explanation

Default arguments are not virtual: they are substituted at compile time from the static type of the expression, so Base's value 10 is used even though Derived::f executes. This mismatch is a classic trap, and the usual advice is to keep defaults identical across an override hierarchy or avoid them in virtual functions altogether. Dispatch of the function body itself still resolves at run time.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is C? 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