15 C++ interview questions for beginners — with answers you can say out loud

Short, honest answers to the questions that come up in junior C++ interviews. Run the examples as you read.

Language basics

1. What's the difference between a pointer and a reference?

A reference is an alias — it must be bound to something when created and can never be rebound or null. A pointer holds an address: it can be null, reassigned, and do arithmetic. Rule of thumb: use a reference when "there must be an object"; use a pointer when "there might be no object" or you need to move around.

2. Pass by value vs pass by reference — why does it matter?

Pass by value copies the argument, so changes inside the function don't affect the caller. Passing by reference gives the function the real object; big structs should be passed by const reference to avoid the copy cost. The demo below shows the contrast with a pointer (our in-browser engine doesn't support reference parameters yet — same idea: hand the function the real object's address instead of a copy):

#include <iostream>
using namespace std;

void tryToChange(int x)      { x = 99; }    // copy — caller unaffected
void actuallyChange(int* x)  { *x = 99; }   // pointer to the real object

int main() {
    int a = 1;
    tryToChange(a);
    cout << "after tryToChange: " << a << endl;
    actuallyChange(&a);
    cout << "after actuallyChange: " << a << endl;
    return 0;
}

3. What does const mean, and why do interviews love it?

const is a promise: "this won't be modified." const int* points to data you can't change through the pointer; int* const is a pointer that can't point elsewhere. Marking parameters and member functions const documents intent and lets the compiler catch mistakes.

4. Difference between declaration and definition?

A declaration says "this thing exists" (int add(int, int);); a definition provides the actual body or storage (int add(int a, int b) { return a + b; }). You can declare many times, define once. This is why headers exist.

5. What is #include really doing?

Literal text pasting: the preprocessor replaces the line with the contents of the header before compilation. That's why including the same header twice without include guards causes "redefinition" errors.

Memory — where juniors are separated from tourists

6. Stack vs heap?

The stack holds local variables; it's fast, automatically cleaned up, and limited in size. The heap is for objects whose lifetime you control (new/delete), is slower to allocate, and is where leaks live. Modern C++ says: prefer stack/automatic storage, and when you need the heap, own it through smart pointers.

7. What is a memory leak, in one sentence?

Heap memory that was allocated but will never be freed because you lost the last way to reach it. Prevention: RAII — tie every resource's lifetime to an object's scope (that's what std::unique_ptr and std::vector do).

8. What does new return that malloc doesn't?

A properly typed pointer — and it runs the object's constructor. malloc hands back raw uninitialized bytes (void*) and knows nothing about constructors. In modern code you rarely call either directly.

Control flow and functions

9. What's the difference between ++i and i++?

Both add one. ++i (pre-increment) increments and yields the new value; i++ (post-increment) yields the old value and increments. For class types, pre-increment can be cheaper (no temporary copy) — a favorite micro-detail question.

10. What is recursion, and what's its hidden cost?

A function calling itself, with a base case to stop. Hidden cost: each call consumes stack space — recursion without a tight base case overflows the stack (stack overflow!).

#include <iostream>
using namespace std;

int fib(int n) {
    if (n < 2) return n;              // base case
    return fib(n - 1) + fib(n - 2);   // recursive case
}

int main() {
    for (int i = 0; i <= 8; i++) {
        cout << fib(i) << " ";
    }
    cout << endl;
    return 0;
}

Follow-up they will ask: "what's the complexity of this Fibonacci?" Answer: O(2ⁿ) — it recomputes the same values; fixing it with memoization or iteration is the second half of the question.

11. Why is main special?

It's the program's entry point, and the standard guarantees two signatures: int main() and int main(int argc, char* argv[]) (command-line arguments). You don't need to call it — the runtime does.

Types and values

12. Integer division: what is 7 / 2 in C++?

3 — both operands are ints, so the result is an int (truncated toward zero). 7.0 / 2 gives 3.5 because one operand is a double. A classic bug source, so it's a classic question.

13. What is integer overflow and is it defined?

For signed integers, overflow is undefined behavior — the compiler may assume it never happens and optimize accordingly. Unsigned integers wrap around modulo 2ⁿ by definition.

14. Difference between struct and class?

Almost nothing: default access. Members of a struct are public by default; members of a class are private. Convention: use struct for plain data bundles, class when there are invariants to protect.

15. What happens if you forget return 0; in main?

Nothing bad — main is the one function that implicitly returns 0. Everywhere else, falling off the end of a non-void function is undefined behavior.

How to use this list

Related reading: C++ vector vs array — the container follow-up that almost always comes next.