LearnFrenzy
Sign in Take a free test

Polymorphism and Virtual Functions · Exercise 2 of 4

Polymorphism and Virtual Functions 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 Hard

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

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

struct Media { virtual ~Media() = default; };
struct Song  : Media { int seconds = 210; };
struct Photo : Media {};

int seconds_of(const Media& m) {
    try {
        return dynamic_cast<const Song&>(m).seconds;
    } catch (const std::bad_cast&) {
        return -1;
    }
}

int main() {
    Song s;
    Photo p;
    std::cout << seconds_of(s) << ' ' << seconds_of(p) << ' ' << (typeid(p) == typeid(Photo)) << '\n';
    return 0;
}
A 210 -1 1
B 210 0 1
C 210 -1 0
D The program crashes
Question page & discussion
Question 12 Hard

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

main.cpp
#include <iostream>

class Payment {
public:
    void pay(int amount) const {
        std::cout << "[check " << amount << "] ";
        do_pay(amount);
        std::cout << "[receipt]\n";
    }
    virtual ~Payment() = default;
private:
    virtual void do_pay(int amount) const = 0;
};

class Upi : public Payment {
    void do_pay(int amount) const override { std::cout << "UPI " << amount << ' '; }
};

int main() {
    Upi u;
    u.pay(250);
    return 0;
}
A [check 250] UPI 250 [receipt]
B UPI 250
C [check 250] [receipt]
D Compilation error
Question page & discussion
Question 13 Hard

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

main.cpp
#include <iostream>

struct Base {
    virtual void f(int)    { std::cout << "B-int "; }
    virtual void f(double) { std::cout << "B-double "; }
    virtual ~Base() = default;
};
struct Derived : Base {
    using Base::f;
    void f(double) override { std::cout << "D-double "; }
};

int main() {
    Derived d;
    Base& b = d;
    b.f(1); b.f(1.0); d.f(1); d.f(1.0);
    std::cout << '\n';
    return 0;
}
A B-int D-double B-int D-double
B D-double D-double D-double D-double
C B-int B-double B-int D-double
D B-int D-double D-double D-double
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 Task {
    virtual void run() const = 0;
    virtual ~Task() = default;
};
void Task::run() const { std::cout << "default setup; "; }   // allowed: pure virtual with a definition

struct Backup : Task {
    void run() const override {
        Task::run();
        std::cout << "copying files\n";
    }
};

int main() {
    Backup b;
    const Task& t = b;
    t.run();
    return 0;
}
A default setup; copying files
B copying files
C default setup;
D Compilation error
Question page & discussion
Question 15 Hard

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

main.cpp
#include <iostream>

template <typename D>
struct Shape {
    double area() const { return static_cast<const D*>(this)->area_impl(); }
    double twice() const { return 2 * area(); }
};
struct Sq : Shape<Sq> { double s; explicit Sq(double x) : s(x) {} double area_impl() const { return s * s; } };
struct Rc : Shape<Rc> { double w, h; Rc(double a, double b) : w(a), h(b) {} double area_impl() const { return w * h; } };

template <typename T>
double total(const Shape<T>& a, const Shape<T>& b) { return a.area() + b.area(); }

int main() {
    Sq a(3), b(4);
    Rc r(2, 5);
    std::cout << total(a, b) << ' ' << r.twice() << ' ' << sizeof(Sq) << '\n';
    return 0;
}
A 25 20 8
B 25 20 16
C 7 20 8
D 25 10 8
Question page & discussion
Question 16 Medium

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

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

struct Card  { double amount; };
struct Cash  { double amount; };
struct Wallet { std::string app; double amount; };

struct Fee {
    double operator()(const Card& c) const { return c.amount * 0.02; }
    double operator()(const Cash&) const { return 0; }
    double operator()(const Wallet& w) const { return w.amount > 1000 ? 5 : 0; }
};

int main() {
    std::vector<std::variant<Card, Cash, Wallet>> payments{Card{500}, Cash{200}, Wallet{"PayNow", 1500}, Card{1000}};
    double fees = 0;
    for (const auto& p : payments) fees += std::visit(Fee{}, p);
    std::cout << fees << ' ' << payments[2].index() << '\n';
    return 0;
}
A 35 2
B 30 2
C 35 3
D 15 2
Question page & discussion
Question 17 Easy

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

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

struct Employee {
    virtual int pay() const = 0;
    virtual ~Employee() = default;
};
struct FullTime : Employee { int pay() const override { return 50000; } };
struct Intern   : Employee { int pay() const override { return 15000; } };
struct Contract : Employee {
    int days;
    explicit Contract(int d) : days(d) {}
    int pay() const override { return days * 2000; }
};

int main() {
    std::vector<std::unique_ptr<Employee>> staff;
    staff.push_back(std::make_unique<FullTime>());
    staff.push_back(std::make_unique<Intern>());
    staff.push_back(std::make_unique<Contract>(12));
    int total = 0;
    for (const auto& e : staff) total += e->pay();
    std::cout << total << '\n';
    return 0;
}
A 89000
B 65000
C 115000
D 74000
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 <string>

struct Greeting {
    std::string greet() const { return hello() + ", " + name() + "!"; }   // non-virtual
    virtual std::string hello() const { return "Hello"; }
    virtual std::string name() const { return "friend"; }
    virtual ~Greeting() = default;
};
struct Hindi : Greeting {
    std::string hello() const override { return "Namaste"; }
};
struct Personal : Hindi {
    std::string name() const override { return "Asha"; }
};

int main() {
    Personal p;
    Hindi h;
    std::cout << p.greet() << ' ' << h.greet() << '\n';
    return 0;
}
A Namaste, Asha! Namaste, friend!
B Hello, friend! Hello, friend!
C Namaste, friend! Namaste, friend!
D Hello, Asha! Namaste, friend!
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 <vector>

struct Vehicle {
    virtual int wheels() const { return 0; }
    virtual ~Vehicle() = default;
};
struct Car  : Vehicle { int wheels() const override { return 4; } };
struct Bike : Vehicle { int wheels() const override { return 2; } };

int main() {
    Car c;
    Bike b;
    std::vector<Vehicle> by_value{c, b};
    std::vector<const Vehicle*> by_ptr{&c, &b};
    int v = 0, p = 0;
    for (const auto& x : by_value) v += x.wheels();
    for (const auto* x : by_ptr) p += x->wheels();
    std::cout << v << ' ' << p << '\n';
    return 0;
}
A 0 6
B 6 6
C 0 0
D 6 0
Question page & discussion
Question 20 Medium

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 Savable   { virtual bool save() const = 0;  virtual ~Savable() = default; };

struct Invoice : Printable, Savable {
    int no = 42;
    void print() const override { std::cout << "Invoice #" << no << ' '; }
    bool save() const override { std::cout << "saved "; return true; }
};

void show(const Printable& p) { p.print(); }
bool store(const Savable& s) { return s.save(); }

int main() {
    Invoice inv;
    show(inv);
    std::cout << store(inv) << '\n';
    return 0;
}
A Invoice #42 saved 1
B Invoice #42 1
C saved Invoice #42 1
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