
Modern C++ (C++11 and later) changed how good C++ code is written, and most interviews now expect you to know it. This fourth C++ set covers unique_ptr, shared_ptr and weak_ptr, move semantics and rvalue references, lambdas and captures, auto, constexpr, emplace_back and the Rule of Five. Assume a C++17 compiler with the usual headers and using namespace std.
How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 15 questions.
Q1. Which statement about std::unique_ptr is true?
a) It can be freely copied
b) It can be moved but not copied
c) It keeps a reference count
d) It never frees its memory
Q2. What is printed?
auto a = make_shared<int>(5);
auto b = a;
{
auto c = a;
}
cout << a.use_count();
a) 1
b) 2
c) 3
d) 0
Q3. What is the main purpose of std::weak_ptr?
a) To make shared_ptr thread-safe
b) To observe a shared object without owning it, which breaks reference cycles
c) To replace raw pointers in arrays
d) To allocate memory faster
Q4. What does std::move actually do?
a) Moves the object's bytes to a new address
b) Casts its argument to an rvalue reference so that a move constructor or move assignment can be chosen
c) Deletes the original object
d) Copies the object and then clears it
Q5. After string a = "hi"; string b = std::move(a); what is guaranteed about a?
a) a still equals "hi"
b) a is destroyed and cannot be used
c) a is in a valid but unspecified state
d) Accessing a is a compile error
Q6. What type is deduced for x in auto x = 5u; ?
a) int
b) unsigned int
c) long
d) size_t
Q7. What is printed?
int x = 1;
auto f = [x]() { return x; };
x = 10;
cout << f();
a) 1
b) 10
c) 0
d) Compilation error
Q8. What is printed?
int x = 1;
auto f = [&x]() { return x; };
x = 10;
cout << f();
a) 1
b) 10
c) 0
d) Compilation error
Q9. Which declaration declares an rvalue reference to int?
a) int &r
b) int &&r
c) int *&r
d) const int &r
Q10. Why is nullptr preferred over NULL in modern C++?
a) It is faster at run time
b) It has its own type (std::nullptr_t), so it never gets confused with an int in overload resolution
c) NULL was removed from C++
d) nullptr can be dereferenced safely
Q11. What is printed?
vector<int> v = {1, 2, 3};
for (auto &x : v) x *= 2;
int s = 0;
for (int x : v) s += x;
cout << s;
a) 6
b) 12
c) 3
d) 0
Q12. What does constexpr on a function mean?
a) It must be inlined
b) It can be evaluated at compile time when called with constant arguments
c) It can never be called at run time
d) It runs only once
Q13. How does emplace_back differ from push_back?
a) It inserts at the front
b) It constructs the element directly inside the container from the given arguments
c) It does not allocate memory
d) It sorts the vector after inserting
Q14. Which of these is NOT one of the five special member functions in the Rule of Five?
a) Move constructor
b) Copy assignment operator
c) Default constructor
d) Destructor
Q15. Why is it important to mark a move constructor noexcept?
a) It makes the code compile faster
b) std::vector will only move elements during reallocation if the move constructor is noexcept; otherwise it copies them
c) noexcept is required for all constructors
d) It prevents the object from being copied
Recommended MCQs
C++ MCQs - Classes, Objects, Constructors and References (Set 1)
C++ MCQs - Inheritance, Virtual Functions and Templates (Set 2)
C++ STL MCQs - Containers, Iterators and Algorithms (Set 3)
Comments
Post a Comment