Modern C++ MCQ Quiz

Test your knowledge of Modern C++ features (C++11, C++14, C++17, C++20) with this interactive quiz. Select the correct answer for each question and see immediate feedback.

Medium Level Questions Medium

1

Which keyword was introduced in C++11 for type inference? C++11

Correct Answer: A) auto

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.

2

What is the primary purpose of nullptr in C++11? C++11

Correct Answer: A) To replace NULL with a type-safe null pointer

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.

3

Which C++11 feature allows functions to accept any number of arguments? C++11

Correct Answer: A) Variadic templates

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.

4

What is the main advantage of using smart pointers over raw pointers? C++11

Correct Answer: A) Automatic memory management

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.

5

Which C++14 feature allows the return type of a function to be deduced? C++14

Correct Answer: A) Return type deduction

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.

6

What does the [[nodiscard]] attribute indicate? C++17

Correct Answer: A) The return value should not be ignored

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.

7

Which C++17 feature allows handling multiple types in a type-safe way? C++17

Correct Answer: A) std::variant

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.

8

What is the purpose of structured bindings in C++17? C++17

Correct Answer: A) To decompose objects into individual variables

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);

9

Which C++20 feature introduces a new way to write functions that can be suspended and resumed? C++20

Correct Answer: A) Coroutines

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.

10

What is the purpose of the spaceship operator (<=>) in C++20? C++20

Correct Answer: A) Three-way comparison

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.

Advanced Level Questions Advanced

11

What is the difference between std::move and std::forward? C++11

Correct Answer: D) All of the above

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.

12

What is a constexpr function in C++? C++11

Correct Answer: A) A function that can be evaluated at compile time

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.

13

What is the purpose of the if constexpr feature in C++17? C++17

Correct Answer: A) Compile-time conditional compilation

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.

14

What are concepts in C++20? C++20

Correct Answer: A) Named constraints on template parameters

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.

15

What is the purpose of std::jthread in C++20? C++20

Correct Answer: A) A joining thread that automatically joins on destruction

std::jthread (joining thread) is an improvement over std::thread that automatically joins on destruction and supports cooperative cancellation through a stop token mechanism.

Your score will appear here