LearnThatStack Ace your next interview
Backend Development
C++.
Change topic Change
Practice · Questions

All questions

Showing of 59
Beginner 20
01

What is the difference between stack and heap memory?

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

Stack memory is managed for you by the compiler; heap memory you ask for and give back yourself.

The stack holds function locals and parameters. Each call pushes a frame, each return pops it, so cleanup is automatic and free. It is small, often one to eight megabytes, and blowing past it with deep recursion or a huge local array crashes the process.

The heap is a separate pool you allocate from at runtime. Objects there live until something frees them, so they can outlive the function that created them. Allocation costs a real call into the allocator, and forgetting to release leaks.

Default to the stack. Reach for the heap when the object must outlive the current scope. Also use it when the size is known only at runtime, or the object is too big for a frame.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

02

What is the difference between a pointer and a reference?

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

A reference is another name for an existing object; a pointer is an object that stores an address.

That difference drives everything else. A reference must be initialized when declared and can never be reseated to a different object. A pointer can be null, can be reassigned, and can be moved through an array with arithmetic. A pointer also has its own storage and its own address; a reference usually has neither.

Practically: use a reference when the thing must exist and will not change identity. Use a pointer when absence is meaningful, when you need to rebind, or when you walk memory.

References make the contract clearer at the call site. A function taking const std::string& promises it will not be handed nothing, so it needs no null check.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

03

What is the difference between a const pointer and a pointer to const?

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

Read the declaration from right to left and the two fall out cleanly. const binds to whatever sits on its left, unless nothing is there, in which case it binds right.

int x = 1, y = 2;
const int* p = &x;   // *p is read-only; p = &y is fine
int* const q = &x;   // *q = 5 is fine; q = &y will not compile

A pointer to const means you may not write through that pointer. The object itself may still change through some other non-const handle. A const pointer means the address is fixed for the pointer's life, so it must be initialized at declaration.

Pointer to const is the one you write constantly. It is how a function says it will read your data and not modify it. That lets callers pass constants safely, and lets the compiler catch an accidental write.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

04

What are smart pointers, and how do unique_ptr, shared_ptr and weak_ptr differ?

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

Smart pointers are class templates that own a heap object and free it automatically when they go out of scope. You get lifetime handling without ever writing delete.

unique_ptr models sole ownership. Exactly one owns the object, it cannot be copied, only moved, and it is as small and fast as a raw pointer. That makes it the default choice.

shared_ptr models shared ownership through a reference count. The object dies when the last owner does. It costs an extra control block and atomic count updates, so reach for it only when ownership has no single home.

weak_ptr owns nothing. It observes an object held by shared_ptr and tells you whether that object is still alive. You call lock() to get a usable shared_ptr, or nothing if it is gone.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

05

What is a memory leak, and how do you prevent one in C++?

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

A memory leak is heap memory you allocated and never released, with no pointer left to release it. Nothing crashes right away, which is what makes leaks nasty. A long-running server just grows until the allocator or the OS kills it.

The classic shapes are a new with no matching delete, and an early return that skips the cleanup. A thrown exception does the same thing. So does overwriting a pointer that held the only handle to a block.

The fix in modern C++ is structural, not a matter of discipline. Store owned objects in unique_ptr, shared_ptr or a standard container. Their destructors run cleanup on every exit path, including the ones you forgot about.

For what still slips through, build with the address sanitizer or run a leak checker. Both name the allocation site, which is far more useful than the symptom.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

06

What happens when a local object goes out of scope, and in what order?

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

Its destructor runs at the closing brace of the enclosing block, automatically. That happens whether you fall off the end, hit a return, or throw.

Order is strictly the reverse of construction. Locals die newest first, which matters when a later object depends on an earlier one. Inside one object, the destructor body runs, then members in reverse declaration order, then base classes.

{
  Logger a;
  Connection b(a);
}  // ~b runs, then ~a

That reverse ordering is what keeps dependent resources safe. A connection that logs during shutdown can count on the logger still being alive. One caveat: only automatic-storage objects get this. A heap object made with new is untouched by scope exit; just the pointer dies.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

07

What is a virtual function, and what does it let you do?

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

A virtual function is one the runtime picks based on the object's actual type. The type of the pointer or reference you call through does not decide it.

Declare it virtual in the base, override it in the derived class, then call through a base handle. The derived version runs. Without virtual, the call binds to the static type and your override is silently skipped.

This is what lets you write code against an interface. A render loop can hold a vector of Shape pointers and call draw() on each. It never learns whether they are circles or polygons, and adding a new shape leaves it untouched.

The cost is one indirection per call and, usually, no inlining. That is cheap but not free, so you do not mark everything virtual by reflex.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

08

What is a pure virtual function, and how does it differ from a virtual one?

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

A pure virtual function is declared with = 0 and has no required implementation in the base. It says every concrete derived class must supply one.

struct Shape {
  virtual double area() const = 0;  // no body, derived classes must provide one
};  // Shape s; fails to compile, Shape is abstract

The difference from a plain virtual function is obligation. A virtual function ships a default that derived classes may replace. A pure virtual ships nothing to fall back on. So the compiler refuses to instantiate the class, and refuses any derived class that leaves it unimplemented.

Any class with at least one pure virtual is abstract. You use it as an interface: hold objects through a Shape pointer or reference. The compiler then proves that no half-finished type ever gets created.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

09

What is move semantics, and what problem does it solve?

Part of Pro
10

What is an rvalue reference, and how does it differ from an lvalue reference?

Part of Pro
11

What does std::move actually do, and what does it not do?

Part of Pro
12

What does a move constructor do, and when is it chosen over copying?

Part of Pro
13

What are templates in C++, and how do function and class templates differ?

Part of Pro
14

What are the main categories of standard containers, and what separates them?

Part of Pro
15

What is the difference between a C array and a std::vector?

Part of Pro
16

What is the difference between a vector's size and its capacity, and how does it grow?

Part of Pro
17

What is an exception, and what do throw, try and catch each do?

Part of Pro
18

How do you define your own exception type, and what should it derive from?

Part of Pro
19

How does the C++ compilation and linking process work, stage by stage?

Part of Pro
20

What is the difference between a declaration and a definition, and why do headers matter?

Part of Pro
Intermediate 33
21

What is RAII, and why is it fundamental to modern C++?

Part of Pro
22

What is wrong with a raw pointer that owns the object it points to?

Part of Pro
23

When is shared_ptr the right choice rather than unique_ptr?

Part of Pro
24

How does shared_ptr keep track of how many owners an object has?

Part of Pro
25

What happens when two objects hold shared_ptr to each other, and how do you fix it?

Part of Pro
26

What happens when you pass a unique_ptr by value into a function?

Part of Pro
27

What is a dangling pointer, and how do you avoid creating one?

Part of Pro
28

How do new and delete differ from malloc and free?

Part of Pro
29

Why is allocating on the stack faster than allocating on the heap?

Part of Pro
30

What are the Rule of Three, the Rule of Five and the Rule of Zero?

Part of Pro
31

What is the difference between std::move and std::forward?

Part of Pro
32

Can an rvalue reference bind to an lvalue?

Part of Pro
33

When does the compiler generate a move constructor for you, and when not?

Part of Pro
34

What state is a moved-from object left in, and what can you do with it?

Part of Pro
35

Should you wrap a return value in std::move?

Part of Pro
36

How does the compiler actually implement virtual function dispatch?

Part of Pro
37

How many vtables exist for a class with five objects, and when are they created?

Part of Pro
38

Why does a base class need a virtual destructor, and what breaks without one?

Part of Pro
39

Can a destructor be pure virtual, and what does that require of you?

Part of Pro
40

Can a constructor be virtual in C++, and what do you do instead?

Part of Pro
41

What is object slicing, and when does it bite you?

Part of Pro
42

How do you choose between vector, list and deque for a given job?

Part of Pro
43

What is the difference between map and unordered_map, and when do you pick each?

Part of Pro
44

What is iterator invalidation, and which container operations cause it?

Part of Pro
45

How do iterators work, and why do the iterator categories matter?

Part of Pro
46

What is template specialization, and when do you actually need it?

Part of Pro
47

What is constexpr, and how is it different from const?

Part of Pro
48

What does the mutable keyword do, and when is using it justified?

Part of Pro
49

What are the usual causes of undefined behaviour in C++ code?

Part of Pro
50

What happens if you return a reference to a local variable?

Part of Pro
51

Can a constructor throw, and what happens to the resources it already holds?

Part of Pro
52

What happens between a throw and the catch that finally handles it?

Part of Pro
53

How do exceptions differ from error codes, and when do you prefer each?

Part of Pro
Expert 6
54

What is perfect forwarding, and how do you implement it correctly?

Part of Pro
55

Why is shared_ptr slower than unique_ptr or a raw pointer?

Part of Pro
56

Why does marking move operations noexcept matter so much in practice?

Part of Pro
57

How does make_shared differ from constructing a shared_ptr from a raw new?

Part of Pro
58

How do you make a copy assignment operator exception-safe?

Part of Pro
59

What is SFINAE, and when would you use it over template specialization?

Part of Pro

No matches

Try a different filter or search term.

Know someone prepping for C++? Send them this set.
Pro · $10/mo

51 of 59 C++ answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime

Change topic

Pick a different technology or stack. Your current topic stays put until you choose a new one.

Technologies
No technologies match “”.
Cross-cutting topics
No topics match “”.
By role
Stacks & frameworks

MEAN

MongoDB, Express, Angular, Node.js

MERN

MongoDB, Express, React, Node.js

LAMP

Linux, Apache, MySQL, PHP

Django

Python Full-Stack Development

Ruby on Rails

Convention over Configuration

Serverless on AWS

Serverless Architecture on AWS

Flutter Mobile

Flutter Cross-Platform Mobile Development

Spring Boot

Enterprise Java Development

.NET

Microsoft Ecosystem

Vue

Vue.js, Vite, TypeScript, Tailwind, Node.js

Go Backend

Golang, gRPC, PostgreSQL, Redis, RabbitMQ

FastAPI

Python, FastAPI, SQLAlchemy, PostgreSQL

React Native

React, TypeScript, Redux, Firebase

iOS Native

Swift, SwiftUI, UIKit, Firebase

Android Native

Java, Jetpack Compose, Firebase

DevOps / Platform

Docker, Kubernetes, Terraform, CI/CD

AI Engineer

LLMs, RAG, Agents, Evals

AI-Powered Developer

Claude Code, Copilot, Agentic Workflows

Core SWE Interview Prep

Data structures, algorithms, OS, concurrency, networking, git