C++ Programming · Practice by type
C++ Programming MCQ Questions and Answers
Multiple-choice questions on C++ Programming concepts and common errors, topic by topic, each with the answer and a clear explanation.
- 810 questions
- 27 topics
- Answers checked on a real compiler
Basic Syntax and Data Types 30 questions
- Which header declares std::cout in C++? Easy
- What does using namespace std; do? Easy
- What is the :: operator called in C++? Easy
- Is return 0; required at the end of main()? Medium
- Which size is guaranteed by the C++ standard? Medium
- What does int x{7.9}; do? Medium
- What type does auto deduce for auto n = 10u;? Easy
- const vs constexpr in C++ Medium
- std::endl vs '\n' – what is the difference? Easy
- Which is a valid C++ identifier? Easy
- What is the type of 'A' in C++ (and in C)? Medium
- Why use static_cast in C++? Easy
- What is std::cerr used for in C++? Easy
- Order of the stages that build a C++ program Easy
- Which escape sequence prints a tab? Easy
- Which of these is not a fundamental C++ type? Easy
- What value does int x{}; hold? Easy
- What does using Score = int; declare? Easy
- What is bool b = 5; in C++? Easy
- What happens when a signed int overflows? Medium
- Error: narrowing conversion in brace initialization Easy
- Error: cout used without std:: Easy
- Error: const variable without an initializer Easy
- Error: constexpr initialized from a run-time value Medium
- Error: auto variable without an initializer Easy
- Error: a C++ keyword used as a variable name Easy
- Error: modifying a const variable Easy
- Error: declaring the same name twice in one scope Easy
- Error: missing semicolon after a declaration Easy
- Error: using a variable outside its block Easy
Input/Output with cin and cout 30 questions
- What are cin and cout in C++? Easy
- What is << called when used with cout? Easy
- Why can cout << a << b be chained? Medium
- What does cin >> do with leading spaces and newlines? Easy
- How to read a full name with spaces in C++ Easy
- Why does getline after cin >> read an empty line? Medium
- What does std::ws do? Medium
- Which header provides setw and setprecision? Easy
- Which manipulator applies only to the next value? Medium
- What does setprecision mean with std::fixed? Medium
- What happens when cin reads "abc" into an int? Medium
- Why call cin.clear() and cin.ignore() after bad input? Medium
- When does while (std::cin >> x) stop? Easy
- cin.get(c) vs cin >> c for a char Medium
- cerr vs clog in C++ Easy
- What is std::istringstream used for? Easy
- What does sync_with_stdio(false) do? Medium
- Which header provides std::println in C++23? Easy
- How to print a literal { with std::format Medium
- What does cin.peek() do? Medium
- Error: cin used with << instead of >> Easy
- Error: cout used with >> Easy
- Error: getline into an int variable Medium
- Error: reading into a string literal Easy
- Error: endl used without std:: after using std::cout Medium
- Error: reading input into a const variable Easy
- Error: printing a struct with cout Medium
- Error: std::format with {:d} for a string Hard
- Error: getline with a string as delimiter Medium
- Error: copying std::cout into a variable Hard
Functions and Default Arguments 30 questions
- Function declaration vs definition in C++ Easy
- Does changing a by-value parameter affect the caller? Easy
- Why pass a std::string by const reference? Easy
- Can a literal be passed to an int& parameter? Medium
- Which default-argument declaration is valid? Easy
- Can C++ functions be overloaded by return type only? Easy
- Function overloading is which kind of polymorphism? Easy
- Which overload is called for f('A')? Medium
- Why is f(2.5) ambiguous with f(int) and f(long)? Medium
- What happens if a recursive function has no base case? Easy
- Lifetime of a static local variable Easy
- What does inline mean in modern C++? Medium
- When is a constexpr function evaluated? Medium
- What happens if a noexcept function throws? Hard
- What does [[nodiscard]] do? Easy
- Returning a reference to a local variable Medium
- How to unpack a returned pair in C++17 Easy
- Why does C++ use name mangling? Hard
- What can std::function<int(int)> hold? Medium
- What is auto f(int x) -> double? Medium
- Error: default argument before a parameter without one Easy
- Error: default argument repeated in the definition Medium
- Error: ambiguous call between int and long overloads Medium
- Error: overloads that differ only by return type Easy
- Error: passing a literal to an int& parameter Easy
- Error: calling a function before it is declared Easy
- Error: returning a value from a void function Easy
- Error: consteval function called with a run-time value Hard
- Error: modifying a const reference parameter Easy
- Error: defining a function inside another function Medium
Arrays and Vectors 30 questions
- What is the index of the first array element? Easy
- int a[5] = {1, 2}; – what are the other elements? Easy
- What is array-to-pointer decay? Medium
- How to get the number of elements of a C array in C++17 Easy
- How is a 2D array stored in memory? Medium
- What can std::array do that a C array cannot? Easy
- Main advantage of std::vector over arrays Easy
- size() vs capacity() of a vector Easy
- reserve(100) vs resize(100) on an empty vector Medium
- Time complexity of vector::push_back Medium
- emplace_back vs push_back Medium
- v.at(i) vs v[i] for an invalid index Easy
- What can happen to iterators after push_back? Hard
- What does the erase-remove idiom do? Medium
- Does vector::clear() free memory? Medium
- Are std::vector elements contiguous? Easy
- What is special about std::vector<bool>? Hard
- std::vector<int> v(3, 5) vs v{3, 5} Medium
- What is std::span used for? Medium
- Value of an uninitialized local array Easy
- Error: array size from a variable (VLA) Medium
- Error: assigning one C array to another Easy
- Error: more initializers than array elements Easy
- Error: a function returning a C array Medium
- Error: std::array<int> without a size Easy
- Error: std::vector of references Hard
- Error: push_back on a const vector Easy
- Error: modifying elements through const auto& Easy
- Error: C array with too many initializers Easy
- Error: passing std::array of the wrong size Medium
std::string 30 questions
- Which header declares std::string? Easy
- Advantage of std::string over char arrays Easy
- Why does "abc" + "def" not compile? Medium
- size() vs length() of a std::string Easy
- What does find() return when nothing is found? Easy
- What do the arguments of substr mean? Easy
- What does s.erase(5) do? Medium
- s.at(i) vs s[i] for strings Easy
- Why is "Zebra" < "apple" true? Medium
- What does std::stoi("hello") do? Medium
- What does std::to_string(2.5) return? Medium
- Does std::string_view own its characters? Medium
- What is c_str() used for? Easy
- How to test a prefix in C++20 Easy
- What is the type of "hi"s? Medium
- Can a std::string be modified in place? Easy
- Reading a full line into a std::string Easy
- What does find_first_of do? Medium
- What does s += 65 append? Hard
- How to reverse a std::string Easy
- Error: adding two string literals Easy
- Error: initialising a std::string from a char with = Medium
- Error: switch on a std::string Medium
- Error: writing through a std::string_view Medium
- Error: comparing a std::string with a char Easy
- Error: s.length used without () Easy
- Error: passing std::string to strlen Easy
- Error: std::to_string called with a string Medium
- Error: push_back on a const std::string Easy
- Error: assigning substr() to a char* Medium
Pointers and Dynamic Memory 30 questions
- What does a pointer variable store? Easy
- What does *p mean when p is a pointer? Easy
- Why prefer nullptr to NULL? Medium
- int* a, b; – what is b? Medium
- How far does p + 1 move for an int pointer? Medium
- a[i] is equivalent to which expression? Easy
- What does const int* p allow? Medium
- What does int* const p allow? Medium
- What is p->name equivalent to? Easy
- new vs malloc in C++ Medium
- How to free memory from new int[10] Easy
- Is deleting a null pointer safe? Easy
- What is a memory leak? Easy
- What is a dangling pointer? Easy
- Can a void* be dereferenced? Medium
- Size of pointers on a 64-bit system Easy
- Stack vs heap memory in C++ Medium
- What does RAII mean? Medium
- Why pass int** to a function? Medium
- What happens when new cannot allocate memory? Medium
- Error: assigning an integer to a pointer Easy
- Error: int* pointing to a const int Medium
- Error: writing through a const int* parameter Easy
- Error: moving an int* const Easy
- Error: int* pointing to a double Easy
- Error: dereferencing a void* Medium
- Error: delete applied to a reference Medium
- Error: arithmetic on a void* Hard
- Error: using memory after delete Medium
- Error: dereferencing a null pointer Easy
References: lvalue and rvalue 30 questions
- Must a reference be initialized? Easy
- What is a C++ reference? Easy
- What does r = y do to a reference? Medium
- Which can be null: a reference or a pointer? Easy
- Which reference can bind to a temporary? Medium
- Why take a parameter as const T&? Easy
- Why a returned reference to a local dangles Medium
- What does sizeof give for a reference? Medium
- Can you create an array of references? Medium
- Which expression is an lvalue? Medium
- What can int&& bind to? Medium
- What does std::move actually do? Medium
- State of an object after std::move Medium
- Is a named T&& parameter an lvalue or rvalue? Hard
- When is T&& a forwarding reference? Hard
- Why use std::forward? Hard
- Reference collapsing: T& && becomes? Hard
- Why pass std::ref(x) to std::thread? Medium
- for (auto x : v) vs for (auto& x : v) Easy
- What happens when you std::move a const object? Hard
- Error: a reference declared without an initializer Easy
- Error: int& bound to a literal Easy
- Error: int&& bound to a named variable Medium
- Error: changing a value through a const reference Easy
- Error: std::vector<std::string&> Medium
- Error: int& bound to a const int Easy
- Error: declaring a pointer to a reference Hard
- Error: binding a reference of the wrong type Medium
- Error: std::thread with an int& parameter Hard
- Error: int& bound to a function returning by value Medium
OOPS Concepts 30 questions
- What are the four pillars of OOP? Easy
- Class vs object in C++ Easy
- What does encapsulation mean? Easy
- Abstraction vs encapsulation Medium
- Which relationship does inheritance model? Easy
- Types of polymorphism in C++ Easy
- Default member access of class and struct Easy
- Who can access protected members? Medium
- What is the purpose of a constructor? Easy
- In what order are local objects destroyed? Medium
- What is the this pointer? Easy
- How many copies of a static data member exist? Medium
- How is a virtual function call resolved? Medium
- Can an abstract class be instantiated? Easy
- Function overloading vs overriding Medium
- Which design uses composition? Easy
- What is object slicing? Medium
- Why give a base class a virtual destructor? Medium
- What can a friend function do? Medium
- What does the Liskov substitution principle state? Hard
- Error: accessing a private member from main Easy
- Error: creating an object of an abstract class Easy
- Error: calling a non-const member on a const object Medium
- Error: missing semicolon after a class definition Easy
- Error: a derived class using a private base member Medium
- Error: override on a function that overrides nothing Medium
- Error: using a non-static member in a static function Medium
- Error: creating an object with a private constructor Medium
- Error: deriving from a final class Medium
- Error: initializing a derived object from a base object Medium
Classes and Objects 30 questions
- How must a class definition end? Easy
- Defining a member function outside the class Easy
- Are member functions defined in the class inline? Medium
- What is a default member initializer? Easy
- What does const after a member function mean? Easy
- What does mutable allow? Medium
- Type of this inside a const member function Hard
- Why write this->name = name? Easy
- How to define a static data member in the class (C++17) Medium
- What can a static member function not do? Medium
- What is sizeof an empty class? Medium
- Do member functions add to an object's size? Medium
- When is a static local object constructed? Medium
- What can you do with a forward-declared class? Hard
- Which class is an aggregate? Medium
- Rules for designated initializers in C++20 Medium
- What does a defaulted operator<=> give you? Medium
- How do you refer to a nested class? Easy
- Is friendship inherited or mutual? Medium
- What does copying an object do by default? Easy
- Error: member function defined without ClassName:: Easy
- Error: a const member function changing a member Easy
- Error: reaching a private member through a pointer Easy
- Error: calling a non-static member through the class name Medium
- Error: initializing a non-const static member in the class Medium
- Error: creating an object of an incomplete class Medium
- Error: definition differs from declaration in const Medium
- Error: designated initializers out of order Medium
- Error: comparing objects without an operator Easy
- Error: using a private nested class outside Medium
Constructors and Destructors 30 questions
- What is the return type of a constructor? Easy
- When is the implicit default constructor not generated? Medium
- Which members must be in the initializer list? Medium
- In which order are members initialized? Medium
- What is a delegating constructor? Medium
- What does explicit prevent? Easy
- Why does a copy constructor take const T&? Medium
- Copy constructor or copy assignment? Easy
- What goes wrong with a shallow copy of an owning pointer? Medium
- What does the rule of three say? Medium
- What is the rule of zero? Medium
- What does a move constructor do? Easy
- Why mark move constructors noexcept? Hard
- What do = default and = delete do? Easy
- Which is true about destructors? Easy
- Order of destruction inside an object Medium
- Why make a destructor virtual? Medium
- Can a constructor be virtual? Medium
- What is guaranteed copy elision? Hard
- Which constructor do braces prefer? Hard
- Error: copy-initialization with an explicit constructor Easy
- Error: a const member assigned in the constructor body Medium
- Error: a reference member never bound Medium
- Error: copying a class with a deleted copy constructor Easy
- Error: no default constructor available Easy
- Error: a constructor declared with a return type Easy
- Error: delegating constructor that also initializes a member Hard
- Error: copying a class that holds a unique_ptr Medium
- Error: a destructor with a parameter Easy
- Error: declaring a virtual constructor Medium
Static and Friend Members 30 questions
- How many copies of a static member exist? Easy
- Declaring vs defining a static data member Medium
- What does static inline do for a data member? Medium
- Why has a static member function no this? Easy
- Can a static member function be const or virtual? Medium
- Can a static member function access private members? Hard
- Do static members change sizeof an object? Easy
- Is a base class static member shared with derived classes? Medium
- Static members of class template instantiations Medium
- Is static local initialization thread-safe? Medium
- What makes a Meyers singleton? Medium
- What is the static initialization order fiasco? Hard
- Is a friend function a member of the class? Easy
- Does it matter where a friend is declared? Medium
- Is friendship mutual? Easy
- Is friendship inherited? Medium
- Why write operator+ as a non-member friend? Medium
- Why can't operator<< be a member of your class? Medium
- Befriending a member function of another class Hard
- How is a hidden friend found? Hard
- Error: a static function reading a non-static member Easy
- Error: a static member function marked const Medium
- Error: using this in a static member function Easy
- Error: a virtual static member function Medium
- Error: calling a friend function like a member Easy
- Error: assuming a derived class inherits friendship Medium
- Error: a friend of a friend accessing private data Medium
- Error: befriending a member of a class not yet defined Hard
- Error: using a static member without its class name Easy
- Error: calling a hidden friend without a class argument Hard
Encapsulation 30 questions
- Which statement best defines encapsulation? Easy
- How is data hiding achieved in C++? Easy
- What is a class invariant? Medium
- How should a constructor handle invalid arguments? Medium
- Accessing private members of another object Medium
- The protected access rule for derived classes Hard
- Why getters and setters everywhere are weak Medium
- What does "tell, don't ask" mean? Medium
- Which getter breaks encapsulation? Medium
- How do const member functions help encapsulation? Easy
- When is mutable appropriate? Medium
- What is a benefit of immutable objects? Medium
- What does a const data member do to assignment? Hard
- Why are protected data members discouraged? Medium
- What does private inheritance express? Hard
- What problem does the pimpl idiom solve? Hard
- Why must a pimpl class define its destructor in the .cpp? Hard
- Encapsulation vs abstraction Medium
- When is a struct with public data appropriate? Easy
- How to expose a private vector read-only without copying Medium
- Error: using a protected member from main Easy
- Error: modifying data through a const reference getter Easy
- Error: calling a private helper function from outside Easy
- Error: calling a setter on a const object Easy
- Error: writing through a pointer-to-const getter Medium
- Error: assigning to an object with const members Medium
- Error: protected member via a base-class object Hard
- Error: calling a base member hidden by private inheritance Medium
- Error: using a private nested enum outside the class Medium
- Error: using pimpl members where Impl is incomplete Hard
Inheritance 30 questions
- Which relationship does public inheritance express? Easy
- Default inheritance mode for class and struct Medium
- A public base member under protected inheritance Medium
- Are private base members inherited? Easy
- Which is multilevel inheritance? Easy
- Which constructor runs first? Easy
- How does a derived class call a base constructor? Easy
- What if the base has no default constructor? Medium
- What does using Base::Base; do? Medium
- What is name hiding in inheritance? Medium
- How to call the base version of a member function Easy
- Is upcasting safe? Easy
- What does a failed dynamic_cast return? Medium
- When does object slicing happen? Medium
- Order of construction with multiple bases Medium
- Resolving an ambiguous member from two bases Medium
- What is the diamond problem? Medium
- Who constructs a virtual base class? Hard
- What does final on a class mean? Easy
- What is not inherited by a derived class? Medium
- Error: an ambiguous member from two base classes Easy
- Error: ambiguous base in a non-virtual diamond Medium
- Error: base class without a default constructor Easy
- Error: converting to a privately inherited base Medium
- Error: calling a base overload hidden by the derived class Medium
- Error: overriding a function marked final Medium
- Error: static_cast downcast from a virtual base Hard
- Error: base member in the derived initializer list Medium
- Error: public base member hidden by protected inheritance Medium
- Error: inheriting from a class that is only declared Easy
Polymorphism and Virtual Functions 30 questions
- How is run-time polymorphism achieved in C++? Easy
- Which is compile-time polymorphism? Easy
- What does virtual change about a function call? Easy
- Is a call on an object by name dynamically dispatched? Medium
- What does the override keyword do? Easy
- What does final do on a virtual function? Easy
- vtable and vptr: per class or per object? Medium
- How do virtual functions affect object size? Medium
- When is a virtual destructor required? Easy
- Calling a virtual function from a constructor Medium
- Default arguments of virtual functions Hard
- What is a covariant return type? Medium
- What does = 0 do to a virtual function? Easy
- When does a derived function hide instead of override? Medium
- Which functions cannot be virtual? Medium
- Why does passing by value break polymorphism? Medium
- What does dynamic_cast require? Medium
- What is CRTP? Hard
- When to use std::variant instead of virtual functions? Hard
- What is the non-virtual interface (NVI) idiom? Hard
- Error: override with a different parameter type Easy
- Error: pure virtual function not overridden Easy
- Error: override returning an unrelated pointer type Medium
- Error: derived-only function called via a base pointer Easy
- Error: = 0 on a non-virtual function Medium
- Error: a virtual member function template Hard
- Error: dynamic_cast downcast on a non-polymorphic class Medium
- Error: std::visit with a visitor missing one alternative Hard
- Error: an abstract class as a by-value parameter Medium
- Error: override changing the return type Easy
Abstraction 30 questions
- What does abstraction mean in C++? Easy
- Abstraction versus encapsulation Easy
- What is an abstract data type? Easy
- How is an interface written in C++? Easy
- Which is NOT a way to achieve abstraction? Easy
- Why program to an interface? Medium
- Why use the pimpl idiom for abstraction? Medium
- Where must a pimpl class define its destructor? Hard
- What does RAII abstract away? Easy
- What is a leaky abstraction? Medium
- What do C++20 concepts provide? Medium
- What is the zero-overhead principle? Medium
- How do header files support abstraction? Easy
- Are getters and setters for every field good abstraction? Medium
- What is control abstraction? Medium
- What can change behind a good abstraction? Easy
- Why use strong types like Metres and Seconds? Medium
- Which abstraction lets std::find work on any container? Medium
- Run-time vs compile-time abstraction Hard
- The four pillars of OOP Easy
- Error: using a hidden data member Easy
- Error: creating an object of an interface Easy
- Error: a type that does not satisfy a concept Medium
- Error: using an incomplete pimpl type Hard
- Error: passing a raw number to a strong type Medium
- Error: modifying state in a const query Easy
- Error: calling a private helper from outside Easy
- Error: returning an interface by value Medium
- Error: an implementation that misses one interface function Easy
- Error: calling a constrained template with the wrong type Medium
Abstract Classes and Interfaces 30 questions
- What makes a C++ class abstract? Easy
- Syntax of a pure virtual function Easy
- Can you create an object of an abstract class? Easy
- Can an abstract class have a constructor? Easy
- Can an abstract class have data and normal functions? Easy
- When does a derived class stay abstract? Easy
- What is an interface in C++? Easy
- Must a pure virtual destructor be defined? Medium
- Can a pure virtual function have a body? Medium
- Implementing several interfaces Easy
- Abstract class as a function parameter Medium
- Why do interfaces need a virtual destructor? Medium
- Calling a pure virtual function from a constructor Hard
- Checking abstractness at compile time Medium
- Size of an interface object Medium
- What is the interface segregation principle? Medium
- Abstract class versus interface Medium
- C++20 concepts compared with interfaces Hard
- Using final on an interface implementation Medium
- Why make an interface constructor protected? Hard
- Error: new on an abstract class Easy
- Error: a vector of an abstract type Medium
- Error: a pure virtual destructor without a definition Hard
- Error: an abstract class as a data member Medium
- Error: override of a function the interface does not declare Easy
- Error: missing const leaves the class abstract Medium
- Error: a function returning an abstract class by value Medium
- Error: an array of interface objects Easy
- Error: a type missing a function required by a concept Medium
- Error: implementing an interface with private inheritance Hard
Operator Overloading 30 questions
- What is operator overloading? Easy
- Which operator cannot be overloaded? Easy
- Can the scope resolution operator be overloaded? Easy
- Which operators must be member functions? Medium
- Why is operator<< usually a friend function? Easy
- How is postfix ++ declared? Easy
- What should prefix ++ return? Medium
- What should operator= return? Easy
- Does overloading change precedence? Easy
- Can you overload operator+ for two ints? Medium
- Making 2 * v and v * 2 both work Medium
- What does a defaulted <=> give you? Medium
- What is a function object (functor)? Easy
- Why make operator bool explicit? Medium
- Overloading && and || Hard
- Why provide two versions of operator[]? Medium
- Implementing + using += Medium
- Rules for user-defined literal suffixes Medium
- What must operator-> return? Hard
- Operator overloading and polymorphism Easy
- Error: trying to overload the conditional operator Easy
- Error: overloading an operator for two ints Easy
- Error: a member operator+ with two parameters Medium
- Error: operator<< written as a member Medium
- Error: operator[] as a non-member function Medium
- Error: using an explicit conversion implicitly Medium
- Error: inventing a new operator Easy
- Error: calling a non-const operator on a const object Medium
- Error: postfix ++ with the wrong dummy parameter Hard
- Error: user-defined literal with the wrong literal type Hard
Templates 30 questions
- What problem do templates solve? Easy
- typename versus class in a template header Easy
- When is template code generated? Easy
- Why does max(3, 2.5) fail with one T? Medium
- What is class template argument deduction? Medium
- What can a non-type template parameter be? Medium
- Syntax of a full template specialisation Medium
- Can function templates be partially specialised? Hard
- Counting arguments in a parameter pack Medium
- What does (xs + ...) do? Medium
- if constexpr versus a normal if in templates Medium
- What is a C++20 concept? Easy
- What does SFINAE stand for? Hard
- Why are templates defined in header files? Easy
- When is typename required inside a template? Hard
- Templates versus virtual functions Medium
- What causes template code bloat? Medium
- What is a forwarding reference? Hard
- Templates compared with macros Easy
- Abbreviated function templates in C++20 Medium
- Error: conflicting types for one template parameter Easy
- Error: a template using an operation the type lacks Medium
- Error: a run-time value as a template argument Easy
- Error: a dependent type name without typename Hard
- Error: calling a constrained template with a wrong type Easy
- Error: partial specialisation of a function template Hard
- Error: class template arguments that cannot be deduced Medium
- Error: a unary fold over an empty pack Hard
- Error: a virtual function template in a class template Medium
- Error: specialising a template after it was used Hard
STL Containers 30 questions
- Which container should be the default choice? Easy
- What data structure does std::map use? Easy
- Average lookup cost in unordered_map Easy
- Does std::set keep duplicates? Easy
- What does deque offer over vector? Easy
- Does std::list support operator[]? Easy
- What does map[key] do for a missing key? Medium
- Which element is on top of a priority_queue? Easy
- Underlying container of std::stack Medium
- When are vector iterators invalidated? Medium
- Iterator stability in std::list Medium
- Requirements for an unordered_map key type Medium
- emplace_back versus push_back Medium
- What does vector::reserve do? Easy
- set::find versus std::find on a set Medium
- Iterating over a std::queue Medium
- std::array compared with std::vector Easy
- When to use std::multimap Medium
- What is std::span? Medium
- What does set::lower_bound return? Medium
- Error: std::sort on a std::list Medium
- Error: operator[] on a const map Medium
- Error: a custom key type without a hash Medium
- Error: a map key type without operator< Medium
- Error: range-for over a std::stack Easy
- Error: modifying an element of a std::set Medium
- Error: std::array with a run-time size Easy
- Error: calling front() on a priority_queue Easy
- Error: push_front on a vector Easy
- Error: writing through a span of const elements Medium
STL Algorithms and Iterators 30 questions
- What does end() point to? Easy
- What does std::find return when nothing matches? Easy
- Which iterators does std::sort require? Medium
- Time complexity of std::sort Easy
- What does stable_sort guarantee? Easy
- Why does std::remove not shrink a vector? Medium
- What does lower_bound return? Medium
- binary_search on unsorted data Medium
- What is std::back_inserter for? Easy
- What does std::unique remove? Medium
- The initial value type in accumulate Medium
- When is nth_element useful? Hard
- Why must comparators not use <=? Hard
- Iterator category of std::list Medium
- What does std::transform do? Easy
- What is a projection in std::ranges algorithms? Medium
- When does a range view do its work? Medium
- accumulate versus reduce Hard
- Generating all permutations Medium
- Cost of std::distance on different iterators Hard
- Error: sorting a std::set with std::sort Medium
- Error: iterators of different types as one range Medium
- Error: it + 2 on a list iterator Easy
- Error: writing results into a const container Medium
- Error: a predicate with the wrong number of parameters Easy
- Error: std::max with an int and a double Easy
- Error: std::ranges::sort on a list Medium
- Error: std::accumulate without an initial value Easy
- Error: back_inserter for a std::array Medium
- Error: writing through a transform view Hard
Lambda Expressions 30 questions
- What is a lambda expression in C++? Easy
- Which part of a lambda is the capture list? Easy
- When is a by-value capture copied? Easy
- What does [&] capture? Easy
- Why is mutable needed in a lambda? Medium
- Do two identical lambdas have the same type? Medium
- Which lambdas convert to function pointers? Medium
- What is a generic lambda? Easy
- What is an init capture used for? Medium
- [this] versus [*this] Hard
- Returning a lambda that captures a local by reference Medium
- Do lambdas need to capture global variables? Medium
- When to use std::function Medium
- How is a lambda's return type determined? Easy
- What does the compiler turn a lambda into? Medium
- Writing a recursive lambda in C++23 Hard
- Why lambdas are fast in std::sort Hard
- Capturing this through [=] in C++20 Hard
- Does mutable state persist between calls? Medium
- Can a lambda be used at compile time? Medium
- Error: using a local variable that was not captured Easy
- Error: modifying a by-value capture without mutable Easy
- Error: a capturing lambda as a function pointer Medium
- Error: capturing a unique_ptr by copy Medium
- Error: return statements with different types Medium
- Error: a lambda calling itself by name Medium
- Error: assigning lambdas of different types to one variable Medium
- Error: storing a lambda with the wrong signature Easy
- Error: capturing a data member by name Hard
- Error: constrained generic lambda with the wrong type Hard
Smart Pointers 30 questions
- What problem do smart pointers solve? Easy
- Can a unique_ptr be copied? Easy
- State of a unique_ptr after std::move Easy
- When does shared_ptr delete its object? Easy
- Does weak_ptr change use_count? Easy
- What does weak_ptr::lock return? Medium
- How do you break a shared_ptr cycle? Medium
- Advantage of make_shared Medium
- release() versus reset() Medium
- Deleting the pointer returned by get() Medium
- Two shared_ptrs from the same raw pointer Hard
- Why use enable_shared_from_this? Hard
- Which smart pointer should be the default? Easy
- What happened to std::auto_ptr? Easy
- unique_ptr for a dynamic array Medium
- How should a function take an object it only reads? Medium
- Overhead of unique_ptr Medium
- Thread safety of shared_ptr Hard
- What is a custom deleter for? Medium
- Converting unique_ptr to shared_ptr Medium
- Error: copying a unique_ptr Easy
- Error: passing a unique_ptr by value without std::move Easy
- Error: assigning a raw pointer to a unique_ptr with = Medium
- Error: copying a vector of unique_ptr Medium
- Error: dereferencing a weak_ptr directly Easy
- Error: pointer arithmetic on a unique_ptr Medium
- Error: operator[] on a shared_ptr to a single object Medium
- Error: a deleter type mismatch between unique_ptrs Hard
- Error: make_unique of an abstract class Easy
- Error: moving from a const unique_ptr Medium
Move Semantics 30 questions
- What is the goal of move semantics? Easy
- Does std::move move anything by itself? Easy
- Signature of a move constructor Easy
- What must a move constructor do to its source? Medium
- Which functions make up the rule of five? Easy
- What does the rule of zero recommend? Medium
- How does declaring a destructor affect moves? Hard
- Declaring a move constructor and copying Medium
- Why does vector care about noexcept moves? Medium
- What can you do with a moved-from object? Medium
- Guaranteed copy elision in C++17 Medium
- Why avoid return std::move(local)? Medium
- std::move on a const object Medium
- Spotting a forwarding reference in a declaration Hard
- Reference collapsing rules Hard
- What does std::forward do? Medium
- Pass by value for sink parameters Medium
- Which standard types are move-only? Easy
- Does moving a struct of ints save time? Medium
- How does emplace_back construct elements? Medium
- Error: copying a class that is move-only Easy
- Error: binding an rvalue reference to an lvalue Easy
- Error: a move constructor deletes the copy constructor Medium
- Error: a move assignment that cannot assign a const member Medium
- Error: calling an &&-qualified member on an lvalue Hard
- Error: std::forward without its template argument Medium
- Error: copying a std::thread Medium
- Error: returning a local of a type with deleted moves Hard
- Error: chaining a move assignment that returns void Medium
- Error: copying a lambda that holds a moved-in unique_ptr Hard
Modern C++ Features (C++11 to C++23) 30 questions
- What type does auto deduce here? Easy
- Does auto keep references? Medium
- Why nullptr instead of NULL? Easy
- What does enum class add over enum? Easy
- Which standard added structured bindings? Easy
- What is std::optional for? Easy
- Dereferencing an empty optional Medium
- std::variant compared with a union Medium
- Ownership and lifetime of a string_view Medium
- Scope of a variable declared in an if initialiser Medium
- Rules for designated initialisers Medium
- consteval compared with constexpr Medium
- What does constinit guarantee? Hard
- Which standard introduced std::format? Easy
- std::expected compared with std::optional Medium
- Ignoring the result of a [[nodiscard]] function Easy
- auto with brace initialisation Hard
- The four major features of C++20 Medium
- What does using enum do? Medium
- How std::any checks types Hard
- Error: an auto variable without an initialiser Easy
- Error: using an enum class value as an int Easy
- Error: an enum class enumerator used without its scope Easy
- Error: designated initialisers out of declaration order Medium
- Error: calling a consteval function with a run-time value Medium
- Error: wrong number of names in a structured binding Easy
- Error: passing a string_view where std::string& is needed Medium
- Error: std::get with a type that is not an alternative Medium
- Error: std::optional of a reference type Hard
- Error: one auto declaration deducing two different types Medium
Exception Handling 30 questions
- What problem does exception handling solve? Easy
- Where does execution continue after a catch block? Easy
- Why catch exceptions by const reference? Easy
- Order of catch blocks Medium
- How do you catch every exception? Easy
- throw; versus throw e; Medium
- What is stack unwinding? Easy
- What happens to an exception nobody catches? Easy
- What if a noexcept function throws? Medium
- Should destructors throw? Medium
- Does the destructor run if a constructor throws? Medium
- Raw new in a constructor that later throws Hard
- logic_error versus runtime_error Medium
- What does what() return? Easy
- What is the strong exception guarantee? Medium
- Why not throw pointers to exceptions? Medium
- What is std::exception_ptr used for? Hard
- What is a function-try-block for? Hard
- When to use std::expected instead of exceptions Medium
- The noexcept operator Medium
- Error: a try block with no catch Easy
- Error: catch(...) placed before other handlers Medium
- Error: using finally in C++ Easy
- Error: overriding what() with the wrong signature Hard
- Error: an exception class with a private base Hard
- Error: throwing an abstract class Medium
- Error: using std::runtime_error without its header Easy
- Error: a catch block without a try Easy
- Error: an override that weakens a noexcept guarantee Hard
- Error: throwing a non-copyable, non-movable type Hard
File Handling (fstream) 30 questions
- Which header provides file streams? Easy
- Which class reads from a file? Easy
- What does ofstream do to an existing file? Easy
- How do you add to the end of a file? Easy
- How do you check that a file opened? Easy
- Why is while (!in.eof()) a bad loop? Medium
- getline versus >> on a file Easy
- fail() versus eof() Medium
- Why call clear() on a stream? Medium
- Do you have to call close()? Easy
- What does std::ios::binary change? Medium
- Writing a std::string with write() Hard
- What does seekg do? Easy
- seekg versus seekp Medium
- Updating a file without erasing it Medium
- What is std::stringstream used for? Easy
- getline with a delimiter Easy
- Which standard added std::filesystem? Easy
- How do you get a file's size? Medium
- Making file streams throw exceptions Hard
- Error: writing to an ifstream Easy
- Error: passing a stream by value Medium
- Error: using ofstream without <fstream> Easy
- Error: std::getline into an int Easy
- Error: reading from a const stream Medium
- Error: write() with an int pointer Medium
- Error: passing a string as the open mode Medium
- Error: joining filesystem paths with + Medium
- Error: copy-assigning one stream to another Medium
- Error: in.read() into a std::string object Hard
Multithreading 30 questions
- Which header declares std::thread? Easy
- When does a std::thread start running? Easy
- join() versus detach() Easy
- Destroying a thread that was never joined Medium
- What is a data race? Easy
- Why is ++counter unsafe across threads? Medium
- Why use std::lock_guard instead of lock()/unlock()? Easy
- Which lock does condition_variable::wait need? Medium
- What is a deadlock? Easy
- What does std::scoped_lock offer? Medium
- When is std::atomic enough? Medium
- Why wait on a condition variable with a predicate? Medium
- Are thread arguments passed by reference? Medium
- What does future::get do? Easy
- Exceptions thrown inside std::async tasks Medium
- What does std::jthread add over std::thread? Medium
- What does thread_local mean? Medium
- When is std::shared_mutex useful? Hard
- std::latch versus std::barrier Hard
- How many threads for CPU-bound work? Medium
- Error: thread argument for a reference parameter Medium
- Error: copy-initialising one std::thread from another Easy
- Error: copying a class that contains a mutex Medium
- Error: calling unlock on a lock_guard Medium
- Error: condition_variable::wait with a lock_guard Medium
- Error: copying a std::atomic Medium
- Error: copying a std::future Medium
- Error: std::thread does not provide a stop_token Hard
- Error: locking a mutex member in a const function Medium
- Error: copying a std::promise into a thread Hard
Ready to test your Programming?
Timed online tests with instant scores and explanations.