Two things: -l gives the long format (permissions, owner, group, size, timestamp per entry), and -a shows all entries including hidden dotfiles (names starting with ., plus the . and .. entries). Plain ls lists only the visible names.
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
};
Why can you use -> on a smart pointer even though it's an object, not a raw pointer?
Because the smart pointer overloads operator-> (and operator*). So p->method() calls p.operator->() to get the raw pointer, then applies -> to that — letting an object behave like a pointer.
If a program's pages are scattered all over physical RAM, how does it still behave as if memory were one contiguous block?
The page table maps each contiguous virtual page to whatever physical frame holds it, and the MMU performs that translation on every memory access. So the program always sees a clean, ordered, contiguous virtual layout, while the hardware quietly redirects each access to the scattered real location. Non-contiguous in RAM, contiguous to the program.
A size that is a compile-time constant: either a constexpr value, or a const integer initialized with a constant expression (e.g. const int n = 4*5;). A plain mutable int — even one obviously set to a fixed value — does not qualify and makes the array a (non-standard) VLA.
Can an abstract class also contain normal (non-pure) methods with bodies?
Yes. An abstract class can mix pure virtuals (which every subclass must implement) with ordinary virtual methods that have bodies (shared default behaviour). This is the Template Method pattern: the base defines a skeleton, forcing subclasses to fill in the pure-virtual steps while reusing the implemented ones.
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.
Given struct Entity { explicit Entity(int a); }; and void f(Entity);, which lines compile? Select all that apply.
explicit blocks implicit conversion: Entity e = 22; (copy-initialization) and f(22) both need it, so they fail. Direct construction Entity e(22); and the explicit Entity(22) still work.
Do both array and vector offer a bounds-checked access?
Yes — both provide .at(i) (bounds-checked, throws std::out_of_range) and operator[] (unchecked, fast). The choice between checked and unchecked access is the same for both containers.
auto a = std::make_shared<int>(5);
std::cout << a.use_count();
{
auto b = a;
std::cout << a.use_count();
}
std::weak_ptr<int> w = a;
std::cout << a.use_count();
One owner: count 1. Inside the block b shares ownership: 2. b dies at the closing brace: back to 1. A weak_ptr observes without owning, so the count stays 1.
Why must operator<< be a free function and return std::ostream&?
Free because its left operand is the stream (std::ostream), which you don't own and can't add members to. It returns the stream by reference so << calls chain: cout << a << b groups as (cout << a) << b, and each << must hand back the stream for the next one.
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.
int i = 0;
do { std::cout << i; } while (i++ < 2);
do-while runs the body first, then checks. Prints 0; 0 < 2 is true (i becomes 1). Prints 1; 1 < 2 is true (i becomes 2). Prints 2; 2 < 2 is false, so the loop ends.
.at()is bounds-checked: index 5 is outside a size-3 vector, so it throws std::out_of_range before anything is written, "ok" is skipped, and the catch prints caught.
free/delete are given only p — how do they know where the header is and how big the block is?
The header offset is a fixed constant compiled into the allocator, so it computes p - headerSize to reach the header from just p. The block size is stored inside that header, so it reads it once the header is found.
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.
a.cpp has static int helper = 5; at file scope. b.cpp says extern int helper; and uses it. What happens?
The link fails (undefined reference to 'helper'). File-scope static gives internal linkage: the name is private to a.cpp, so b.cpp's extern finds no definition.
One shared count for all objects: three constructions make it 3, and b was second, so b.id is 2. A static member lives outside the objects, so sizeof(Entity) is just the int id: 4.
int& f() { int x = 5; return x; }: x dies when f returns, so the reference points at a dead stack slot (g++ warns: reference to local variable 'x' returned). Returning by value, a static local, or a unique_ptr are all safe.
A prints, then throw jumps straight to the matching catch, so B never runs. The handler prints C and the message boom, and the program continues to print D.
What is the core difference between const and constexpr?
const means read-only after initialization — but the value may be computed at runtime. constexpr is stronger: the value (or function result) must be computable at compile time, so it can be used where the language requires a compile-time constant (array sizes, template arguments, switch cases). Every constexpr is implicitly const; not every const is constexpr.
Each function call pushes a stack frame (return address, saved registers, locals). The stack is small — typically only 1–8 MB. Deep or unbounded recursion keeps pushing frames until the stack is exhausted → stack overflow (a crash). This is one reason large data lives on the heap instead.
Where is the wasted space in internal fragmentation versus external fragmentation?
External fragmentation wastes memory in the gaps between allocations: enough total memory is free, but it's scattered in pieces too small (or separated by live allocations) to satisfy a large contiguous request. Internal fragmentation wastes memory inside an allocated block — the unused tail of the last page of a region. The names say it: between blocks (external) vs inside a block (internal).
Why don't OSes use a single flat page table (on x86-64)?
Because it would be enormous. With a 48-bit address and 4 KB pages there are 2^36 pages; a flat table of 8-byte entries is 2^36 × 8 = ~512 GB per process — impossible, and almost all empty since a process uses a tiny slice of its address space. Multi-level paging uses a sparse tree of small (512-entry) tables and creates only the sub-tables for regions actually in use, shrinking it to a few KB.
Memory: every polymorphic object stores a hidden vptr (~8 bytes on 64-bit), plus one vtable per class. Speed: each virtual call adds one indirection (load vptr → load slot → call) and usually can't be inlined, since the target is chosen at runtime. A class with no virtual functions has no vtable/vptr and pays nothing.