LearnFrenzy
Sign in Take a free test

Encapsulation · Exercise 2 of 4

Encapsulation 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, so the answers are the real output.

Question 11 Easy

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

main.cpp
#include <iostream>

class StockCounter {
    unsigned count_ = 0;
public:
    void add(unsigned n) { count_ += n; }
    bool remove(unsigned n) {
        if (n > count_) return false;
        count_ -= n;
        return true;
    }
    unsigned count() const { return count_; }
};

int main() {
    StockCounter s;
    s.add(5);
    bool a = s.remove(3), b = s.remove(4), c = s.remove(2);
    std::cout << a << b << c << ' ' << s.count() << '\n';
    return 0;
}
A 101 0
B 111 0
C 100 2
D 101 4294967295
Question page & discussion
Question 12 Medium

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

main.cpp
#include <iostream>

class Door {
    enum class State { closed, open, locked } s_ = State::closed;
public:
    bool open()   { if (s_ != State::closed) return false; s_ = State::open; return true; }
    bool close()  { if (s_ != State::open) return false; s_ = State::closed; return true; }
    bool lock()   { if (s_ != State::closed) return false; s_ = State::locked; return true; }
    bool unlock() { if (s_ != State::locked) return false; s_ = State::closed; return true; }
};

int main() {
    Door d;
    std::cout << d.lock() << d.open() << d.unlock() << d.open() << d.lock() << d.close() << '\n';
    return 0;
}
A 101101
B 111111
C 110101
D 100110
Question page & discussion
Question 13 Medium

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

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

class Playlist {
    std::vector<int> durations_{210, 185, 240};
public:
    std::span<const int> durations() const { return durations_; }
    void add(int sec) { if (sec > 0) durations_.push_back(sec); }
};

int main() {
    Playlist p;
    p.add(-5);
    p.add(150);
    int total = 0;
    for (int d : p.durations()) total += d;
    std::cout << p.durations().size() << ' ' << total / 60 << ':' << total % 60 << '\n';
    return 0;
}
A 4 13:5
B 5 12:60
C 4 13:05
D 3 10:35
Question page & discussion
Question 14 Easy

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

main.cpp
#include <iostream>

struct Pixel { int r = 0, g = 0, b = 0; };

class Brush {
    int size_ = 3;
public:
    int size() const { return size_; }
    void grow() { if (size_ < 10) ++size_; }
};

int main() {
    Pixel p;
    p.g = 255;
    Brush b;
    for (int i = 0; i < 9; ++i) b.grow();
    std::cout << p.r << ',' << p.g << ' ' << b.size() << '\n';
    return 0;
}
A 0,255 10
B 0,255 12
C 255,255 10
D Compilation error
Question page & discussion
Question 15 Medium

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

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

class Username {
    std::string value_;
    static bool valid(const std::string& s) {
        if (s.size() < 3 || s.size() > 12) return false;
        for (char c : s) if (!(c >= 'a' && c <= 'z') && !(c >= '0' && c <= '9') && c != '_') return false;
        return true;
    }
public:
    bool set(const std::string& s) { if (!valid(s)) return false; value_ = s; return true; }
    const std::string& get() const { return value_; }
};

int main() {
    Username u;
    std::cout << u.set("ab") << u.set("Asha") << u.set("asha_99") << u.set("a b c") << ' ' << u.get() << '\n';
    return 0;
}
A 0010 asha_99
B 0110 Asha
C 0011 a b c
D 1010 asha_99
Question page & discussion
Question 16 Hard

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

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

class Counter {
    struct Impl;
    std::unique_ptr<Impl> p_;
public:
    Counter();
    ~Counter();
    void hit();
    int hits() const;
};

struct Counter::Impl { int hits = 0; int resets = 0; };

Counter::Counter() : p_(std::make_unique<Impl>()) {}
Counter::~Counter() = default;
void Counter::hit() { ++p_->hits; }
int Counter::hits() const { return p_->hits; }

int main() {
    Counter c;
    for (int i = 0; i < 4; ++i) c.hit();
    std::cout << c.hits() << ' ' << sizeof(c) << '\n';
    return 0;
}
A 4 8
B 4 16
C 4 4
D Compilation error
Question page & discussion
Question 17 Medium

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

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

class Age {
    int v_;
public:
    explicit Age(int v) : v_(v) { if (v < 0 || v > 150) throw std::out_of_range("bad age"); }
    int value() const { return v_; }
};

int main() {
    std::vector<Age> ages;
    for (int raw : {25, -3, 40, 200, 0}) {
        try { ages.emplace_back(raw); }
        catch (const std::out_of_range&) { std::cout << 'x'; }
    }
    int sum = 0;
    for (const Age& a : ages) sum += a.value();
    std::cout << ' ' << ages.size() << ' ' << sum << '\n';
    return 0;
}
A xx 3 65
B 5 262
C xx 5 65
D x 4 265
Question page & discussion
Question 18 Easy

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

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

class Course {
    std::string title_;
    std::vector<std::string> students_;
public:
    explicit Course(std::string t) : title_(std::move(t)) {}
    const std::string& title() const { return title_; }
    void enrol(const std::string& s) { if (!s.empty()) students_.push_back(s); }
    std::size_t size() const { return students_.size(); }
};

int main() {
    Course c("C++ Basics");
    c.enrol("Asha");
    c.enrol("");
    c.enrol("Ravi");
    std::string copy = c.title();
    copy += " (copy)";
    std::cout << c.title() << " | " << copy << " | " << c.size() << '\n';
    return 0;
}
A C++ Basics | C++ Basics (copy) | 2
B C++ Basics (copy) | C++ Basics (copy) | 3
C C++ Basics | C++ Basics (copy) | 3
D Compilation error
Question page & discussion
Question 19 Medium

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

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

class History {
    std::vector<int> pages_;
public:
    void visit(int p) { pages_.push_back(p); }
    std::optional<int> back() {
        if (pages_.size() < 2) return std::nullopt;   // nothing to go back to: change nothing
        pages_.pop_back();
        return pages_.back();
    }
    std::size_t depth() const { return pages_.size(); }
};

int main() {
    History h;
    h.visit(1); h.visit(2); h.visit(3);
    auto a = h.back();
    auto b = h.back();
    auto c = h.back();
    std::cout << a.value_or(-1) << b.value_or(-1) << c.value_or(-1) << ' ' << h.depth() << '\n';
    return 0;
}
A 21-1 1
B 210 0
C 321 0
D 21-1 0
Question page & discussion
Question 20 Medium

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

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

class Leaderboard {
    std::vector<int> scores_{40, 90, 65};
public:
    std::vector<int> scores() const { return scores_; }       // returns a copy
    std::vector<int> top(std::size_t n) const {
        auto s = scores_;
        std::sort(s.rbegin(), s.rend());
        s.resize(std::min(n, s.size()));
        return s;
    }
};

int main() {
    Leaderboard lb;
    auto all = lb.scores();
    std::sort(all.begin(), all.end());
    auto best = lb.top(2);
    std::cout << all.front() << ' ' << best[0] << ' ' << best[1] << ' ' << lb.scores()[0] << '\n';
    return 0;
}
A 40 90 65 40
B 40 90 65 90
C 90 40 65 40
D 40 90 40 40
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