1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. What is sizeof(Empty) for class Empty {}; in C++?

Added 30 Nov -0001

What is sizeof(Empty) for class Empty {}; in C++?

  1. 0, because the class has no members
  2. 4, because every object needs a vtable pointer
  3. 8 on 64-bit platforms, matching the pointer size
  4. 1, because distinct objects must have distinct addresses Correct answer

Correct answer

D. 1, because distinct objects must have distinct addresses

13 views

MCQSEXAM

Explanation

Object identity requires distinct objects to have distinct addresses, so an empty class occupies at least one byte and sizeof returns 1. A vtable pointer appears only when the class has virtual functions, so no hidden pointer exists here. The empty base optimisation can still make an empty base class consume zero bytes inside a derived object.

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