🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Move Semantics and rvalue References: all 10 questions

Quiz: Move Semantics and rvalue References · Question 7 of 10

What does this print? (code)

What does this print?

#include <iostream>
#include <vector>
using namespace std;
struct T {
    T(int) { cout << "B"; }
    T(const T &) { cout << "C"; }
    T(T &&) noexcept { cout << "M"; }
};
int main() {
    vector<T> v;
    v.reserve(3);
    v.push_back(T(1));
    v.emplace_back(2);
    T t(3);
    v.push_back(t);
    return 0;
}
  1. A.BMBMBC
  2. B.BCBBC
  3. C.BMBBC
  4. D.BMBMBM
Answer & explanation

Correct answer: BMBBC

push_back(T(1)) builds then moves (BM), emplace_back builds in place (B), and t is built (B) then copied because it is an lvalue (C).

Can you get the rest right without peeking?

Take the timed Quiz: Move Semantics and rvalue References quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →