Test Your C++ Knowledge with the CPP Questions Quiz!


adaptablebastio

Created 6/20/2024

1

70%

Q & A


Share This Quiz


Dive into the world of C++ with our specially curated quiz based on the most popular questions from the CPP Questions subreddit. Challenge yourself and see how many correct answers you can get out of 10!

Dive into the world of C++ with our specially curated quiz based on the most popular questions from the CPP Questions subreddit. Challenge yourself and see how many correct answers you can get out of 10!

1. Which of the following is a correct way to initialize a std::vector in C++?

std::vector<int> v = {1, 2, 3};
std::vector<int> v = 1, 2, 3;
std::vector<int> v(1, 2, 3);
std::vector<int> v(1, 2, 3, 4);

2. What is the output of the following code? int main() { int a = 10; int b = 20; swap(a, b); cout << a << " " << b; return 0; }

10 20
20 10
Compiler error
Runtime error

3. Which C++ feature allows a function or an operator to be defined for multiple data types?

Polymorphism
Inheritance
Overloading
Encapsulation

4. What is the primary use of the 'std::unique_ptr' class in C++?

To ensure only one reference to a dynamically allocated object
To allow multiple references to a dynamically allocated object
To provide reference counting for dynamic objects
To create arrays of dynamic objects

5. Which keyword is used to prevent a method from being overridden in a derived class?

static
final
const
override

6. In C++, what is the purpose of the 'decltype' specifier?

To define a new type
To automatically deduce the type of an expression
To declare a variable constant
To delete a dynamically allocated object

7. What does the following code print? #include <iostream> void func(const int& x) { std::cout << x << std::endl; } int main() { int y = 10; func(y); return 0; }

10
Compiler error
Garbage value
Runtime error

8. Which of the following is correct about virtual functions in C++?

They must be defined in the derived class.
They cannot have a return type.
They support runtime polymorphism.
They cannot be overridden in the derived class.

9. What is the use of the 'std::move' function in C++?

To copy an object
To move an object, enabling transfer of resources
To delete an object
To create a new object

10. Which of the following statements about 'templates' in C++ is true?

Templates allow functions and classes to operate with generic types.
Templates can only be used with classes, not functions.
Templates need to be defined in a separate file.
Templates are executed at runtime