LearnFrenzy
Sign in Take a free test

Smart Pointers · Exercise 2 of 4

Smart Pointers Predict the Output

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

Read each C++ program and choose what it prints. Every program was compiled and run on a 64-bit system, so the answers are the real output.

Question 11 Easy

What is the output of the following C++ program on a 64-bit system?

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

int main() {
    auto marks = std::make_unique<int[]>(4);         // value-initialised: all 0
    marks[1] = 80;
    marks[3] = 95;
    int total = 0;
    for (int i = 0; i < 4; ++i) total += marks[i];
    std::cout << marks[0] << ' ' << total << '\n';
    return 0;
}
A 0 175
B garbage 175
C 80 175
D 0 95
Question page & discussion
Question 12 Medium

What is the output of the following C++ program on a 64-bit system?

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

struct Logger {
    std::string name;
    explicit Logger(std::string n) : name(std::move(n)) {}
    ~Logger() { std::cout << "~" << name << ' '; }
};

std::unique_ptr<Logger> make_logger(const std::string& n) { return std::make_unique<Logger>(n); }

int main() {
    {
        auto a = make_logger("solo");
        std::shared_ptr<Logger> b = make_logger("team");
        auto c = b;
        std::cout << b.use_count() << ' ';
    }
    std::cout << '\n';
    return 0;
}
A 2 ~team ~solo
B 1 ~solo ~team
C 2 ~solo ~team
D 2 ~team
Question page & discussion
Question 13 Hard

What is the output of the following C++ program on a 64-bit system?

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

std::map<std::string, std::weak_ptr<std::string>> cache;
int loads = 0;

std::shared_ptr<std::string> get(const std::string& key) {
    if (auto p = cache[key].lock()) return p;
    ++loads;
    auto p = std::make_shared<std::string>("data:" + key);
    cache[key] = p;
    return p;
}

int main() {
    auto a = get("home");
    auto b = get("home");          // still alive: no reload
    a.reset(); b.reset();          // nobody uses it any more
    auto c = get("home");          // expired: reload
    auto d = get("about");
    std::cout << loads << ' ' << *c << ' ' << cache.size() << '\n';
    return 0;
}
A 3 data:home 2
B 2 data:home 2
C 4 data:home 2
D 3 data:home 3
Question page & discussion
Question 14 Hard

What is the output of the following C++ program on a 64-bit system?

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

struct Job : std::enable_shared_from_this<Job> {
    std::shared_ptr<Job> self() { return shared_from_this(); }
};

int main() {
    auto j = std::make_shared<Job>();
    auto a = j->self();
    auto b = a->self();
    std::cout << j.use_count() << ' ' << (a == j) << (b == j) << '\n';
    return 0;
}
A 3 11
B 1 11
C 3 00
D 2 11
Question page & discussion
Question 15 Easy

What is the output of the following C++ program on a 64-bit system?

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

int main() {
    auto a = std::make_unique<std::string>("left");
    auto b = std::make_unique<std::string>("right");
    std::string* before = a.get();
    a.swap(b);
    std::cout << *a << ' ' << *b << ' ' << (b.get() == before) << '\n';
    return 0;
}
A right left 1
B left right 1
C right left 0
D left right 0
Question page & discussion
Question 16 Hard

What is the output of the following C++ program on a 64-bit system?

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

struct Profile {
    std::string name = "Asha";
    int age = 21;
    ~Profile() { std::cout << "~Profile "; }
};

int main() {
    std::shared_ptr<int> age;
    {
        auto p = std::make_shared<Profile>();
        age = std::shared_ptr<int>(p, &p->age);    // points to a member, shares ownership of the whole Profile
        std::cout << p.use_count() << ' ';
    }
    std::cout << *age << ' ' << age.use_count() << ' ';
    age.reset();
    std::cout << '\n';
    return 0;
}
A 2 21 1 ~Profile
B 1 21 1 ~Profile
C 2 ~Profile 21 1
D 2 21 0 ~Profile
Question page & discussion
Question 17 Medium

What is the output of the following C++ program on a 64-bit system?

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

struct Task {
    int id;
    explicit Task(int i) : id(i) {}
    ~Task() { std::cout << "~Task" << id << ' '; }
};

void run(std::unique_ptr<Task> t) { std::cout << "run" << t->id << ' '; }    // t dies here

void peek(const Task& t) { std::cout << "peek" << t.id << ' '; }

int main() {
    auto a = std::make_unique<Task>(1);
    auto b = std::make_unique<Task>(2);
    peek(*a);
    run(std::move(a));
    std::cout << (a ? "a" : "-") << ' ';
    peek(*b);
    std::cout << "end ";
    return 0;
}
A peek1 run1 ~Task1 - peek2 end ~Task2
B peek1 run1 - peek2 end ~Task1 ~Task2
C peek1 run1 ~Task1 a peek2 end ~Task2
D peek1 run1 - peek2 end ~Task2 ~Task1
Question page & discussion
Question 18 Medium

What is the output of the following C++ program on a 64-bit system?

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

int main() {
    auto x = std::make_shared<int>(1);
    auto y = std::make_shared<int>(2);
    auto z = x;
    std::cout << x.use_count() << y.use_count() << ' ';
    z = y;                                 // z leaves x's group and joins y's
    std::cout << x.use_count() << y.use_count() << ' ' << *z << '\n';
    return 0;
}
A 21 12 2
B 21 21 2
C 21 12 1
D 11 12 2
Question page & discussion
Question 19 Medium

What is the output of the following C++ program on a 64-bit system?

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

struct City { std::string name; int pop; };

int main() {
    std::vector<std::unique_ptr<City>> owned;
    owned.push_back(std::make_unique<City>(City{"Pune", 7}));
    owned.push_back(std::make_unique<City>(City{"Delhi", 33}));
    owned.push_back(std::make_unique<City>(City{"Agra", 2}));

    City* biggest = nullptr;                        // observer: never deletes
    for (const auto& c : owned) if (!biggest || c->pop > biggest->pop) biggest = c.get();

    for (int i = 0; i < 100; ++i) owned.push_back(std::make_unique<City>(City{"x", 0}));   // vector reallocates…
    std::cout << biggest->name << ' ' << biggest->pop << ' ' << owned.size() << '\n';      // …but the City objects did not move
    return 0;
}
A Delhi 33 103
B Pune 7 103
C Delhi 33 3
D Undefined behaviour
Question page & discussion
Question 20 Medium

What is the output of the following C++ program on a 64-bit system?

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

int main() {
    int freed = 0;
    auto deleter = [&freed](int* p) { std::cout << "free " << *p << ' '; ++freed; delete p; };
    {
        std::unique_ptr<int, decltype(deleter)> a(new int(1), deleter);
        std::shared_ptr<int> b(new int(2), deleter);
        auto c = b;
        std::cout << "| ";
    }
    std::cout << freed << '\n';
    return 0;
}
A | free 2 free 1 2
B | free 1 free 2 2
C | free 2 free 2 free 1 3
D free 1 free 2 | 2
Question page & discussion
Up next · Exercise 3Find the Error10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links