CPP Syntax Fundamentals & Data Structures 3 — Questions and Answers
Question 1: What does 'std::move' do to an object?
- Physically moves memory to a new location
- Casts the object to an rvalue reference, enabling move semantics (Correct answer)
- Copies the object to a new variable
- Deallocates the object's memory
Correct answer: Casts the object to an rvalue reference, enabling move semantics
std::move is a cast to rvalue reference; it signals that the object's resources can be transferred rather than copied.
Question 2: Which STL data structure provides LIFO (Last-In, First-Out) behavior?
- std::queue
- std::deque
- std::stack (Correct answer)
- std::priority_queue
Correct answer: std::stack
std::stack implements LIFO semantics; elements are pushed and popped from the same end (top).
Question 3: What is the result of this expression in C++? ```cpp std::cout << (true + true + false); ```
- true
- 2 (Correct answer)
- 1
- Compilation error
Correct answer: 2
In arithmetic context, true converts to 1 and false to 0, so 1+1+0 = 2.
Question 4: What is a 'dangling pointer' in C++?
- A pointer that points to nullptr
- A pointer that points to memory that has been freed or gone out of scope (Correct answer)
- A pointer declared but never initialized
- A pointer to a constant value
Correct answer: A pointer that points to memory that has been freed or gone out of scope
A dangling pointer points to memory that is no longer valid, such as after delete or when a local variable's scope ends.
Question 5: Which keyword is used to prevent a derived class from overriding a virtual function in C++11 and later?
- const
- static
- final (Correct answer)
- sealed
Correct answer: final
The 'final' specifier prevents further overriding of a virtual function in derived classes.
Question 6: What is the time complexity of inserting an element at the front of a std::deque?
- O(n)
- O(log n)
- O(1) (Correct answer)
- O(n log n)
Correct answer: O(1)
std::deque supports O(1) amortized insertion at both front and back, unlike std::vector which is O(n) at the front.
Question 7: What does the 'explicit' keyword do when applied to a constructor?
- Makes the constructor inline
- Prevents the constructor from being used for implicit type conversions (Correct answer)
- Forces the constructor to initialize all members
- Makes the constructor virtual
Correct answer: Prevents the constructor from being used for implicit type conversions
The explicit keyword prevents the compiler from using that constructor for implicit single-argument conversions.
What does 'std::move' do to an object?