LearnFrenzy
Sign in Take a free test

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

  1. Which header declares std::cout in C++? Easy
  2. What does using namespace std; do? Easy
  3. What is the :: operator called in C++? Easy
  4. Is return 0; required at the end of main()? Medium
  5. Which size is guaranteed by the C++ standard? Medium
  6. What does int x{7.9}; do? Medium
  7. What type does auto deduce for auto n = 10u;? Easy
  8. const vs constexpr in C++ Medium
  9. std::endl vs '\n' – what is the difference? Easy
  10. Which is a valid C++ identifier? Easy
  11. What is the type of 'A' in C++ (and in C)? Medium
  12. Why use static_cast in C++? Easy
  13. What is std::cerr used for in C++? Easy
  14. Order of the stages that build a C++ program Easy
  15. Which escape sequence prints a tab? Easy
  16. Which of these is not a fundamental C++ type? Easy
  17. What value does int x{}; hold? Easy
  18. What does using Score = int; declare? Easy
  19. What is bool b = 5; in C++? Easy
  20. What happens when a signed int overflows? Medium
  21. Error: narrowing conversion in brace initialization Easy
  22. Error: cout used without std:: Easy
  23. Error: const variable without an initializer Easy
  24. Error: constexpr initialized from a run-time value Medium
  25. Error: auto variable without an initializer Easy
  26. Error: a C++ keyword used as a variable name Easy
  27. Error: modifying a const variable Easy
  28. Error: declaring the same name twice in one scope Easy
  29. Error: missing semicolon after a declaration Easy
  30. Error: using a variable outside its block Easy

Input/Output with cin and cout 30 questions

  1. What are cin and cout in C++? Easy
  2. What is << called when used with cout? Easy
  3. Why can cout << a << b be chained? Medium
  4. What does cin >> do with leading spaces and newlines? Easy
  5. How to read a full name with spaces in C++ Easy
  6. Why does getline after cin >> read an empty line? Medium
  7. What does std::ws do? Medium
  8. Which header provides setw and setprecision? Easy
  9. Which manipulator applies only to the next value? Medium
  10. What does setprecision mean with std::fixed? Medium
  11. What happens when cin reads "abc" into an int? Medium
  12. Why call cin.clear() and cin.ignore() after bad input? Medium
  13. When does while (std::cin >> x) stop? Easy
  14. cin.get(c) vs cin >> c for a char Medium
  15. cerr vs clog in C++ Easy
  16. What is std::istringstream used for? Easy
  17. What does sync_with_stdio(false) do? Medium
  18. Which header provides std::println in C++23? Easy
  19. How to print a literal { with std::format Medium
  20. What does cin.peek() do? Medium
  21. Error: cin used with << instead of >> Easy
  22. Error: cout used with >> Easy
  23. Error: getline into an int variable Medium
  24. Error: reading into a string literal Easy
  25. Error: endl used without std:: after using std::cout Medium
  26. Error: reading input into a const variable Easy
  27. Error: printing a struct with cout Medium
  28. Error: std::format with {:d} for a string Hard
  29. Error: getline with a string as delimiter Medium
  30. Error: copying std::cout into a variable Hard

Functions and Default Arguments 30 questions

  1. Function declaration vs definition in C++ Easy
  2. Does changing a by-value parameter affect the caller? Easy
  3. Why pass a std::string by const reference? Easy
  4. Can a literal be passed to an int& parameter? Medium
  5. Which default-argument declaration is valid? Easy
  6. Can C++ functions be overloaded by return type only? Easy
  7. Function overloading is which kind of polymorphism? Easy
  8. Which overload is called for f('A')? Medium
  9. Why is f(2.5) ambiguous with f(int) and f(long)? Medium
  10. What happens if a recursive function has no base case? Easy
  11. Lifetime of a static local variable Easy
  12. What does inline mean in modern C++? Medium
  13. When is a constexpr function evaluated? Medium
  14. What happens if a noexcept function throws? Hard
  15. What does [[nodiscard]] do? Easy
  16. Returning a reference to a local variable Medium
  17. How to unpack a returned pair in C++17 Easy
  18. Why does C++ use name mangling? Hard
  19. What can std::function<int(int)> hold? Medium
  20. What is auto f(int x) -> double? Medium
  21. Error: default argument before a parameter without one Easy
  22. Error: default argument repeated in the definition Medium
  23. Error: ambiguous call between int and long overloads Medium
  24. Error: overloads that differ only by return type Easy
  25. Error: passing a literal to an int& parameter Easy
  26. Error: calling a function before it is declared Easy
  27. Error: returning a value from a void function Easy
  28. Error: consteval function called with a run-time value Hard
  29. Error: modifying a const reference parameter Easy
  30. Error: defining a function inside another function Medium

Arrays and Vectors 30 questions

  1. What is the index of the first array element? Easy
  2. int a[5] = {1, 2}; – what are the other elements? Easy
  3. What is array-to-pointer decay? Medium
  4. How to get the number of elements of a C array in C++17 Easy
  5. How is a 2D array stored in memory? Medium
  6. What can std::array do that a C array cannot? Easy
  7. Main advantage of std::vector over arrays Easy
  8. size() vs capacity() of a vector Easy
  9. reserve(100) vs resize(100) on an empty vector Medium
  10. Time complexity of vector::push_back Medium
  11. emplace_back vs push_back Medium
  12. v.at(i) vs v[i] for an invalid index Easy
  13. What can happen to iterators after push_back? Hard
  14. What does the erase-remove idiom do? Medium
  15. Does vector::clear() free memory? Medium
  16. Are std::vector elements contiguous? Easy
  17. What is special about std::vector<bool>? Hard
  18. std::vector<int> v(3, 5) vs v{3, 5} Medium
  19. What is std::span used for? Medium
  20. Value of an uninitialized local array Easy
  21. Error: array size from a variable (VLA) Medium
  22. Error: assigning one C array to another Easy
  23. Error: more initializers than array elements Easy
  24. Error: a function returning a C array Medium
  25. Error: std::array<int> without a size Easy
  26. Error: std::vector of references Hard
  27. Error: push_back on a const vector Easy
  28. Error: modifying elements through const auto& Easy
  29. Error: C array with too many initializers Easy
  30. Error: passing std::array of the wrong size Medium

std::string 30 questions

  1. Which header declares std::string? Easy
  2. Advantage of std::string over char arrays Easy
  3. Why does "abc" + "def" not compile? Medium
  4. size() vs length() of a std::string Easy
  5. What does find() return when nothing is found? Easy
  6. What do the arguments of substr mean? Easy
  7. What does s.erase(5) do? Medium
  8. s.at(i) vs s[i] for strings Easy
  9. Why is "Zebra" < "apple" true? Medium
  10. What does std::stoi("hello") do? Medium
  11. What does std::to_string(2.5) return? Medium
  12. Does std::string_view own its characters? Medium
  13. What is c_str() used for? Easy
  14. How to test a prefix in C++20 Easy
  15. What is the type of "hi"s? Medium
  16. Can a std::string be modified in place? Easy
  17. Reading a full line into a std::string Easy
  18. What does find_first_of do? Medium
  19. What does s += 65 append? Hard
  20. How to reverse a std::string Easy
  21. Error: adding two string literals Easy
  22. Error: initialising a std::string from a char with = Medium
  23. Error: switch on a std::string Medium
  24. Error: writing through a std::string_view Medium
  25. Error: comparing a std::string with a char Easy
  26. Error: s.length used without () Easy
  27. Error: passing std::string to strlen Easy
  28. Error: std::to_string called with a string Medium
  29. Error: push_back on a const std::string Easy
  30. Error: assigning substr() to a char* Medium

Pointers and Dynamic Memory 30 questions

  1. What does a pointer variable store? Easy
  2. What does *p mean when p is a pointer? Easy
  3. Why prefer nullptr to NULL? Medium
  4. int* a, b; – what is b? Medium
  5. How far does p + 1 move for an int pointer? Medium
  6. a[i] is equivalent to which expression? Easy
  7. What does const int* p allow? Medium
  8. What does int* const p allow? Medium
  9. What is p->name equivalent to? Easy
  10. new vs malloc in C++ Medium
  11. How to free memory from new int[10] Easy
  12. Is deleting a null pointer safe? Easy
  13. What is a memory leak? Easy
  14. What is a dangling pointer? Easy
  15. Can a void* be dereferenced? Medium
  16. Size of pointers on a 64-bit system Easy
  17. Stack vs heap memory in C++ Medium
  18. What does RAII mean? Medium
  19. Why pass int** to a function? Medium
  20. What happens when new cannot allocate memory? Medium
  21. Error: assigning an integer to a pointer Easy
  22. Error: int* pointing to a const int Medium
  23. Error: writing through a const int* parameter Easy
  24. Error: moving an int* const Easy
  25. Error: int* pointing to a double Easy
  26. Error: dereferencing a void* Medium
  27. Error: delete applied to a reference Medium
  28. Error: arithmetic on a void* Hard
  29. Error: using memory after delete Medium
  30. Error: dereferencing a null pointer Easy

References: lvalue and rvalue 30 questions

  1. Must a reference be initialized? Easy
  2. What is a C++ reference? Easy
  3. What does r = y do to a reference? Medium
  4. Which can be null: a reference or a pointer? Easy
  5. Which reference can bind to a temporary? Medium
  6. Why take a parameter as const T&? Easy
  7. Why a returned reference to a local dangles Medium
  8. What does sizeof give for a reference? Medium
  9. Can you create an array of references? Medium
  10. Which expression is an lvalue? Medium
  11. What can int&& bind to? Medium
  12. What does std::move actually do? Medium
  13. State of an object after std::move Medium
  14. Is a named T&& parameter an lvalue or rvalue? Hard
  15. When is T&& a forwarding reference? Hard
  16. Why use std::forward? Hard
  17. Reference collapsing: T& && becomes? Hard
  18. Why pass std::ref(x) to std::thread? Medium
  19. for (auto x : v) vs for (auto& x : v) Easy
  20. What happens when you std::move a const object? Hard
  21. Error: a reference declared without an initializer Easy
  22. Error: int& bound to a literal Easy
  23. Error: int&& bound to a named variable Medium
  24. Error: changing a value through a const reference Easy
  25. Error: std::vector<std::string&> Medium
  26. Error: int& bound to a const int Easy
  27. Error: declaring a pointer to a reference Hard
  28. Error: binding a reference of the wrong type Medium
  29. Error: std::thread with an int& parameter Hard
  30. Error: int& bound to a function returning by value Medium

OOPS Concepts 30 questions

  1. What are the four pillars of OOP? Easy
  2. Class vs object in C++ Easy
  3. What does encapsulation mean? Easy
  4. Abstraction vs encapsulation Medium
  5. Which relationship does inheritance model? Easy
  6. Types of polymorphism in C++ Easy
  7. Default member access of class and struct Easy
  8. Who can access protected members? Medium
  9. What is the purpose of a constructor? Easy
  10. In what order are local objects destroyed? Medium
  11. What is the this pointer? Easy
  12. How many copies of a static data member exist? Medium
  13. How is a virtual function call resolved? Medium
  14. Can an abstract class be instantiated? Easy
  15. Function overloading vs overriding Medium
  16. Which design uses composition? Easy
  17. What is object slicing? Medium
  18. Why give a base class a virtual destructor? Medium
  19. What can a friend function do? Medium
  20. What does the Liskov substitution principle state? Hard
  21. Error: accessing a private member from main Easy
  22. Error: creating an object of an abstract class Easy
  23. Error: calling a non-const member on a const object Medium
  24. Error: missing semicolon after a class definition Easy
  25. Error: a derived class using a private base member Medium
  26. Error: override on a function that overrides nothing Medium
  27. Error: using a non-static member in a static function Medium
  28. Error: creating an object with a private constructor Medium
  29. Error: deriving from a final class Medium
  30. Error: initializing a derived object from a base object Medium

Classes and Objects 30 questions

  1. How must a class definition end? Easy
  2. Defining a member function outside the class Easy
  3. Are member functions defined in the class inline? Medium
  4. What is a default member initializer? Easy
  5. What does const after a member function mean? Easy
  6. What does mutable allow? Medium
  7. Type of this inside a const member function Hard
  8. Why write this->name = name? Easy
  9. How to define a static data member in the class (C++17) Medium
  10. What can a static member function not do? Medium
  11. What is sizeof an empty class? Medium
  12. Do member functions add to an object's size? Medium
  13. When is a static local object constructed? Medium
  14. What can you do with a forward-declared class? Hard
  15. Which class is an aggregate? Medium
  16. Rules for designated initializers in C++20 Medium
  17. What does a defaulted operator<=> give you? Medium
  18. How do you refer to a nested class? Easy
  19. Is friendship inherited or mutual? Medium
  20. What does copying an object do by default? Easy
  21. Error: member function defined without ClassName:: Easy
  22. Error: a const member function changing a member Easy
  23. Error: reaching a private member through a pointer Easy
  24. Error: calling a non-static member through the class name Medium
  25. Error: initializing a non-const static member in the class Medium
  26. Error: creating an object of an incomplete class Medium
  27. Error: definition differs from declaration in const Medium
  28. Error: designated initializers out of order Medium
  29. Error: comparing objects without an operator Easy
  30. Error: using a private nested class outside Medium

Constructors and Destructors 30 questions

  1. What is the return type of a constructor? Easy
  2. When is the implicit default constructor not generated? Medium
  3. Which members must be in the initializer list? Medium
  4. In which order are members initialized? Medium
  5. What is a delegating constructor? Medium
  6. What does explicit prevent? Easy
  7. Why does a copy constructor take const T&? Medium
  8. Copy constructor or copy assignment? Easy
  9. What goes wrong with a shallow copy of an owning pointer? Medium
  10. What does the rule of three say? Medium
  11. What is the rule of zero? Medium
  12. What does a move constructor do? Easy
  13. Why mark move constructors noexcept? Hard
  14. What do = default and = delete do? Easy
  15. Which is true about destructors? Easy
  16. Order of destruction inside an object Medium
  17. Why make a destructor virtual? Medium
  18. Can a constructor be virtual? Medium
  19. What is guaranteed copy elision? Hard
  20. Which constructor do braces prefer? Hard
  21. Error: copy-initialization with an explicit constructor Easy
  22. Error: a const member assigned in the constructor body Medium
  23. Error: a reference member never bound Medium
  24. Error: copying a class with a deleted copy constructor Easy
  25. Error: no default constructor available Easy
  26. Error: a constructor declared with a return type Easy
  27. Error: delegating constructor that also initializes a member Hard
  28. Error: copying a class that holds a unique_ptr Medium
  29. Error: a destructor with a parameter Easy
  30. Error: declaring a virtual constructor Medium

Static and Friend Members 30 questions

  1. How many copies of a static member exist? Easy
  2. Declaring vs defining a static data member Medium
  3. What does static inline do for a data member? Medium
  4. Why has a static member function no this? Easy
  5. Can a static member function be const or virtual? Medium
  6. Can a static member function access private members? Hard
  7. Do static members change sizeof an object? Easy
  8. Is a base class static member shared with derived classes? Medium
  9. Static members of class template instantiations Medium
  10. Is static local initialization thread-safe? Medium
  11. What makes a Meyers singleton? Medium
  12. What is the static initialization order fiasco? Hard
  13. Is a friend function a member of the class? Easy
  14. Does it matter where a friend is declared? Medium
  15. Is friendship mutual? Easy
  16. Is friendship inherited? Medium
  17. Why write operator+ as a non-member friend? Medium
  18. Why can't operator<< be a member of your class? Medium
  19. Befriending a member function of another class Hard
  20. How is a hidden friend found? Hard
  21. Error: a static function reading a non-static member Easy
  22. Error: a static member function marked const Medium
  23. Error: using this in a static member function Easy
  24. Error: a virtual static member function Medium
  25. Error: calling a friend function like a member Easy
  26. Error: assuming a derived class inherits friendship Medium
  27. Error: a friend of a friend accessing private data Medium
  28. Error: befriending a member of a class not yet defined Hard
  29. Error: using a static member without its class name Easy
  30. Error: calling a hidden friend without a class argument Hard

Encapsulation 30 questions

  1. Which statement best defines encapsulation? Easy
  2. How is data hiding achieved in C++? Easy
  3. What is a class invariant? Medium
  4. How should a constructor handle invalid arguments? Medium
  5. Accessing private members of another object Medium
  6. The protected access rule for derived classes Hard
  7. Why getters and setters everywhere are weak Medium
  8. What does "tell, don't ask" mean? Medium
  9. Which getter breaks encapsulation? Medium
  10. How do const member functions help encapsulation? Easy
  11. When is mutable appropriate? Medium
  12. What is a benefit of immutable objects? Medium
  13. What does a const data member do to assignment? Hard
  14. Why are protected data members discouraged? Medium
  15. What does private inheritance express? Hard
  16. What problem does the pimpl idiom solve? Hard
  17. Why must a pimpl class define its destructor in the .cpp? Hard
  18. Encapsulation vs abstraction Medium
  19. When is a struct with public data appropriate? Easy
  20. How to expose a private vector read-only without copying Medium
  21. Error: using a protected member from main Easy
  22. Error: modifying data through a const reference getter Easy
  23. Error: calling a private helper function from outside Easy
  24. Error: calling a setter on a const object Easy
  25. Error: writing through a pointer-to-const getter Medium
  26. Error: assigning to an object with const members Medium
  27. Error: protected member via a base-class object Hard
  28. Error: calling a base member hidden by private inheritance Medium
  29. Error: using a private nested enum outside the class Medium
  30. Error: using pimpl members where Impl is incomplete Hard

Inheritance 30 questions

  1. Which relationship does public inheritance express? Easy
  2. Default inheritance mode for class and struct Medium
  3. A public base member under protected inheritance Medium
  4. Are private base members inherited? Easy
  5. Which is multilevel inheritance? Easy
  6. Which constructor runs first? Easy
  7. How does a derived class call a base constructor? Easy
  8. What if the base has no default constructor? Medium
  9. What does using Base::Base; do? Medium
  10. What is name hiding in inheritance? Medium
  11. How to call the base version of a member function Easy
  12. Is upcasting safe? Easy
  13. What does a failed dynamic_cast return? Medium
  14. When does object slicing happen? Medium
  15. Order of construction with multiple bases Medium
  16. Resolving an ambiguous member from two bases Medium
  17. What is the diamond problem? Medium
  18. Who constructs a virtual base class? Hard
  19. What does final on a class mean? Easy
  20. What is not inherited by a derived class? Medium
  21. Error: an ambiguous member from two base classes Easy
  22. Error: ambiguous base in a non-virtual diamond Medium
  23. Error: base class without a default constructor Easy
  24. Error: converting to a privately inherited base Medium
  25. Error: calling a base overload hidden by the derived class Medium
  26. Error: overriding a function marked final Medium
  27. Error: static_cast downcast from a virtual base Hard
  28. Error: base member in the derived initializer list Medium
  29. Error: public base member hidden by protected inheritance Medium
  30. Error: inheriting from a class that is only declared Easy

Polymorphism and Virtual Functions 30 questions

  1. How is run-time polymorphism achieved in C++? Easy
  2. Which is compile-time polymorphism? Easy
  3. What does virtual change about a function call? Easy
  4. Is a call on an object by name dynamically dispatched? Medium
  5. What does the override keyword do? Easy
  6. What does final do on a virtual function? Easy
  7. vtable and vptr: per class or per object? Medium
  8. How do virtual functions affect object size? Medium
  9. When is a virtual destructor required? Easy
  10. Calling a virtual function from a constructor Medium
  11. Default arguments of virtual functions Hard
  12. What is a covariant return type? Medium
  13. What does = 0 do to a virtual function? Easy
  14. When does a derived function hide instead of override? Medium
  15. Which functions cannot be virtual? Medium
  16. Why does passing by value break polymorphism? Medium
  17. What does dynamic_cast require? Medium
  18. What is CRTP? Hard
  19. When to use std::variant instead of virtual functions? Hard
  20. What is the non-virtual interface (NVI) idiom? Hard
  21. Error: override with a different parameter type Easy
  22. Error: pure virtual function not overridden Easy
  23. Error: override returning an unrelated pointer type Medium
  24. Error: derived-only function called via a base pointer Easy
  25. Error: = 0 on a non-virtual function Medium
  26. Error: a virtual member function template Hard
  27. Error: dynamic_cast downcast on a non-polymorphic class Medium
  28. Error: std::visit with a visitor missing one alternative Hard
  29. Error: an abstract class as a by-value parameter Medium
  30. Error: override changing the return type Easy

Abstraction 30 questions

  1. What does abstraction mean in C++? Easy
  2. Abstraction versus encapsulation Easy
  3. What is an abstract data type? Easy
  4. How is an interface written in C++? Easy
  5. Which is NOT a way to achieve abstraction? Easy
  6. Why program to an interface? Medium
  7. Why use the pimpl idiom for abstraction? Medium
  8. Where must a pimpl class define its destructor? Hard
  9. What does RAII abstract away? Easy
  10. What is a leaky abstraction? Medium
  11. What do C++20 concepts provide? Medium
  12. What is the zero-overhead principle? Medium
  13. How do header files support abstraction? Easy
  14. Are getters and setters for every field good abstraction? Medium
  15. What is control abstraction? Medium
  16. What can change behind a good abstraction? Easy
  17. Why use strong types like Metres and Seconds? Medium
  18. Which abstraction lets std::find work on any container? Medium
  19. Run-time vs compile-time abstraction Hard
  20. The four pillars of OOP Easy
  21. Error: using a hidden data member Easy
  22. Error: creating an object of an interface Easy
  23. Error: a type that does not satisfy a concept Medium
  24. Error: using an incomplete pimpl type Hard
  25. Error: passing a raw number to a strong type Medium
  26. Error: modifying state in a const query Easy
  27. Error: calling a private helper from outside Easy
  28. Error: returning an interface by value Medium
  29. Error: an implementation that misses one interface function Easy
  30. Error: calling a constrained template with the wrong type Medium

Abstract Classes and Interfaces 30 questions

  1. What makes a C++ class abstract? Easy
  2. Syntax of a pure virtual function Easy
  3. Can you create an object of an abstract class? Easy
  4. Can an abstract class have a constructor? Easy
  5. Can an abstract class have data and normal functions? Easy
  6. When does a derived class stay abstract? Easy
  7. What is an interface in C++? Easy
  8. Must a pure virtual destructor be defined? Medium
  9. Can a pure virtual function have a body? Medium
  10. Implementing several interfaces Easy
  11. Abstract class as a function parameter Medium
  12. Why do interfaces need a virtual destructor? Medium
  13. Calling a pure virtual function from a constructor Hard
  14. Checking abstractness at compile time Medium
  15. Size of an interface object Medium
  16. What is the interface segregation principle? Medium
  17. Abstract class versus interface Medium
  18. C++20 concepts compared with interfaces Hard
  19. Using final on an interface implementation Medium
  20. Why make an interface constructor protected? Hard
  21. Error: new on an abstract class Easy
  22. Error: a vector of an abstract type Medium
  23. Error: a pure virtual destructor without a definition Hard
  24. Error: an abstract class as a data member Medium
  25. Error: override of a function the interface does not declare Easy
  26. Error: missing const leaves the class abstract Medium
  27. Error: a function returning an abstract class by value Medium
  28. Error: an array of interface objects Easy
  29. Error: a type missing a function required by a concept Medium
  30. Error: implementing an interface with private inheritance Hard

Operator Overloading 30 questions

  1. What is operator overloading? Easy
  2. Which operator cannot be overloaded? Easy
  3. Can the scope resolution operator be overloaded? Easy
  4. Which operators must be member functions? Medium
  5. Why is operator<< usually a friend function? Easy
  6. How is postfix ++ declared? Easy
  7. What should prefix ++ return? Medium
  8. What should operator= return? Easy
  9. Does overloading change precedence? Easy
  10. Can you overload operator+ for two ints? Medium
  11. Making 2 * v and v * 2 both work Medium
  12. What does a defaulted <=> give you? Medium
  13. What is a function object (functor)? Easy
  14. Why make operator bool explicit? Medium
  15. Overloading && and || Hard
  16. Why provide two versions of operator[]? Medium
  17. Implementing + using += Medium
  18. Rules for user-defined literal suffixes Medium
  19. What must operator-> return? Hard
  20. Operator overloading and polymorphism Easy
  21. Error: trying to overload the conditional operator Easy
  22. Error: overloading an operator for two ints Easy
  23. Error: a member operator+ with two parameters Medium
  24. Error: operator<< written as a member Medium
  25. Error: operator[] as a non-member function Medium
  26. Error: using an explicit conversion implicitly Medium
  27. Error: inventing a new operator Easy
  28. Error: calling a non-const operator on a const object Medium
  29. Error: postfix ++ with the wrong dummy parameter Hard
  30. Error: user-defined literal with the wrong literal type Hard

Templates 30 questions

  1. What problem do templates solve? Easy
  2. typename versus class in a template header Easy
  3. When is template code generated? Easy
  4. Why does max(3, 2.5) fail with one T? Medium
  5. What is class template argument deduction? Medium
  6. What can a non-type template parameter be? Medium
  7. Syntax of a full template specialisation Medium
  8. Can function templates be partially specialised? Hard
  9. Counting arguments in a parameter pack Medium
  10. What does (xs + ...) do? Medium
  11. if constexpr versus a normal if in templates Medium
  12. What is a C++20 concept? Easy
  13. What does SFINAE stand for? Hard
  14. Why are templates defined in header files? Easy
  15. When is typename required inside a template? Hard
  16. Templates versus virtual functions Medium
  17. What causes template code bloat? Medium
  18. What is a forwarding reference? Hard
  19. Templates compared with macros Easy
  20. Abbreviated function templates in C++20 Medium
  21. Error: conflicting types for one template parameter Easy
  22. Error: a template using an operation the type lacks Medium
  23. Error: a run-time value as a template argument Easy
  24. Error: a dependent type name without typename Hard
  25. Error: calling a constrained template with a wrong type Easy
  26. Error: partial specialisation of a function template Hard
  27. Error: class template arguments that cannot be deduced Medium
  28. Error: a unary fold over an empty pack Hard
  29. Error: a virtual function template in a class template Medium
  30. Error: specialising a template after it was used Hard

STL Containers 30 questions

  1. Which container should be the default choice? Easy
  2. What data structure does std::map use? Easy
  3. Average lookup cost in unordered_map Easy
  4. Does std::set keep duplicates? Easy
  5. What does deque offer over vector? Easy
  6. Does std::list support operator[]? Easy
  7. What does map[key] do for a missing key? Medium
  8. Which element is on top of a priority_queue? Easy
  9. Underlying container of std::stack Medium
  10. When are vector iterators invalidated? Medium
  11. Iterator stability in std::list Medium
  12. Requirements for an unordered_map key type Medium
  13. emplace_back versus push_back Medium
  14. What does vector::reserve do? Easy
  15. set::find versus std::find on a set Medium
  16. Iterating over a std::queue Medium
  17. std::array compared with std::vector Easy
  18. When to use std::multimap Medium
  19. What is std::span? Medium
  20. What does set::lower_bound return? Medium
  21. Error: std::sort on a std::list Medium
  22. Error: operator[] on a const map Medium
  23. Error: a custom key type without a hash Medium
  24. Error: a map key type without operator< Medium
  25. Error: range-for over a std::stack Easy
  26. Error: modifying an element of a std::set Medium
  27. Error: std::array with a run-time size Easy
  28. Error: calling front() on a priority_queue Easy
  29. Error: push_front on a vector Easy
  30. Error: writing through a span of const elements Medium

STL Algorithms and Iterators 30 questions

  1. What does end() point to? Easy
  2. What does std::find return when nothing matches? Easy
  3. Which iterators does std::sort require? Medium
  4. Time complexity of std::sort Easy
  5. What does stable_sort guarantee? Easy
  6. Why does std::remove not shrink a vector? Medium
  7. What does lower_bound return? Medium
  8. binary_search on unsorted data Medium
  9. What is std::back_inserter for? Easy
  10. What does std::unique remove? Medium
  11. The initial value type in accumulate Medium
  12. When is nth_element useful? Hard
  13. Why must comparators not use <=? Hard
  14. Iterator category of std::list Medium
  15. What does std::transform do? Easy
  16. What is a projection in std::ranges algorithms? Medium
  17. When does a range view do its work? Medium
  18. accumulate versus reduce Hard
  19. Generating all permutations Medium
  20. Cost of std::distance on different iterators Hard
  21. Error: sorting a std::set with std::sort Medium
  22. Error: iterators of different types as one range Medium
  23. Error: it + 2 on a list iterator Easy
  24. Error: writing results into a const container Medium
  25. Error: a predicate with the wrong number of parameters Easy
  26. Error: std::max with an int and a double Easy
  27. Error: std::ranges::sort on a list Medium
  28. Error: std::accumulate without an initial value Easy
  29. Error: back_inserter for a std::array Medium
  30. Error: writing through a transform view Hard

Lambda Expressions 30 questions

  1. What is a lambda expression in C++? Easy
  2. Which part of a lambda is the capture list? Easy
  3. When is a by-value capture copied? Easy
  4. What does [&] capture? Easy
  5. Why is mutable needed in a lambda? Medium
  6. Do two identical lambdas have the same type? Medium
  7. Which lambdas convert to function pointers? Medium
  8. What is a generic lambda? Easy
  9. What is an init capture used for? Medium
  10. [this] versus [*this] Hard
  11. Returning a lambda that captures a local by reference Medium
  12. Do lambdas need to capture global variables? Medium
  13. When to use std::function Medium
  14. How is a lambda's return type determined? Easy
  15. What does the compiler turn a lambda into? Medium
  16. Writing a recursive lambda in C++23 Hard
  17. Why lambdas are fast in std::sort Hard
  18. Capturing this through [=] in C++20 Hard
  19. Does mutable state persist between calls? Medium
  20. Can a lambda be used at compile time? Medium
  21. Error: using a local variable that was not captured Easy
  22. Error: modifying a by-value capture without mutable Easy
  23. Error: a capturing lambda as a function pointer Medium
  24. Error: capturing a unique_ptr by copy Medium
  25. Error: return statements with different types Medium
  26. Error: a lambda calling itself by name Medium
  27. Error: assigning lambdas of different types to one variable Medium
  28. Error: storing a lambda with the wrong signature Easy
  29. Error: capturing a data member by name Hard
  30. Error: constrained generic lambda with the wrong type Hard

Smart Pointers 30 questions

  1. What problem do smart pointers solve? Easy
  2. Can a unique_ptr be copied? Easy
  3. State of a unique_ptr after std::move Easy
  4. When does shared_ptr delete its object? Easy
  5. Does weak_ptr change use_count? Easy
  6. What does weak_ptr::lock return? Medium
  7. How do you break a shared_ptr cycle? Medium
  8. Advantage of make_shared Medium
  9. release() versus reset() Medium
  10. Deleting the pointer returned by get() Medium
  11. Two shared_ptrs from the same raw pointer Hard
  12. Why use enable_shared_from_this? Hard
  13. Which smart pointer should be the default? Easy
  14. What happened to std::auto_ptr? Easy
  15. unique_ptr for a dynamic array Medium
  16. How should a function take an object it only reads? Medium
  17. Overhead of unique_ptr Medium
  18. Thread safety of shared_ptr Hard
  19. What is a custom deleter for? Medium
  20. Converting unique_ptr to shared_ptr Medium
  21. Error: copying a unique_ptr Easy
  22. Error: passing a unique_ptr by value without std::move Easy
  23. Error: assigning a raw pointer to a unique_ptr with = Medium
  24. Error: copying a vector of unique_ptr Medium
  25. Error: dereferencing a weak_ptr directly Easy
  26. Error: pointer arithmetic on a unique_ptr Medium
  27. Error: operator[] on a shared_ptr to a single object Medium
  28. Error: a deleter type mismatch between unique_ptrs Hard
  29. Error: make_unique of an abstract class Easy
  30. Error: moving from a const unique_ptr Medium

Move Semantics 30 questions

  1. What is the goal of move semantics? Easy
  2. Does std::move move anything by itself? Easy
  3. Signature of a move constructor Easy
  4. What must a move constructor do to its source? Medium
  5. Which functions make up the rule of five? Easy
  6. What does the rule of zero recommend? Medium
  7. How does declaring a destructor affect moves? Hard
  8. Declaring a move constructor and copying Medium
  9. Why does vector care about noexcept moves? Medium
  10. What can you do with a moved-from object? Medium
  11. Guaranteed copy elision in C++17 Medium
  12. Why avoid return std::move(local)? Medium
  13. std::move on a const object Medium
  14. Spotting a forwarding reference in a declaration Hard
  15. Reference collapsing rules Hard
  16. What does std::forward do? Medium
  17. Pass by value for sink parameters Medium
  18. Which standard types are move-only? Easy
  19. Does moving a struct of ints save time? Medium
  20. How does emplace_back construct elements? Medium
  21. Error: copying a class that is move-only Easy
  22. Error: binding an rvalue reference to an lvalue Easy
  23. Error: a move constructor deletes the copy constructor Medium
  24. Error: a move assignment that cannot assign a const member Medium
  25. Error: calling an &&-qualified member on an lvalue Hard
  26. Error: std::forward without its template argument Medium
  27. Error: copying a std::thread Medium
  28. Error: returning a local of a type with deleted moves Hard
  29. Error: chaining a move assignment that returns void Medium
  30. Error: copying a lambda that holds a moved-in unique_ptr Hard

Modern C++ Features (C++11 to C++23) 30 questions

  1. What type does auto deduce here? Easy
  2. Does auto keep references? Medium
  3. Why nullptr instead of NULL? Easy
  4. What does enum class add over enum? Easy
  5. Which standard added structured bindings? Easy
  6. What is std::optional for? Easy
  7. Dereferencing an empty optional Medium
  8. std::variant compared with a union Medium
  9. Ownership and lifetime of a string_view Medium
  10. Scope of a variable declared in an if initialiser Medium
  11. Rules for designated initialisers Medium
  12. consteval compared with constexpr Medium
  13. What does constinit guarantee? Hard
  14. Which standard introduced std::format? Easy
  15. std::expected compared with std::optional Medium
  16. Ignoring the result of a [[nodiscard]] function Easy
  17. auto with brace initialisation Hard
  18. The four major features of C++20 Medium
  19. What does using enum do? Medium
  20. How std::any checks types Hard
  21. Error: an auto variable without an initialiser Easy
  22. Error: using an enum class value as an int Easy
  23. Error: an enum class enumerator used without its scope Easy
  24. Error: designated initialisers out of declaration order Medium
  25. Error: calling a consteval function with a run-time value Medium
  26. Error: wrong number of names in a structured binding Easy
  27. Error: passing a string_view where std::string& is needed Medium
  28. Error: std::get with a type that is not an alternative Medium
  29. Error: std::optional of a reference type Hard
  30. Error: one auto declaration deducing two different types Medium

Exception Handling 30 questions

  1. What problem does exception handling solve? Easy
  2. Where does execution continue after a catch block? Easy
  3. Why catch exceptions by const reference? Easy
  4. Order of catch blocks Medium
  5. How do you catch every exception? Easy
  6. throw; versus throw e; Medium
  7. What is stack unwinding? Easy
  8. What happens to an exception nobody catches? Easy
  9. What if a noexcept function throws? Medium
  10. Should destructors throw? Medium
  11. Does the destructor run if a constructor throws? Medium
  12. Raw new in a constructor that later throws Hard
  13. logic_error versus runtime_error Medium
  14. What does what() return? Easy
  15. What is the strong exception guarantee? Medium
  16. Why not throw pointers to exceptions? Medium
  17. What is std::exception_ptr used for? Hard
  18. What is a function-try-block for? Hard
  19. When to use std::expected instead of exceptions Medium
  20. The noexcept operator Medium
  21. Error: a try block with no catch Easy
  22. Error: catch(...) placed before other handlers Medium
  23. Error: using finally in C++ Easy
  24. Error: overriding what() with the wrong signature Hard
  25. Error: an exception class with a private base Hard
  26. Error: throwing an abstract class Medium
  27. Error: using std::runtime_error without its header Easy
  28. Error: a catch block without a try Easy
  29. Error: an override that weakens a noexcept guarantee Hard
  30. Error: throwing a non-copyable, non-movable type Hard

File Handling (fstream) 30 questions

  1. Which header provides file streams? Easy
  2. Which class reads from a file? Easy
  3. What does ofstream do to an existing file? Easy
  4. How do you add to the end of a file? Easy
  5. How do you check that a file opened? Easy
  6. Why is while (!in.eof()) a bad loop? Medium
  7. getline versus >> on a file Easy
  8. fail() versus eof() Medium
  9. Why call clear() on a stream? Medium
  10. Do you have to call close()? Easy
  11. What does std::ios::binary change? Medium
  12. Writing a std::string with write() Hard
  13. What does seekg do? Easy
  14. seekg versus seekp Medium
  15. Updating a file without erasing it Medium
  16. What is std::stringstream used for? Easy
  17. getline with a delimiter Easy
  18. Which standard added std::filesystem? Easy
  19. How do you get a file's size? Medium
  20. Making file streams throw exceptions Hard
  21. Error: writing to an ifstream Easy
  22. Error: passing a stream by value Medium
  23. Error: using ofstream without <fstream> Easy
  24. Error: std::getline into an int Easy
  25. Error: reading from a const stream Medium
  26. Error: write() with an int pointer Medium
  27. Error: passing a string as the open mode Medium
  28. Error: joining filesystem paths with + Medium
  29. Error: copy-assigning one stream to another Medium
  30. Error: in.read() into a std::string object Hard

Multithreading 30 questions

  1. Which header declares std::thread? Easy
  2. When does a std::thread start running? Easy
  3. join() versus detach() Easy
  4. Destroying a thread that was never joined Medium
  5. What is a data race? Easy
  6. Why is ++counter unsafe across threads? Medium
  7. Why use std::lock_guard instead of lock()/unlock()? Easy
  8. Which lock does condition_variable::wait need? Medium
  9. What is a deadlock? Easy
  10. What does std::scoped_lock offer? Medium
  11. When is std::atomic enough? Medium
  12. Why wait on a condition variable with a predicate? Medium
  13. Are thread arguments passed by reference? Medium
  14. What does future::get do? Easy
  15. Exceptions thrown inside std::async tasks Medium
  16. What does std::jthread add over std::thread? Medium
  17. What does thread_local mean? Medium
  18. When is std::shared_mutex useful? Hard
  19. std::latch versus std::barrier Hard
  20. How many threads for CPU-bound work? Medium
  21. Error: thread argument for a reference parameter Medium
  22. Error: copy-initialising one std::thread from another Easy
  23. Error: copying a class that contains a mutex Medium
  24. Error: calling unlock on a lock_guard Medium
  25. Error: condition_variable::wait with a lock_guard Medium
  26. Error: copying a std::atomic Medium
  27. Error: copying a std::future Medium
  28. Error: std::thread does not provide a stop_token Hard
  29. Error: locking a mutex member in a const function Medium
  30. Error: copying a std::promise into a thread Hard

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links