LearnFrenzy
Sign in Take a free test

Abstract Classes and Interfaces · Exercise 2 of 4

Abstract Classes and Interfaces 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 Medium

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

main.cpp
#include <iostream>

struct Account {
    virtual double rate() const = 0;
    virtual double fee() const = 0;
    virtual ~Account() = default;
};
struct BasicAccount : Account {              // still abstract: implements fee() only
    double fee() const override { return 100; }
};
struct Savings : BasicAccount { double rate() const override { return 0.04; } };
struct Salary  : BasicAccount {
    double rate() const override { return 0.035; }
    double fee() const override { return 0; }
};

int main() {
    Savings s;
    Salary p;
    const Account* a[] = {&s, &p};
    for (auto* x : a) std::cout << 10000 * x->rate() - x->fee() << ' ';
    std::cout << '\n';
    return 0;
}
A 300 350
B 400 350
C 300 250
D 400 250
Question page & discussion
Question 12 Easy

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

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

struct Shape { virtual double area() const = 0; virtual ~Shape() = default; };
struct Rect : Shape { double w, h; Rect(double a, double b) : w(a), h(b) {} double area() const override { return w * h; } };
struct Tri  : Shape { double b, h; Tri(double x, double y) : b(x), h(y) {} double area() const override { return b * h / 2; } };

int main() {
    std::vector<std::unique_ptr<Shape>> v;
    v.push_back(std::make_unique<Rect>(3, 4));
    v.push_back(std::make_unique<Tri>(6, 5));
    v.push_back(std::make_unique<Rect>(1.5, 2));
    double total = 0;
    for (const auto& s : v) total += s->area();
    std::cout << total << ' ' << v.size() << '\n';
    return 0;
}
A 30 3
B 45 3
C 27 3
D 30 2
Question page & discussion
Question 13 Medium

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

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

class Parser {
public:
    std::string parse(const std::string& line) const { return before() + body(line) + after(); }
    virtual ~Parser() = default;
protected:
    virtual std::string body(const std::string& line) const = 0;
    virtual std::string before() const { return "<"; }
    virtual std::string after() const { return ">"; }
};
struct Csv : Parser {
    std::string body(const std::string& l) const override { return std::to_string(l.size()); }
};
struct Json : Parser {
    std::string body(const std::string& l) const override { return "\"" + l + "\""; }
    std::string before() const override { return "{"; }
    std::string after() const override { return "}"; }
};

int main() {
    std::cout << Csv{}.parse("a,b,c") << ' ' << Json{}.parse("ok") << '\n';
    return 0;
}
A <5> {"ok"}
B <5> <"ok">
C 5 "ok"
D <a,b,c> {"ok"}
Question page & discussion
Question 14 Hard

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

main.cpp
#include <iostream>

struct Printable { virtual void print() const = 0; virtual ~Printable() = default; };
struct Exportable { virtual int export_size() const = 0; virtual ~Exportable() = default; };

struct Report : Printable, Exportable {
    void print() const override { std::cout << "report "; }
    int export_size() const override { return 42; }
};
struct Note : Printable {
    void print() const override { std::cout << "note "; }
};

void handle(const Printable& p) {
    p.print();
    if (auto* e = dynamic_cast<const Exportable*>(&p)) std::cout << e->export_size() << ' ';
    else std::cout << "- ";
}

int main() {
    handle(Report{});
    handle(Note{});
    std::cout << '\n';
    return 0;
}
A report 42 note -
B report - note -
C report 42 note 42
D Compilation error
Question page & discussion
Question 15 Medium

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

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

class Widget {
    static inline int alive_ = 0;
public:
    Widget() { ++alive_; }
    virtual ~Widget() { --alive_; }
    virtual const char* kind() const = 0;
    static int alive() { return alive_; }
};
struct Label : Widget { const char* kind() const override { return "label"; } };
struct Input : Widget { const char* kind() const override { return "input"; } };

int main() {
    auto a = std::make_unique<Label>();
    {
        Input i;
        auto b = std::make_unique<Input>();
        std::cout << Widget::alive() << ' ';
    }
    std::cout << Widget::alive() << ' ' << a->kind() << '\n';
    return 0;
}
A 3 1 label
B 3 3 label
C 2 1 label
D 1 1 label
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>
#include <vector>

struct Value {
    virtual std::string str() const = 0;
    virtual ~Value() = default;
};

template <typename T>
struct Boxed : Value {
    T v;
    explicit Boxed(T x) : v(x) {}
    std::string str() const override {
        if constexpr (std::is_same_v<T, bool>) return v ? "true" : "false";
        else return std::to_string(v);
    }
};

int main() {
    std::vector<std::unique_ptr<Value>> row;
    row.push_back(std::make_unique<Boxed<int>>(7));
    row.push_back(std::make_unique<Boxed<bool>>(true));
    row.push_back(std::make_unique<Boxed<long>>(-3));
    for (const auto& v : row) std::cout << v->str() << ';';
    std::cout << '\n';
    return 0;
}
A 7;true;-3;
B 7;1;-3;
C 7;true;3;
D Compilation error
Question page & discussion
Question 17 Hard

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

main.cpp
#include <iostream>

struct Pager {
    virtual int page_size(int requested = 10) const = 0;
    virtual ~Pager() = default;
};
struct WebPager : Pager {
    int page_size(int requested = 25) const override { return requested > 50 ? 50 : requested; }
};

int main() {
    WebPager w;
    const Pager& p = w;
    std::cout << p.page_size() << ' ' << w.page_size() << ' ' << p.page_size(80) << '\n';
    return 0;
}
A 10 25 50
B 25 25 50
C 10 10 80
D 25 10 50
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>

struct Shipping { virtual int cost(int kg) const = 0; virtual ~Shipping() = default; };
struct Standard : Shipping { int cost(int kg) const override { return 40 + 10 * kg; } };
struct Express  : Shipping { int cost(int kg) const override { return 100 + 20 * kg; } };

class Order {
    int kg_;
    std::unique_ptr<Shipping> ship_;
public:
    Order(int kg, std::unique_ptr<Shipping> s) : kg_(kg), ship_(std::move(s)) {}
    void set_shipping(std::unique_ptr<Shipping> s) { ship_ = std::move(s); }
    int total() const { return 500 + ship_->cost(kg_); }
};

int main() {
    Order o(3, std::make_unique<Standard>());
    std::cout << o.total() << ' ';
    o.set_shipping(std::make_unique<Express>());
    std::cout << o.total() << '\n';
    return 0;
}
A 570 660
B 570 570
C 540 660
D 660 570
Question page & discussion
Question 19 Easy

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

main.cpp
#include <iostream>

struct Sensor { virtual double read() const = 0; virtual ~Sensor() = default; };
struct Temp     : Sensor { double read() const override { return 36.5; } };
struct Humidity : Sensor { double read() const override { return 62; } };

double average(const Sensor& a, const Sensor& b) { return (a.read() + b.read()) / 2; }

int main() {
    Temp t;
    Humidity h;
    std::cout << average(t, h) << ' ' << average(t, t) << '\n';
    return 0;
}
A 49.25 36.5
B 49 36
C 98.5 73
D 49.25 49.25
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 <string>

class Course {
protected:
    std::string title_;
    int hours_;
    Course(std::string t, int h) : title_(std::move(t)), hours_(h) {}   // protected: for derived classes only
public:
    virtual int fee() const = 0;
    std::string summary() const { return title_ + "/" + std::to_string(hours_) + "h/" + std::to_string(fee()); }
    virtual ~Course() = default;
};

struct Online : Course {
    Online(std::string t, int h) : Course(std::move(t), h) {}
    int fee() const override { return hours_ * 100; }
};
struct Classroom : Course {
    Classroom(std::string t, int h) : Course(std::move(t), h) {}
    int fee() const override { return hours_ * 250 + 1000; }
};

int main() {
    Online o("C++", 40);
    Classroom c("DSA", 30);
    std::cout << o.summary() << ' ' << c.summary() << '\n';
    return 0;
}
A C++/40h/4000 DSA/30h/8500
B C++/40h/4000 DSA/30h/7500
C C++/40h/0 DSA/30h/0
D Compilation error
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