LearnFrenzy
Sign in Take a free test

Constructors and Destructors · Exercise 2 of 4

Constructors and Destructors Predict the Output

  • 20 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Read each C++ program and choose what it prints. Every program was compiled and run, so the answers are the real output.

Question 1 Easy

What is the output of the following C++ program?

main.cpp
#include <iostream>

struct T {
    T()              { std::cout << "D"; }
    T(int)           { std::cout << "I"; }
    T(int, int)      { std::cout << "II"; }
    T(const T&)      { std::cout << "C"; }
};

int main() {
    T a;
    T b(1);
    T c{1, 2};
    T d = b;
    T e(d);
    T f = 5;
    std::cout << '\n';
    return 0;
}
A DIIICCI
B DIIICCC
C DIIIDCI
D DIIC
Question page & discussion
Question 2 Medium

What is the output of the following C++ program?

main.cpp
#include <iostream>

struct Noisy {
    explicit Noisy(char c) { std::cout << c; }
};

struct Holder {
    Noisy z;
    Noisy y;
    Noisy x;
    Holder() : x('x'), y('y'), z('z') { std::cout << '!'; }
};

int main() {
    Holder h;
    std::cout << '\n';
    return 0;
}
A zyx!
B xyz!
C !zyx
D !xyz
Question page & discussion
Question 3 Medium

What is the output of the following C++ program?

main.cpp
#include <iostream>

class Box {
    int w_, h_;
public:
    Box(int w, int h) : w_(w), h_(h) { std::cout << "A" << w_ * h_ << ' '; }
    Box(int side) : Box(side, side) { std::cout << "B "; }
    Box() : Box(1) { std::cout << "C "; }
};

int main() {
    Box b;
    std::cout << '\n';
    return 0;
}
A A1 B C
B C B A1
C A1 C
D C A1 B
Question page & discussion
Question 4 Medium

What is the output of the following C++ program?

main.cpp
#include <iostream>

struct T {
    T() {}
    T(const T&) { std::cout << "ctor "; }
    T& operator=(const T&) { std::cout << "assign "; return *this; }
};

int main() {
    T a;
    T b = a;
    T c;
    c = a;
    T d(c);
    d = b = a;
    std::cout << '\n';
    return 0;
}
A ctor assign ctor assign assign
B ctor ctor ctor assign assign
C assign assign ctor assign assign
D ctor assign ctor assign
Question page & discussion
Question 5 Medium

What is the output of the following C++ program?

main.cpp
#include <iostream>

class Scores {
    int* data_;
    int n_;
public:
    explicit Scores(int n) : data_(new int[n]{}), n_(n) {}
    Scores(const Scores& o) : data_(new int[o.n_]), n_(o.n_) {
        for (int i = 0; i < n_; ++i) data_[i] = o.data_[i];
    }
    Scores& operator=(const Scores&) = delete;
    ~Scores() { delete[] data_; }
    int& operator[](int i) { return data_[i]; }
};

int main() {
    Scores a(3);
    a[0] = 10;
    Scores b = a;
    b[0] = 99;
    a[1] = 5;
    std::cout << a[0] << ' ' << a[1] << ' ' << b[0] << ' ' << b[1] << '\n';
    return 0;
}
A 10 5 99 0
B 99 5 99 5
C 10 5 99 5
D 99 5 99 0
Question page & discussion
Question 6 Easy

What is the output of the following C++ program?

main.cpp
#include <iostream>
#include <string>
#include <vector>

struct Playlist {
    std::string name;
    std::vector<std::string> songs;
};

int main() {
    Playlist a{"road trip", {"song1", "song2"}};
    Playlist b = a;
    b.songs.push_back("song3");
    b.name += "!";
    std::cout << a.name << ' ' << a.songs.size() << " | " << b.name << ' ' << b.songs.size() << '\n';
    return 0;
}
A road trip 2 | road trip! 3
B road trip! 3 | road trip! 3
C road trip 3 | road trip! 3
D Compilation error
Question page & discussion
Question 7 Medium

What is the output of the following C++ program?

main.cpp
#include <iostream>
#include <utility>

class Handle {
    int id_;
public:
    explicit Handle(int id) : id_(id) {}
    Handle(Handle&& o) noexcept : id_(o.id_) { o.id_ = -1; }
    int id() const { return id_; }
};

int main() {
    Handle a(7);
    Handle b = std::move(a);
    Handle c(std::move(b));
    std::cout << a.id() << ' ' << b.id() << ' ' << c.id() << '\n';
    return 0;
}
A -1 -1 7
B 7 7 7
C -1 7 7
D 7 -1 7
Question page & discussion
Question 8 Medium

What is the output of the following C++ program?

main.cpp
#include <iostream>
#include <string>

struct Label {
    std::string text;
    Label(const char* t) : text(t) { std::cout << "[conv]"; }
};

void print(const Label& l) { std::cout << l.text << ' '; }

int main() {
    print("hello");
    print(Label("world"));
    Label x = "!";
    std::cout << x.text << '\n';
    return 0;
}
A [conv]hello [conv]world [conv]!
B hello world !
C [conv]hello world !
D Compilation error
Question page & discussion
Question 9 Hard

What is the output of the following C++ program?

main.cpp
#include <initializer_list>
#include <iostream>

struct Bag {
    int size;
    Bag(int n, int)                     : size(n) { std::cout << "pair "; }
    Bag(std::initializer_list<int> lst) : size(static_cast<int>(lst.size())) { std::cout << "list "; }
};

int main() {
    Bag a(5, 1);
    Bag b{5, 1};
    Bag c{};
    std::cout << a.size << b.size << '\n';
    return 0;
}
A pair list list 52
B pair pair list 55
C list list list 22
D pair list pair 52
Question page & discussion
Question 10 Medium

What is the output of the following C++ program?

main.cpp
#include <iostream>

struct M {
    char c;
    explicit M(char x) : c(x) {}
    ~M() { std::cout << c; }
};

struct Owner {
    M a{'a'}, b{'b'}, c{'c'};
    ~Owner() { std::cout << "[body]"; }
};

int main() {
    { Owner o; }
    std::cout << '\n';
    return 0;
}
A [body]cba
B abc[body]
C [body]abc
D cba[body]
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links