OA. free
Free
MathWorks Data Structures & Algorithms Data Structures & Algorithms Medium

Object of Empty Class Is it possible to create an object of an empty class type such as: If...

MathWorks technical mcq question, verified with a worked answer. Free to practise - no sign-up.

Object of Empty Class

Is it possible to create an object of an empty class type such as:

class xyz
{};

If so, what is the size of such an object?

Options

Pick ONE option

Choose one option.
Show answer & explanation
Answer: B. Yes, it is possible, and its size is greater than zero.

In C++, an empty class is valid and compilable; you can create objects from it without errors. However, due to the C++ standard's requirement that every object must have a unique address in memory, even an empty class object has a minimum non-zero size (typically 1 byte). This ensures that two different objects have distinct memory addresses.

Step-by-step Derivation:
Empty classes are allowed in C++ and are syntactically and semantically valid. The compiler does not produce an error. At runtime, objects are created normally. The key insight is that while an empty class has no data members, the C++ Standard mandates that sizeof(object) must be at least 1 byte to guarantee unique addresses for distinct objects. This is why option B is correct: empty class objects are possible and have size > 0. Option A is incorrect because the size is never zero; option C and D are incorrect because no compilation or runtime errors occur.