ProgrammingModerateWipro · Capgemini pattern
What does sizeof print for an empty class in C++?
What does sizeof print for an empty class in C++?
class Empty { };
int main() { cout << sizeof(Empty); }- A. 0
- B. 8
- C. 1
- D. 4
Correct answer: C. 1
C++ requires distinct objects to have distinct addresses, so even an empty class occupies at least 1 byte. (As a base class it may take 0 bytes thanks to the empty-base optimisation.)
More OOP with C++ and Java questions
- What is printed? java System.out.print("apple".compareTo("apricot") + " " + "app".compareTo("apple") + " " + "b".compareTo("a"));
- What is printed? java class P { static void sm() { System.out.print("P"); } void im() { System.out.print("P"); } } class C extends P {…
- What is printed? java class P { int x = 10; int getX() { return x; } } class C extends P { int x = 20; int getX() { return x; } } // in…
- What is printed (default JVM settings)? java Integer x = 127, y = 127; Integer p = 128, q = 128; System.out.print((x == y) + " " + (p ==…
- What is printed? java // static methods of the main class: static void f(Object o) { System.out.print("Object"); } static void f(String s)…
- What is printed? java // static methods of the main class: static void f(long x) { System.out.print("long"); } static void f(Integer x) {…