Modern C++ MCQ Quiz
Practice 15 basic and 10 tricky MCQs with instant feedback. Continue with the tutorial and interview Q&A links below.
Basic Level (15 Questions)
Which keyword was introduced in C++11 for type inference? C++11
The auto keyword was repurposed in C++11 for type inference, allowing the compiler to automatically deduce the type of a variable from its initializer.
What is the primary purpose of nullptr in C++11? C++11
nullptr was introduced in C++11 to provide a type-safe null pointer constant. Unlike NULL (which is typically defined as 0), nullptr has its own type std::nullptr_t and doesn't implicitly convert to integral types.
Which C++11 feature allows functions to accept any number of arguments? C++11
Variadic templates allow functions and classes to accept any number of template arguments. This is used extensively in modern C++ libraries, such as std::make_shared and std::tuple.
What is the main advantage of using smart pointers over raw pointers? C++11
Smart pointers (std::unique_ptr, std::shared_ptr, std::weak_ptr) automatically manage the lifetime of dynamically allocated objects, preventing memory leaks and simplifying resource management.
Which C++14 feature allows the return type of a function to be deduced? C++14
C++14 introduced return type deduction for functions, allowing the use of auto as a return type. The compiler deduces the return type from the return statements in the function body.
What does the [[nodiscard]] attribute indicate? C++17
The [[nodiscard]] attribute indicates that the return value of a function should not be ignored. The compiler will issue a warning if the return value is discarded.
Which C++17 feature allows handling multiple types in a type-safe way? C++17
std::variant is a type-safe union that can hold one of several specified types. It provides type-safe access to the stored value through std::visit and other mechanisms.
What is the purpose of structured bindings in C++17? C++17
Structured bindings allow you to declare multiple variables initialized from elements of a tuple, pair, array, or struct. For example: auto [x, y] = std::pair(1, 2);
Which C++20 feature introduces a new way to write functions that can be suspended and resumed? C++20
C++20 introduced coroutines, which are functions that can be suspended and resumed. They are useful for asynchronous programming, generators, and other use cases requiring cooperative multitasking.
What is the purpose of the spaceship operator (<=>) in C++20? C++20
The spaceship operator (<=>) performs a three-way comparison, returning a value that indicates whether the first operand is less than, equal to, or greater than the second operand. It can automatically generate all six comparison operators.
What is the difference between std::move and std::forward? C++11
All descriptions are correct: std::move unconditionally converts its argument to an rvalue, while std::forward conditionally converts based on the original value category. std::move is used for moving objects, and std::forward is used for perfect forwarding in templates.
What is a constexpr function in C++? C++11
A constexpr function is a function that can potentially be evaluated at compile time. If its arguments are constant expressions, it will be evaluated at compile time; otherwise, it will be executed at runtime.
What is the purpose of the if constexpr feature in C++17? C++17
if constexpr allows compile-time conditional compilation. The condition must be a constant expression, and only the branch that matches the condition is compiled, which is particularly useful in template code.
What are concepts in C++20? C++20
Concepts are named constraints that specify requirements on template parameters. They make templates more readable, provide better error messages, and allow overloading based on constraints.
What is the purpose of std::jthread in C++20? C++20
std::jthread (joining thread) is an improvement over std::thread that automatically joins on destruction and supports cooperative cancellation through a stop token mechanism.
Tricky Level (10 Questions)
auto deduces type from?
auto uses the type of the initializer.
range-for `for (auto x : v)` needs?
Uses begin() and end() or array decay.
nullptr replaces?
Prefer nullptr over NULL or 0 for pointers.
move semantics avoid?
Move transfers resources instead of deep copying.
constexpr variables can be used in?
constexpr values usable at compile time when evaluated as such.
enum class is?
enum class has scoped enumerators.
std::optional models?
optional may or may not contain a value.
[[nodiscard]] warns if?
Compiler warns when return value is discarded.
Quick review: pick the best C++ statement (1)
Review the tutorial and Q&A for this topic.
Quick review: pick the best C++ statement (2)
Review the tutorial and Q&A for this topic.