Advertisement

Top 50 C++ interview questions and answers (2026)

The 50 C++ questions worth preparing first, picked from all 174 on W3Teacher and spread across 16 topics. Each one has a short answer here and a full answer with examples and follow-ups on its own page.

34Beginner 16Intermediate 174questions in total
Jump to: Beginner · Intermediate
Advertisement

Beginner C++ interview questions

Q1 Beginner Casting & Conversions

What are the four C++ named casts?

Short answer

C++ provides static_cast (compile-time, related-type conversions), dynamic_cast (safe downcasting in polymorphic hierarchies, checked at runtime), const_cast (add/remove const/volatile), and reinterpret_cast (low-level bit reinterpretation).

Full answer with examples
Q2 Beginner Compilation & Linking

What are the stages of building a C++ program?

Short answer

Building involves: preprocessing (handling #include/#define to produce a translation unit), compilation (translating each translation unit to an object file, checking syntax and types), and linking (combining object files and libraries, resolving symbol references, into an…

Full answer with examples
Q3 Beginner Concepts & Constraints

What are C++20 concepts?

Short answer

Concepts are named, compile-time predicates that constrain template parameters—specifying the requirements a type must satisfy (e.g., must be sortable, must support +).

Full answer with examples
Q4 Beginner Const Correctness

What does const mean in C++?

Short answer

const declares that something won't be modified after initialization. Applied to a variable, it makes it read-only; to a function parameter, it promises not to change the argument; to a member function, it promises not to modify the object.

Full answer with examples
Q5 Beginner Constexpr & Compile-Time

What does constexpr mean?

Short answer

constexpr indicates that a value or function can be evaluated at compile time. A constexpr variable is a compile-time constant; a constexpr function can be evaluated at compile time when given constant arguments (and at run time otherwise).

Full answer with examples
Q6 Beginner Coroutines

What is a coroutine in C++20?

Short answer

A coroutine is a function that can suspend its execution and resume later, preserving its local state across suspensions. A function becomes a coroutine if its body uses co_await, co_yield, or co_return.

Full answer with examples
Q8 Beginner I/O Streams

What are C++ I/O streams?

Short answer

C++ streams are a type-safe, extensible I/O abstraction in : std::cin (input), std::cout (output), std::cerr (unbuffered error), and std::clog (buffered log). They use operator > (extraction), which are overloadable for user types, unlike C's printf/scanf format strings.

Full answer with examples
Q9 Beginner Lambda Expressions

What is a lambda expression in C++?

Short answer

A lambda expression is an anonymous function object defined inline, introduced in C++11. It has the form [captures](params){ body } and can be stored, passed to algorithms, or called immediately. The compiler generates a unique closure type with an operator() containing the body.

Full answer with examples
Q11 Beginner Namespaces

What is a namespace and why use one?

Short answer

A namespace is a named scope that groups related declarations (functions, classes, variables) to prevent name collisions between different libraries or modules. Qualifying names with the namespace (ns::name) disambiguates them.

Full answer with examples
Q12 Beginner Preprocessor & Macros

What is the C++ preprocessor?

Short answer

The preprocessor is a text-processing step that runs before compilation, handling directives beginning with # : #include (textual inclusion of headers), #define (macros), and conditional compilation (#if/#ifdef/#endif).

Full answer with examples
Q13 Beginner Ranges & Views

What are C++20 ranges?

Short answer

Ranges (C++20) are an abstraction over 'something you can iterate'—anything with a begin and end—paired with algorithms that operate on whole ranges instead of iterator pairs.

Full answer with examples
Q15 Beginner Type Deduction & auto

What is the auto keyword?

Short answer

auto lets the compiler deduce a variable's type from its initializer, reducing verbosity and coupling to exact types. auto x = expr; gives x the type of expr (with top-level const and references stripped unless specified).

Full answer with examples
Q16 Beginner Undefined Behavior

What is undefined behavior (UB) in C++?

Short answer

Undefined behavior is program behavior the C++ standard places no requirements on—anything can happen: a crash, wrong results, or seemingly working code that breaks later or under optimization. The compiler may assume UB never occurs and optimize accordingly.

Full answer with examples
Q17 Beginner Casting & Conversions

What is static_cast used for?

Short answer

static_cast performs well-defined conversions known at compile time: numeric conversions (int to double), converting between related pointer types in a hierarchy (up or down without runtime check), void* to typed pointer, and explicit calls to conversion operators/constructors.

Full answer with examples
Q18 Beginner Compilation & Linking

What is a translation unit?

Short answer

A translation unit is a single source file after preprocessing—the .cpp file with all its #included headers textually expanded. The compiler processes one translation unit at a time into an object file.

Full answer with examples
Q20 Beginner Const Correctness

What is a const member function?

Short answer

A const member function (declared with const after the parameter list) promises not to modify the object's observable state and can be called on const objects. Inside it, member variables are treated as const (unless marked mutable).

Full answer with examples
Q21 Beginner Constexpr & Compile-Time

What is the difference between const and constexpr?

Short answer

const means the value won't change after initialization but it may be initialized at run time. constexpr means the value is a compile-time constant and must be initializable at compile time. All constexpr objects are const, but not all const objects are constexpr.

Full answer with examples
Q25 Beginner Lambda Expressions

What is the capture list in a lambda?

Short answer

The capture list [...] specifies which surrounding variables the lambda can use and how. [x] captures x by value (a copy), [&x] by reference, [=] captures all used variables by value, [&] all by reference, and [this] captures the enclosing object.

Full answer with examples
Q26 Beginner Memory Management

What do new and delete do?

Short answer

new allocates memory on the heap and constructs an object, returning a pointer; delete calls the destructor and frees the memory. For arrays use new[] and delete[]. Every new must be matched by exactly one delete (and new[] with delete[]) or you get leaks or undefined behavior.

Full answer with examples
Q29 Beginner Ranges & Views

What is a view in the ranges library?

Short answer

A view is a lightweight, non-owning, lazily-evaluated range that adapts another range without copying its elements. Views (like std::views::filter, std::views::transform) compute elements on demand as you iterate, so composing them doesn't create intermediate containers.

Full answer with examples
Q30 Beginner Strings & string_view

What is std::string_view and why was it added?

Short answer

std::string_view (C++17) is a non-owning, read-only view over a contiguous sequence of characters (a pointer plus length). It avoids copying when you only need to read a string, and can refer to std::string, C-strings, or substrings uniformly.

Full answer with examples
Q33 Beginner Memory Management

What is a memory leak?

Short answer

A memory leak occurs when heap memory is allocated but never freed, so it remains reserved for the program's lifetime, gradually exhausting available memory. Leaks happen when a delete is missed, a pointer is overwritten before freeing, or an exception skips the cleanup.

Full answer with examples
Q34 Beginner Preprocessor & Macros

What is an include guard and why is it needed?

Short answer

An include guard prevents a header's contents from being included more than once in a translation unit, which would cause redefinition errors. It uses #ifndef/#define/#endif around the header body, or the non-standard-but-widely-supported #pragma once.

Full answer with examples

Intermediate C++ interview questions

Q35 Intermediate Casting & Conversions

What is dynamic_cast and when do you use it?

Short answer

dynamic_cast safely converts pointers/references within a polymorphic class hierarchy (the base must have a virtual function), checking at run time using RTTI. On a pointer, it returns nullptr if the cast is invalid; on a reference, it throws std::bad_cast.

Full answer with examples
Q46 Intermediate Strings & string_view

What are the dangers of std::string_view?

Short answer

Because string_view doesn't own its data, it dangles if the underlying string is destroyed or modified (e.g., a view of a temporary, or of a std::string that reallocates). You must ensure the referenced data outlives the view.

Full answer with examples
Q48 Intermediate Undefined Behavior

What are common causes of undefined behavior?

Short answer

Common UB: out-of-bounds array/container access, dereferencing null or dangling pointers, use-after-free/double-free, reading uninitialized variables, signed integer overflow, data races (unsynchronized concurrent access), violating strict aliasing, invalid downcasts with…

Full answer with examples
Q50 Intermediate Compilation & Linking

What is the One Definition Rule (ODR)?

Short answer

The ODR states that every entity (variable, function, class, template) must have exactly one definition across the whole program (with specific exceptions: inline functions/variables and templates may be defined identically in multiple translation units).

Full answer with examples

Can you answer these under pressure?

Run a timed C++ mock interview: random questions, answer out loud, reveal the model answer and get a readiness score you can share.