Why must a reference be initialized at the point where it is declared?
Because a reference is a permanent alias with no identity of its own — it must be bound to an existing object the moment it's created, and can never be re-bound afterward. int& r; is a compile error; int& r = x; is required. (That's exactly why a reference makes a good fixed alias but a poor 'optional' or reseatable handle — use a pointer for those.)
Mark a method const whenever it doesn't modify the object's state — typically getters: int GetX() const;. The payoff: it can then be called on const objects and const references, and the compiler enforces the read-only promise (modifying a member inside a const method is an error unless the member is mutable). Missing const on getters is something interviewers notice.
byValue takes an Animalby value, so the Dog is sliced: only its Animal part is copied and name() returns animal. byRef keeps the real object, so the virtual call reaches Dog::name.
True or false: deleting a derived object through a base pointer is safe even when the base destructor is not virtual.
False. It is undefined behaviour: typically only the base destructor runs, so the derived part is never cleaned up (a leak, or worse). A polymorphic base needs a virtual destructor.
True or false: making a member private stops other code from reading that object's memory at runtime.
False.private is a compile-time rule: the compiler refuses direct access from other code. Nothing protects the bytes at runtime. Its purpose is controlled change and protecting invariants.
What does static do to a variable declared at file/global scope?
It gives the variable internal linkage — the symbol is visible only within that translation unit (that .cpp). Other files can't see or link to it, and two files can each have their own static int x; with no clash. This is the opposite of extern, which shares one symbol across files.
What are the ONLY two language-level differences between a class and a struct?
(1) Default member access: class members are private by default, struct members are public by default. (2) Default inheritance mode: class B : A is private inheritance by default, struct B : A is public by default. Everything else — constructors, methods, virtual functions, templates, inheritance — is identical.
True or false: a static member function can read the object's non-static data members.
False. A static member function has no this, so there is no object to read from; it can only use static members (or an object passed in). g++ rejects it.
Does a static data member increase each object's sizeof?
No. A static member is shared by all objects — there is exactly one copy in static storage, not one per object. So it doesn't live inside the object and doesn't count toward sizeof(obj). (An ordinary non-static member does live in each object and adds to its size.) A classic use is a counter of how many instances exist.
Why must an interface / polymorphic base declare a virtual destructor?
Because clients hold and delete derived objects through base pointers (Base* p = new Derived; ... delete p;). If the base destructor isn't virtual, delete p only runs ~Base() and the derived part leaks (undefined behaviour). A virtual ~Base() = default; ensures the full derived destructor chain runs.
Name two cases where a member initializer list is required, not merely preferred.
The : member(value), ... list (which initializes members directly, before the body) is required for: (1) const members — they must be initialized, never assigned; and (2) reference members — same reason (a reference must bind at creation). It's also required for member objects whose type has no default constructor.
C++
class A { constint id; int& ref;
A(int i, int& r) : id(i), ref(r) {} // must use init list
};
&x means 'address of x' — it produces the memory address where x lives. *p means 'dereference p' — go to the address stored in p and read/write the value there. They are inverses: *(&x) is just x.
C++
int x = 42;
int* p = &x; // p holds x's addressint y = *p; // y = 42 (value at that address)
*p = 99; // x is now 99
Given struct Shape { virtual double area() const = 0; };, which line does not compile?
Shape s;: a class with a pure virtual function is abstract and can't be instantiated. Pointers and references to it are fine, and so is a derived class that overrides area().
Which members must be initialized in the member initializer list? Select all that apply.
A const member and a reference member can't be assigned after construction, and a member object with no default constructor can't be built without arguments, so all three need the initializer list. A std::string has a default constructor, so it can be assigned in the body (the list is still preferred).
Why is stack allocation faster than heap allocation?
Stack memory is already committed to the process, so allocating a local is just moving the stack-pointer register down by the needed bytes; freeing is moving it back when the function returns — no searching, no bookkeeping. Heap allocation (new) must ask the allocator to search a free list for a suitable block, mark it used, and return it (and you must delete it later). That search + metadata make it slower.
If a class needs a custom destructor, it almost certainly also needs a custom copy constructor and copy assignment operator. Why: a class that owns a raw resource and defines only a destructor gets a compiler-generated shallow copy — two objects end up owning the same pointer, and when both destruct, the memory is freed twice (double-free). Defining all three (deep copy) prevents it. (C++11 adds move ctor/assignment → Rule of Five.)