LearnThatStack Ace your next interview
Backend Development

C++.

Start free Change topic Change
Practice · Questions

All questions

Showing of 65
Beginner 20
01

What is the difference between stack and heap memory?

Visual

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
Visual

The compiler manages stack memory for you, while heap memory is something you request and give back yourself.

The stack holds function locals and parameters. Each call pushes a frame, the block that holds that call's locals, and each return pops it. So cleanup is automatic and free. The stack is small, often one to eight megabytes. Deep recursion or a huge local array can overflow it and crash 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 memory you never release leaks.

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

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?

Visual

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
Visual

A reference is another name for an existing object, while 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, and a reference usually has neither.

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& always receives a real object, 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

A pointer to const cannot be used to change the object it points at. A const pointer cannot be made to point anywhere else. To tell them apart, read the declaration from right to left. 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 only stops writes through that pointer. The object itself may still change through some other non-const handle. A const pointer has a fixed address for its whole 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 manage 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. It can be moved but not copied. It is as small and fast as a raw pointer, which 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 use it only when no single object can be the owner.

weak_ptr owns nothing. It observes an object held by shared_ptr and can tell whether it is still alive. You call lock() to get a usable shared_ptr, which is empty if the object 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, so leaks are easy to miss. A long-running server just grows until the allocator or the operating system kills it.

The usual causes are a new with no matching delete, and an early return or a thrown exception that skips the cleanup. Overwriting the only pointer to a block leaks it too.

The fix in modern C++ is structural, not a matter of discipline. Store owned objects in unique_ptr, shared_ptr or a standard container, and their destructors free them on every exit path. For what still slips through, build with AddressSanitizer or run a leak checker. Both name the allocation site.

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 automatically when control leaves the enclosing block. That happens whether you reach the closing brace, return early, or throw.

The order is strictly the reverse of construction. Locals are destroyed 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

This order keeps dependent resources safe. A connection that logs during shutdown can rely on the logger still being alive.

Only objects with automatic storage get this, meaning ordinary locals, not static or heap objects. A heap object made with new is untouched by scope exit, and only the pointer to it is destroyed.

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 a member function whose call is resolved at run time by the object's dynamic type, the type it was created as. Declare it virtual in the base, override it in the derived class, then call through a base pointer or reference. The derived version runs. Without virtual, the call binds to the static type, the type of the pointer or reference, and your override is skipped.

This dynamic dispatch lets you code against an interface. A render loop can call draw() through Shape pointers without knowing the real shapes, so a new shape needs no change to the loop.

The cost is one indirection per call and, usually, no inlining. That is cheap but not free, so not every function should be virtual.

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 needs no 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 provides a default that derived classes may replace. A pure virtual function provides nothing to fall back on. So the compiler refuses to create an object of the class, or of any derived class that leaves the function unimplemented.

Any class with at least one pure virtual function is abstract. You use it as an interface by holding objects through a Shape pointer or reference.

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 Visual
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 Visual
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 Visual
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 Visual
Intermediate 38
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 Visual
25

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

Part of Pro Visual
26

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

Part of Pro Visual
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 Visual
36

How does the compiler actually implement virtual function dispatch?

Part of Pro Visual
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 Visual
41

What is object slicing, and when does it cause bugs?

Part of Pro Visual
42

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

Part of Pro Visual
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 Visual
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 Visual
51

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

Part of Pro Visual
52

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

Part of Pro Visual
53

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

Part of Pro
54

What are concepts in C++20, and how do they improve templates?

Part of Pro
55

What is a lambda in C++, and how do value and reference captures differ?

Part of Pro
56

How does virtual inheritance solve the diamond problem?

Part of Pro
57

What does std::atomic provide, and when do you still need a mutex?

Part of Pro Visual
58

What is std::string_view, and when can it dangle?

Part of Pro
Expert 7
59

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

Part of Pro
60

Why is shared_ptr slower than unique_ptr or a raw pointer?

Part of Pro
61

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

Part of Pro Visual
62

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

Part of Pro Visual
63

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

Part of Pro
64

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

Part of Pro
65

What is the Curiously Recurring Template Pattern (CRTP) used for?

Part of Pro

No matches

Try a different filter or search term.

Know someone prepping for C++? Send them this set.
Quick recap

C++ in pictures.

A quick recap of C++ in 32 short animated pictures, one idea at a time. About 8 min to get the whole topic back in your head before you walk in. Pause any picture, slow it down, or step through it.

Pro The first chapter is free to play. The other 28 pictures come with Pro.

Get Pro · $10/mo
Pro · $10/mo

57 of 65 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