C++ quiz

Classes, references, the STL, templates and modern memory management.

68 questionsInstant feedbackFree

Set up your quiz

Difficulty
Number of questions

Tip: press 1–4 to answer and Enter for the next question.

All 68 questions with answers
  1. Who created C++?

    • Ken Thompson
    • James Gosling
    • Dennis Ritchie
    • Bjarne Stroustrup (correct)
  2. C++ was originally called what?

    • C Plus
    • C with Classes (correct)
    • New C
    • Object C
  3. Which header is needed for std::cout?

    • <cout>
    • <string>
    • <iostream> (correct)
    • <stdio.h>
  4. Which operator is used with std::cout to print output?

    • ::
    • ->
    • >>
    • << (correct)
  5. Which operator is used with std::cin to read input?

    • <<
    • >> (correct)
    • ->
    • =>
  6. What does std::endl do?

    • Ends the current function
    • Ends the program
    • Inserts a newline and flushes the output (correct)
    • Clears the screen
  7. What is :: called in C++?

    • The scope resolution operator (correct)
    • The pointer operator
    • The ternary operator
    • The member access operator
  8. Which C++ type stores true or false?

    • boolean
    • bool (correct)
    • bit
    • logical
  9. Which keyword creates an object on the heap in C++?

    • new (correct)
    • alloc
    • create
    • malloc
  10. Which keyword releases memory allocated with new?

    • remove
    • release
    • free
    • delete (correct)
  11. Which C++ standard library class represents text?

    • std::string (correct)
    • std::chars
    • char only
    • std::text
  12. Which container is a resizable array in the C++ standard library?

    • std::map
    • std::vector (correct)
    • std::list
    • std::set
  13. What is the default access level of members in a C++ class?

    • internal
    • protected
    • private (correct)
    • public
  14. What is the default access level of members in a C++ struct?

    • protected
    • public (correct)
    • internal
    • private
  15. What is the name of the function every C++ program starts in?

    • WinMain only
    • main (correct)
    • start
    • init
  16. Which symbol ends a statement in C++?

    • .
    • ; (correct)
    • ,
    • :
  17. Which file extension is common for C++ source files?

    • .cp1
    • .cpp (correct)
    • .cs
    • .c++x
  18. What does // start in C++?

    • A preprocessor command
    • A division
    • A single-line comment (correct)
    • A string
  19. What does the & symbol mean in a declaration like int& r = x;?

    • r is the address of x
    • r is a reference to x (correct)
    • r is a copy of x
    • r is a pointer
  20. Which keyword is used for a constant in C++?

    • let
    • static
    • const (correct)
    • final
  21. What is a constructor?

    • A global initialization function
    • A function that deletes an object
    • A function that copies a class
    • A special member function that runs when an object is created (correct)
  22. What is a destructor?

    • A function that frees the stack
    • A member function that runs when an object is destroyed (correct)
    • The opposite of an operator
    • A function that deletes a class definition
  23. How is a destructor named for a class Box?

    • delete Box()
    • ~Box() (correct)
    • Box~()
    • !Box()
  24. What is function overloading?

    • A virtual function
    • Several functions with the same name but different parameters (correct)
    • A function calling itself
    • A function with default arguments only
  25. What does the virtual keyword enable on a member function?

    • Runtime polymorphism through base-class pointers or references (correct)
    • Making the function private
    • Compile-time evaluation
    • Inline expansion
  26. What is a pure virtual function?

    • A static function
    • A function with no parameters
    • A function that cannot be overridden
    • A virtual function declared with = 0 that must be overridden (correct)
  27. What is an abstract class in C++?

    • A class with only private members
    • A class with no constructor
    • A template class
    • A class with at least one pure virtual function (correct)
  28. What is the difference between a pointer and a reference?

    • A reference must be initialized and cannot be reseated; a pointer can be null and reassigned (correct)
    • They are identical
    • A pointer cannot be changed
    • A reference can be null
  29. What does nullptr represent?

    • The integer 0
    • A null pointer constant with its own type (correct)
    • An uninitialized variable
    • An empty string
  30. What does the auto keyword do in modern C++?

    • Marks automatic storage only in C
    • Allocates memory automatically
    • Lets the compiler deduce a variable's type (correct)
    • Makes a variable static
  31. Which container stores unique keys in sorted order?

    • std::deque
    • std::unordered_set
    • std::set (correct)
    • std::vector
  32. Which container stores key-value pairs with keys sorted?

    • std::unordered_map
    • std::map (correct)
    • std::vector
    • std::pair
  33. What is the average lookup time in std::unordered_map?

    • O(1) (correct)
    • O(log n)
    • O(n)
    • O(n log n)
  34. What is the lookup time in std::map?

    • O(n)
    • O(1)
    • O(n^2)
    • O(log n) (correct)
  35. Which function adds an element to the end of a std::vector?

    • insert_end()
    • push_back() (correct)
    • append()
    • add()
  36. What does std::sort require from its iterators?

    • Random-access iterators (correct)
    • No iterators
    • Input iterators
    • Forward iterators only
  37. What does a range-based for loop like for (int x : v) do?

    • Visits only the first element
    • Sorts v
    • Visits each element of v (correct)
    • Iterates indexes of v
  38. What is a template in C++?

    • A design pattern
    • A macro library
    • A precompiled header
    • A way to write code that works with any type (correct)
  39. What is inheritance in C++?

    • Including a header file
    • Overloading an operator
    • Copying an object
    • A class acquiring members of another class (correct)
  40. What does the friend keyword allow?

    • Template specialization
    • A class to inherit privately
    • A function or class to access private members of another class (correct)
    • Two objects to share memory
  41. What is operator overloading?

    • Changing operator precedence
    • Defining custom behaviour of operators for your own types (correct)
    • Creating new operator symbols
    • Using too many operators in one line
  42. What does the this pointer point to inside a member function?

    • The object the function was called on (correct)
    • The calling function
    • The class definition
    • The base class
  43. What is the output of: int a = 5; int& r = a; r = 10; cout << a;?

    • 5
    • 15
    • Compile error
    • 10 (correct)
  44. Which header provides std::sort?

    • <vector>
    • <algorithm> (correct)
    • <sort>
    • <utility>
  45. Which C++ casting operator is checked at runtime for polymorphic types?

    • dynamic_cast (correct)
    • static_cast
    • reinterpret_cast
    • const_cast
  46. What is a namespace used for?

    • Defining classes faster
    • Running code in parallel
    • Allocating memory
    • Grouping names to avoid naming conflicts (correct)
  47. What does using namespace std; do?

    • Brings names from std into the current scope (correct)
    • Imports the whole standard library binary
    • Makes all functions inline
    • Defines a new namespace
  48. What does const after a member function declaration mean?

    • The function is static
    • The function cannot be called
    • The function returns a constant
    • The function does not modify the object (correct)
  49. What is RAII?

    • A runtime type check
    • A compiler optimization
    • Tying resource lifetime to an object's lifetime so cleanup happens in the destructor (correct)
    • Random access iterator interface
  50. Which smart pointer has exclusive ownership of an object?

    • std::shared_ptr
    • std::unique_ptr (correct)
    • std::auto_ptr only
    • std::weak_ptr
  51. Which smart pointer uses reference counting?

    • std::scoped_ptr
    • std::unique_ptr
    • std::shared_ptr (correct)
    • std::weak_ptr
  52. What problem does std::weak_ptr help solve?

    • Reference cycles between shared_ptrs (correct)
    • Dangling raw pointers in arrays
    • Slow allocation
    • Thread safety
  53. What does std::move do?

    • Casts an object to an rvalue so its resources can be moved (correct)
    • Copies an object to a new address
    • Moves a pointer to the heap
    • Moves a file
  54. What is the Rule of Three?

    • If a class needs a custom destructor, copy constructor or copy assignment, it likely needs all three (correct)
    • Every class needs three members
    • Templates need three parameters
    • A class should have three constructors
  55. What is object slicing?

    • Splitting an object into threads
    • Taking a sub-vector
    • Losing derived-class data when copying a derived object into a base object (correct)
    • Deleting part of an array
  56. Why should a base class with virtual functions have a virtual destructor?

    • It is required by the compiler
    • So deleting through a base pointer calls the derived destructor (correct)
    • To make the class abstract
    • To speed up deletion
  57. What is a vtable?

    • A table of variable types
    • A template table
    • A table of virtual function pointers used for dynamic dispatch (correct)
    • A vector of tables
  58. What happens if you delete the same pointer twice?

    • The second delete is ignored safely
    • Undefined behaviour (correct)
    • Nothing
    • A compile error
  59. What is the difference between delete and delete[]?

    • delete[] must be used for arrays allocated with new[] (correct)
    • They are the same
    • delete[] deletes two objects
    • delete is only for arrays
  60. What does constexpr indicate?

    • The variable is constant at runtime only
    • The expression is volatile
    • The value or function can be evaluated at compile time (correct)
    • The function is inline
  61. What is an lvalue?

    • A temporary value
    • A literal
    • Any function call
    • An expression that refers to a persistent object with an address (correct)
  62. What happens to iterators of a std::vector when it reallocates?

    • They become invalid (correct)
    • Nothing
    • They point to the new end
    • They are updated automatically
  63. What does the explicit keyword on a constructor prevent?

    • Copying the object
    • Inheritance
    • Implicit conversions using that constructor (correct)
    • Calling the constructor
  64. What is template specialization?

    • A template with many parameters
    • Instantiating a template
    • A template inside a class
    • Providing a custom implementation of a template for a specific type (correct)
  65. What does std::lock_guard do?

    • Locks a file
    • Guards against exceptions
    • Prevents a thread from starting
    • Locks a mutex on construction and unlocks it on destruction (correct)
  66. Which container gives O(1) push and pop at both ends?

    • std::set
    • std::deque (correct)
    • std::vector
    • std::map
  67. What is the output of: std::vector<int> v{1,2,3}; std::cout << v.size();?

    • 3 (correct)
    • 0
    • 2
    • 4
  68. What is undefined behaviour?

    • A compile error
    • A runtime exception
    • A warning
    • Code whose result the C++ standard does not define, so anything may happen (correct)