C++ Move Semantics & Perfect Forwarding Quiz

C++
0 Passed
0% acceptance

40 in-depth questions covering C++ move semantics, rvalue references, move constructors, std::move, perfect forwarding with std::forward, and performance considerations — with 16 code examples to solidify understanding.

40 Questions
~80 minutes
1

Question 1

What is the purpose of rvalue references in C++?

A
To enable move semantics by binding to temporary objects and allowing resource transfer instead of expensive copying
B
To create permanent references
C
To prevent object modification
D
To declare constant variables
2

Question 2

What is the difference between lvalue and rvalue references?

cpp
int x = 42;
int& lref = x;        // Lvalue reference
int&& rref = 42;      // Rvalue reference to temporary
int&& rref2 = std::move(x); // Rvalue reference to moved lvalue
A
& binds to lvalues (named objects), && binds to rvalues (temporaries) and can extend their lifetime
B
They are identical reference types
C
&& binds to lvalues, & binds to rvalues
D
References don't have lvalue/rvalue distinctions
3

Question 3

What is a move constructor?

cpp
class String {
    char* data;
    size_t size;
public:
    String(String&& other) noexcept 
        : data(other.data), size(other.size) {
        other.data = nullptr; // Steal resources
        other.size = 0;
    }
};
A
A constructor that takes an rvalue reference parameter to efficiently transfer resources from a temporary object instead of copying
B
A constructor that creates new objects
C
A constructor that copies objects
D
A constructor that deletes objects
4

Question 4

What is the purpose of std::move?

cpp
std::vector<int> v1 = {1, 2, 3};
std::vector<int> v2 = std::move(v1); // Move contents
// v1 is now empty
A
To cast an lvalue to an rvalue reference, enabling move operations on named objects that would otherwise copy
B
To create new objects
C
To delete objects
D
To copy objects
5

Question 5

What is perfect forwarding?

cpp
template<typename T>
void wrapper(T&& arg) {
    func(std::forward<T>(arg)); // Preserve value category
}

wrapper(42);      // Forwards as rvalue
int x = 42;
wrapper(x);       // Forwards as lvalue
A
Using std::forward with universal references to preserve the original value category (lvalue/rvalue) when forwarding arguments to other functions
B
Forwarding arguments in reverse order
C
Converting all arguments to rvalues
D
Preventing argument forwarding
6

Question 6

What is the difference between move assignment and copy assignment?

cpp
class String {
    String& operator=(String&& other) noexcept {
        if (this != &other) {
            delete[] data;           // Clean up current
            data = other.data;       // Steal resources
            size = other.size;
            other.data = nullptr;    // Leave other empty
        }
        return *this;
    }
};
A
Move assignment transfers resources from rvalue, copy assignment creates duplicate resources from lvalue
B
They are identical operations
C
Copy assignment is faster
D
Move assignment creates duplicates
7

Question 7

What is a universal reference?

cpp
template<typename T>
void func(T&& arg) { // Universal reference
    // arg can bind to both lvalues and rvalues
}

int x = 42;
func(x);     // T=int&, arg is int& (lvalue)
func(42);    // T=int, arg is int&& (rvalue)
A
A template parameter of type T&& that can bind to both lvalues and rvalues, with reference collapsing determining the actual type
B
A reference that can only bind to rvalues
C
A reference that can only bind to lvalues
D
A reference that cannot bind to anything
8

Question 8

What is reference collapsing?

A
Rules that determine final reference type when references to references occur, enabling universal references to work correctly
B
Creating circular references
C
Preventing reference creation
D
Converting references to pointers
9

Question 9

What is the difference between std::move and std::forward?

cpp
template<typename T>
void func(T&& arg) {
    other_func(std::move(arg));    // Always rvalue
    another_func(std::forward<T>(arg)); // Preserve category
}
A
std::move unconditionally casts to rvalue reference, std::forward conditionally casts to rvalue reference only if original argument was rvalue
B
They are identical functions
C
std::forward is always rvalue
D
std::move preserves value category
10

Question 10

What is move semantics performance benefit?

A
Eliminates expensive deep copying by transferring ownership of resources, providing O(1) operations instead of O(n) copying
B
Makes programs run slower
C
Increases memory usage
D
Prevents compilation
11

Question 11

What is the rule of five in C++11?

A
If a class defines any of destructor, copy constructor, copy assignment, move constructor, or move assignment, it should define all five
B
A class should have exactly five member functions
C
Classes should not have more than five members
D
The rule of three plus two more arbitrary functions
12

Question 12

What is the difference between rvalue and lvalue temporaries?

cpp
std::string create_string() {
    return std::string("hello"); // Rvalue temporary
}

std::string s = create_string(); // Moves from temporary

std::string x = "world";
std::string y = x; // x is lvalue, copies
A
Rvalue temporaries can be moved from for efficiency, lvalue named objects require explicit std::move to enable moving
B
They are identical in behavior
C
Lvalues can always be moved from
D
Rvalues require copying
13

Question 13

What is the purpose of noexcept in move operations?

cpp
class String {
public:
    String(String&& other) noexcept 
        : data_(other.data_), size_(other.size_) {
        other.data_ = nullptr;
        other.size_ = 0;
    }
    // noexcept enables move in containers
};
A
noexcept guarantees allow STL containers to use move operations instead of copy operations for reallocation, providing performance benefits
B
To prevent move operations
C
To make operations slower
D
To require exception handling
14

Question 14

What is the difference between move and forward in template functions?

cpp
template<typename T>
std::unique_ptr<T> make_unique_move(T&& arg) {
    return std::unique_ptr<T>(new T(std::move(arg))); // Always move
}

template<typename T>
void perfect_forward(T&& arg) {
    func(std::forward<T>(arg)); // Preserve original category
}
A
std::move always creates rvalue, std::forward preserves whether original argument was lvalue or rvalue
B
They are identical in templates
C
std::forward always creates rvalue
D
std::move preserves value category
15

Question 15

What is copy elision and how does it relate to move semantics?

A
Copy elision optimizes away unnecessary copies, move semantics optimizes the cases where copying cannot be elided
B
They are identical optimizations
C
Move semantics prevents copy elision
D
Copy elision requires move semantics
16

Question 16

What is the difference between rvalue reference and universal reference syntax?

cpp
void func(int&& arg) { }        // Rvalue reference

template<typename T>
void func(T&& arg) { }         // Universal reference

func(42);    // Both bind to rvalue
int x = 42;
func(x);     // Rvalue reference: error, universal: binds
A
Rvalue reference (Type&&) only binds to rvalues, universal reference (T&&) binds to both lvalues and rvalues through reference collapsing
B
They are identical syntaxes
C
Universal reference only binds to rvalues
D
Rvalue reference binds to lvalues
17

Question 17

What is the difference between move constructor and move assignment operator?

A
Move constructor initializes new object from rvalue, move assignment transfers resources to existing object that already manages resources
B
They are identical operations
C
Move assignment creates new objects
D
Move constructor modifies existing objects
18

Question 18

What is the difference between std::move and static_cast to rvalue reference?

cpp
std::vector<int> v;
std::vector<int> v2 = std::move(v); // Clear intent
std::vector<int> v3 = static_cast<std::vector<int>&&>(v); // Same effect
A
std::move provides clear intent and documentation, static_cast is more verbose but functionally identical
B
They have different effects
C
static_cast is safer
D
std::move requires casting
19

Question 19

What is the difference between perfect forwarding and normal forwarding?

A
Perfect forwarding preserves value categories and cv-qualifiers, normal forwarding may change them through implicit conversions
B
They are identical approaches
C
Normal forwarding is more perfect
D
Perfect forwarding changes value categories
20

Question 20

What is the difference between move semantics and copy semantics?

A
Move semantics transfer ownership (O(1)), copy semantics duplicate resources (O(n) for large objects)
B
They are identical performance-wise
C
Copy semantics are faster
D
Move semantics duplicate resources
21

Question 21

What is the difference between rvalue reference lifetime extension and move semantics?

cpp
const int& ref = 42;     // Lifetime extended
int&& rref = 42;         // Lifetime extended

std::string&& moved = std::move(some_string); // Move, no extension
A
Lifetime extension keeps temporaries alive for reference duration, move semantics transfer resources from objects that may be destroyed
B
They are identical concepts
C
Move semantics extend lifetime
D
Lifetime extension transfers resources
22

Question 22

What is the difference between move-aware containers and regular containers?

A
Move-aware containers use move operations for reallocation and insertion, providing better performance for non-copyable types
B
They are identical in performance
C
Regular containers are move-aware
D
Move-aware containers are slower
23

Question 23

What is the difference between universal reference and rvalue reference overloading?

cpp
template<typename T>
void func(T&& arg);     // Universal

void func(int& arg);     // Lvalue
void func(int&& arg);    // Rvalue

func(42);  // Universal: T=int, perfect forwarding
           // Overloads: calls int&& version
A
Universal reference enables perfect forwarding in templates, overloads provide different behavior for lvalues and rvalues
B
They are identical approaches
C
Overloads enable perfect forwarding
D
Universal reference provides overloads
24

Question 24

What is the difference between move constructor and default constructor?

A
Move constructor initializes from existing object by transferring resources, default constructor creates new object with default values
B
They are identical constructors
C
Default constructor transfers resources
D
Move constructor creates default values
25

Question 25

What is the difference between std::forward and template argument deduction?

A
std::forward preserves deduced template argument properties, template deduction determines T from argument types
B
They are identical processes
C
std::forward performs deduction
D
Template deduction preserves properties
26

Question 26

What is the difference between move semantics and swap operations?

cpp
std::vector<int> v1 = {1,2,3};
std::vector<int> v2 = {4,5,6};
std::swap(v1, v2); // Swap contents

std::vector<int> v3 = std::move(v1); // Move contents
A
Swap exchanges ownership between two objects, move transfers ownership from one object to another leaving source empty
B
They are identical operations
C
Move exchanges ownership
D
Swap leaves objects empty
27

Question 27

What is the difference between rvalue reference and const lvalue reference binding?

cpp
void func(const int& x);  // Binds to everything
void func(int&& x);       // Only rvalues

func(42);    // Both work, const& preferred
int y = 42;
func(y);     // Only const& works
A
const lvalue reference binds to any argument, rvalue reference only binds to rvalues and modifies them
B
They are identical in binding
C
Rvalue reference binds to lvalues
D
const lvalue reference only binds to rvalues
28

Question 28

What is the difference between move assignment and swap assignment?

cpp
String& operator=(String&& other) noexcept {
    if (this != &other) {
        delete[] data;       // Clean current
        data = other.data;   // Take other's data
        other.data = nullptr;
    }
    return *this;
}

String& operator=(String other) { // Copy and swap
    swap(*this, other);   // Swap with copy
    return *this;         // other cleans up
}
A
Move assignment transfers resources directly, copy-and-swap creates temporary copy then swaps for exception safety
B
They are identical implementations
C
Copy-and-swap is faster
D
Move assignment creates temporaries
29

Question 29

What is the difference between perfect forwarding and parameter passing?

A
Perfect forwarding preserves argument value categories in templates, parameter passing may decay types or change categories
B
They are identical concepts
C
Parameter passing preserves categories
D
Perfect forwarding decays types
30

Question 30

What is the difference between move semantics and RVO?

A
RVO eliminates copying by constructing object directly in return location, move semantics transfer resources when RVO cannot be applied
B
They are identical optimizations
C
Move semantics prevent RVO
D
RVO transfers resources
31

Question 31

What is the difference between universal reference and forwarding reference?

A
They are synonymous terms for T&& in template contexts that enable perfect forwarding
B
They have different meanings
C
Universal reference is more general
D
Forwarding reference is more specific
32

Question 32

What is the difference between move constructor and converting constructor?

A
Move constructor transfers resources from same type, converting constructor creates object from different type with conversion
B
They are identical constructors
C
Converting constructor transfers resources
D
Move constructor performs conversion
33

Question 33

What is the difference between std::move and std::move_if_noexcept?

A
std::move_if_noexcept only moves if move constructor is noexcept, otherwise copies for exception safety
B
They are identical functions
C
std::move_if_noexcept always moves
D
std::move checks for noexcept
34

Question 34

What is the difference between rvalue reference and universal reference syntax?

cpp
using RRef = int&&;     // Rvalue reference type

template<typename T>
using URef = T&&;      // Universal reference type

RRef r = 42;           // Only rvalues
URef<int&> ur = x;     // Collapses to int&
A
Rvalue reference type (Type&&) is concrete, universal reference type (T&&) participates in reference collapsing
B
They are identical type constructions
C
Universal reference type is concrete
D
Rvalue reference type collapses
35

Question 35

What is the difference between move semantics and destructive operations?

A
Move semantics leave source object in valid but unspecified state, destructive operations may leave object in invalid state
B
They are identical concepts
C
Destructive operations leave valid state
D
Move semantics leave invalid state
36

Question 36

What is the difference between perfect forwarding and argument decay?

A
Perfect forwarding preserves cv-qualifiers and value categories, argument decay converts arrays to pointers and loses cv-qualifiers
B
They are identical processes
C
Argument decay preserves qualifiers
D
Perfect forwarding causes decay
37

Question 37

What is the difference between move constructor and move assignment performance?

A
Move constructor initializes new object efficiently, move assignment must cleanup existing resources before transferring
B
They have identical performance
C
Move assignment is faster
D
Move constructor requires cleanup
38

Question 38

What is the difference between std::forward and std::move in lambda captures?

cpp
auto lambda = [arg = std::move(obj)]() { };     // Move capture

auto forward_lambda = [](auto&&... args) {
    return func(std::forward<decltype(args)>(args)...);
}; // Forward
A
std::move captures by moving resources, std::forward preserves argument categories in generic lambda forwarding
B
They are identical in lambdas
C
std::forward captures by moving
D
std::move preserves categories
39

Question 39

What is the difference between move semantics and placement new?

A
Move semantics transfer existing object resources, placement new constructs object at specific memory location
B
They are identical operations
C
Placement new transfers resources
D
Move semantics construct at specific location
40

Question 40

What are the fundamental principles for effective move semantics in C++?

A
Implement rule of five, use noexcept for move operations, prefer pass-by-value for copyable types, use std::move for lvalues, std::forward for perfect forwarding, and understand value categories
B
Never use move semantics
C
Use move for all operations
D
Avoid rvalue references

QUIZZES IN C++