Who created C++?
- A. Ken Thompson
- B. James Gosling
- C. Dennis Ritchie
- D. Bjarne Stroustrup (correct)
C++ was originally called what?
- A. C Plus
- B. C with Classes (correct)
- C. New C
- D. Object C
Which header is needed for std::cout?
- A. <cout>
- B. <string>
- C. <iostream> (correct)
- D. <stdio.h>
Which operator is used with std::cout to print output?
- A. ::
- B. ->
- C. >>
- D. << (correct)
Which operator is used with std::cin to read input?
- A. <<
- B. >> (correct)
- C. ->
- D. =>
What does std::endl do?
- A. Ends the current function
- B. Ends the program
- C. Inserts a newline and flushes the output (correct)
- D. Clears the screen
What is :: called in C++?
- A. The scope resolution operator (correct)
- B. The pointer operator
- C. The ternary operator
- D. The member access operator
Which C++ type stores true or false?
- A. boolean
- B. bool (correct)
- C. bit
- D. logical
Which keyword creates an object on the heap in C++?
- A. new (correct)
- B. alloc
- C. create
- D. malloc
Which keyword releases memory allocated with new?
- A. remove
- B. release
- C. free
- D. delete (correct)
Which C++ standard library class represents text?
- A. std::string (correct)
- B. std::chars
- C. char only
- D. std::text
Which container is a resizable array in the C++ standard library?
- A. std::map
- B. std::vector (correct)
- C. std::list
- D. std::set
What is the default access level of members in a C++ class?
- A. internal
- B. protected
- C. private (correct)
- D. public
What is the default access level of members in a C++ struct?
- A. protected
- B. public (correct)
- C. internal
- D. private
What is the name of the function every C++ program starts in?
- A. WinMain only
- B. main (correct)
- C. start
- D. init
Which symbol ends a statement in C++?
- A. .
- B. ; (correct)
- C. ,
- D. :
Which file extension is common for C++ source files?
- A. .cp1
- B. .cpp (correct)
- C. .cs
- D. .c++x
What does // start in C++?
- A. A preprocessor command
- B. A division
- C. A single-line comment (correct)
- D. A string
What does the & symbol mean in a declaration like int& r = x;?
- A. r is the address of x
- B. r is a reference to x (correct)
- C. r is a copy of x
- D. r is a pointer
Which keyword is used for a constant in C++?
- A. let
- B. static
- C. const (correct)
- D. final
What is a constructor?
- A. A global initialization function
- B. A function that deletes an object
- C. A function that copies a class
- D. A special member function that runs when an object is created (correct)
What is a destructor?
- A. A function that frees the stack
- B. A member function that runs when an object is destroyed (correct)
- C. The opposite of an operator
- D. A function that deletes a class definition
How is a destructor named for a class Box?
- A. delete Box()
- B. ~Box() (correct)
- C. Box~()
- D. !Box()
What is function overloading?
- A. A virtual function
- B. Several functions with the same name but different parameters (correct)
- C. A function calling itself
- D. A function with default arguments only
What does the virtual keyword enable on a member function?
- A. Runtime polymorphism through base-class pointers or references (correct)
- B. Making the function private
- C. Compile-time evaluation
- D. Inline expansion
What is a pure virtual function?
- A. A static function
- B. A function with no parameters
- C. A function that cannot be overridden
- D. A virtual function declared with = 0 that must be overridden (correct)
What is an abstract class in C++?
- A. A class with only private members
- B. A class with no constructor
- C. A template class
- D. A class with at least one pure virtual function (correct)
What is the difference between a pointer and a reference?
- A. A reference must be initialized and cannot be reseated; a pointer can be null and reassigned (correct)
- B. They are identical
- C. A pointer cannot be changed
- D. A reference can be null
What does nullptr represent?
- A. The integer 0
- B. A null pointer constant with its own type (correct)
- C. An uninitialized variable
- D. An empty string
What does the auto keyword do in modern C++?
- A. Marks automatic storage only in C
- B. Allocates memory automatically
- C. Lets the compiler deduce a variable's type (correct)
- D. Makes a variable static
Which container stores unique keys in sorted order?
- A. std::deque
- B. std::unordered_set
- C. std::set (correct)
- D. std::vector
Which container stores key-value pairs with keys sorted?
- A. std::unordered_map
- B. std::map (correct)
- C. std::vector
- D. std::pair
What is the average lookup time in std::unordered_map?
- A. O(1) (correct)
- B. O(log n)
- C. O(n)
- D. O(n log n)
What is the lookup time in std::map?
- A. O(n)
- B. O(1)
- C. O(n^2)
- D. O(log n) (correct)
Which function adds an element to the end of a std::vector?
- A. insert_end()
- B. push_back() (correct)
- C. append()
- D. add()
What does std::sort require from its iterators?
- A. Random-access iterators (correct)
- B. No iterators
- C. Input iterators
- D. Forward iterators only
What does a range-based for loop like for (int x : v) do?
- A. Visits only the first element
- B. Sorts v
- C. Visits each element of v (correct)
- D. Iterates indexes of v
What is a template in C++?
- A. A design pattern
- B. A macro library
- C. A precompiled header
- D. A way to write code that works with any type (correct)
What is inheritance in C++?
- A. Including a header file
- B. Overloading an operator
- C. Copying an object
- D. A class acquiring members of another class (correct)
What does the friend keyword allow?
- A. Template specialization
- B. A class to inherit privately
- C. A function or class to access private members of another class (correct)
- D. Two objects to share memory
What is operator overloading?
- A. Changing operator precedence
- B. Defining custom behaviour of operators for your own types (correct)
- C. Creating new operator symbols
- D. Using too many operators in one line
What does the this pointer point to inside a member function?
- A. The object the function was called on (correct)
- B. The calling function
- C. The class definition
- D. The base class
What is the output of: int a = 5; int& r = a; r = 10; cout << a;?
- A. 5
- B. 15
- C. Compile error
- D. 10 (correct)
Which header provides std::sort?
- A. <vector>
- B. <algorithm> (correct)
- C. <sort>
- D. <utility>
Which C++ casting operator is checked at runtime for polymorphic types?
- A. dynamic_cast (correct)
- B. static_cast
- C. reinterpret_cast
- D. const_cast
What is a namespace used for?
- A. Defining classes faster
- B. Running code in parallel
- C. Allocating memory
- D. Grouping names to avoid naming conflicts (correct)
What does using namespace std; do?
- A. Brings names from std into the current scope (correct)
- B. Imports the whole standard library binary
- C. Makes all functions inline
- D. Defines a new namespace
What does const after a member function declaration mean?
- A. The function is static
- B. The function cannot be called
- C. The function returns a constant
- D. The function does not modify the object (correct)
What is RAII?
- A. A runtime type check
- B. A compiler optimization
- C. Tying resource lifetime to an object's lifetime so cleanup happens in the destructor (correct)
- D. Random access iterator interface
Which smart pointer has exclusive ownership of an object?
- A. std::shared_ptr
- B. std::unique_ptr (correct)
- C. std::auto_ptr only
- D. std::weak_ptr
Which smart pointer uses reference counting?
- A. std::scoped_ptr
- B. std::unique_ptr
- C. std::shared_ptr (correct)
- D. std::weak_ptr
What problem does std::weak_ptr help solve?
- A. Reference cycles between shared_ptrs (correct)
- B. Dangling raw pointers in arrays
- C. Slow allocation
- D. Thread safety
What does std::move do?
- A. Casts an object to an rvalue so its resources can be moved (correct)
- B. Copies an object to a new address
- C. Moves a pointer to the heap
- D. Moves a file
What is the Rule of Three?
- A. If a class needs a custom destructor, copy constructor or copy assignment, it likely needs all three (correct)
- B. Every class needs three members
- C. Templates need three parameters
- D. A class should have three constructors
What is object slicing?
- A. Splitting an object into threads
- B. Taking a sub-vector
- C. Losing derived-class data when copying a derived object into a base object (correct)
- D. Deleting part of an array
Why should a base class with virtual functions have a virtual destructor?
- A. It is required by the compiler
- B. So deleting through a base pointer calls the derived destructor (correct)
- C. To make the class abstract
- D. To speed up deletion
What is a vtable?
- A. A table of variable types
- B. A template table
- C. A table of virtual function pointers used for dynamic dispatch (correct)
- D. A vector of tables
What happens if you delete the same pointer twice?
- A. The second delete is ignored safely
- B. Undefined behaviour (correct)
- C. Nothing
- D. A compile error
What is the difference between delete and delete[]?
- A. delete[] must be used for arrays allocated with new[] (correct)
- B. They are the same
- C. delete[] deletes two objects
- D. delete is only for arrays
What does constexpr indicate?
- A. The variable is constant at runtime only
- B. The expression is volatile
- C. The value or function can be evaluated at compile time (correct)
- D. The function is inline
What is an lvalue?
- A. A temporary value
- B. A literal
- C. Any function call
- D. An expression that refers to a persistent object with an address (correct)
What happens to iterators of a std::vector when it reallocates?
- A. They become invalid (correct)
- B. Nothing
- C. They point to the new end
- D. They are updated automatically
What does the explicit keyword on a constructor prevent?
- A. Copying the object
- B. Inheritance
- C. Implicit conversions using that constructor (correct)
- D. Calling the constructor
What is template specialization?
- A. A template with many parameters
- B. Instantiating a template
- C. A template inside a class
- D. Providing a custom implementation of a template for a specific type (correct)
What does std::lock_guard do?
- A. Locks a file
- B. Guards against exceptions
- C. Prevents a thread from starting
- D. Locks a mutex on construction and unlocks it on destruction (correct)
Which container gives O(1) push and pop at both ends?
- A. std::set
- B. std::deque (correct)
- C. std::vector
- D. std::map
What is the output of: std::vector<int> v{1,2,3}; std::cout << v.size();?
- A. 3 (correct)
- B. 0
- C. 2
- D. 4
What is undefined behaviour?
- A. A compile error
- B. A runtime exception
- C. A warning
- D. Code whose result the C++ standard does not define, so anything may happen (correct)