LearnFrenzy
Sign in Take a free test

OOPS Concepts · Exercise 2 of 4

OOPS Concepts 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>

class Box {
public:
    int items = 0;
    void add(int n) { items += n; }
};

int main() {
    Box a, b;
    a.add(3);
    b.add(5);
    a.add(b.items);
    Box c = a;
    c.add(1);
    std::cout << a.items << ' ' << b.items << ' ' << c.items << '\n';
    return 0;
}
A 8 5 9
B 9 5 9
C 8 8 9
D 3 5 4
Question page & discussion
Question 2 Medium

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

main.cpp
#include <iostream>

struct T {
    char c;
    T(char x) : c(x) { std::cout << c; }
    ~T() { std::cout << (char)(c - 32); }
};

int main() {
    T a('a');
    {
        T b('b');
        T c('c');
    }
    T d('d');
    std::cout << '|';
    return 0;
}
A abcCBd|DA
B abcBCd|AD
C abcd|DCBA
D abcCBd|AD
Question page & discussion
Question 3 Medium

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

main.cpp
#include <iostream>

class Session {
public:
    static inline int live = 0;
    Session() { ++live; }
    ~Session() { --live; }
};

int main() {
    Session a;
    {
        Session b, c;
        std::cout << Session::live << ' ';
    }
    Session* d = new Session;
    std::cout << Session::live << ' ';
    delete d;
    std::cout << a.live << '\n';
    return 0;
}
A 3 2 1
B 3 4 1
C 3 2 2
D 1 1 1
Question page & discussion
Question 4 Medium

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

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

class Builder {
    std::string s_;
public:
    Builder& add(const std::string& t) { s_ += t; return *this; }
    Builder& repeat(int n) { std::string base = s_; for (int i = 1; i < n; ++i) s_ += base; return *this; }
    const std::string& str() const { return s_; }
};

int main() {
    Builder b;
    std::cout << b.add("ab").add("c").repeat(2).add("!").str() << '\n';
    return 0;
}
A abcabc!
B abc!abc!
C abcc!
D ab!
Question page & discussion
Question 5 Medium

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

main.cpp
#include <iostream>

struct Base {
    void hello() const { std::cout << "Base::hello "; }
    virtual void greet() const { std::cout << "Base::greet "; }
    virtual ~Base() = default;
};

struct Derived : Base {
    void hello() const { std::cout << "Derived::hello "; }
    void greet() const override { std::cout << "Derived::greet "; }
};

int main() {
    Derived d;
    Base* p = &d;
    p->hello();
    p->greet();
    d.hello();
    std::cout << '\n';
    return 0;
}
A Base::hello Derived::greet Derived::hello
B Derived::hello Derived::greet Derived::hello
C Base::hello Base::greet Derived::hello
D Base::hello Derived::greet Base::hello
Question page & discussion
Question 6 Medium

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

main.cpp
#include <iostream>

struct A { A() { std::cout << "A "; } ~A() { std::cout << "~A "; } };
struct B : A { B() { std::cout << "B "; } ~B() { std::cout << "~B "; } };
struct C : B { C() { std::cout << "C "; } ~C() { std::cout << "~C "; } };

int main() {
    C obj;
    std::cout << "| ";
    return 0;
}
A A B C | ~C ~B ~A
B C B A | ~A ~B ~C
C A B C | ~A ~B ~C
D C | ~C
Question page & discussion
Question 7 Medium

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

main.cpp
#include <iostream>

struct Wheel  { Wheel()  { std::cout << "W"; } ~Wheel()  { std::cout << "w"; } };
struct Engine { Engine() { std::cout << "E"; } ~Engine() { std::cout << "e"; } };

struct Bike {
    Engine engine;
    Wheel front, back;
    Bike()  { std::cout << "B"; }
    ~Bike() { std::cout << "b"; }
};

int main() {
    { Bike x; }
    std::cout << '\n';
    return 0;
}
A EWWBbwwe
B BEWWbeww
C EWWBewwb
D WWEBbeww
Question page & discussion
Question 8 Medium

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

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

struct Animal {
    virtual std::string speak() const { return "generic"; }
    virtual ~Animal() = default;
};
struct Cat : Animal {
    std::string speak() const override { return "meow"; }
};

int main() {
    Cat c;
    Animal copy = c;
    Animal& ref = c;
    Animal* ptr = &c;
    std::cout << copy.speak() << ' ' << ref.speak() << ' ' << ptr->speak() << '\n';
    return 0;
}
A generic meow meow
B meow meow meow
C generic generic meow
D generic generic generic
Question page & discussion
Question 9 Easy

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

main.cpp
#include <iostream>

class Thermostat {
    int temp_ = 24;
public:
    bool set(int t) {
        if (t < 16 || t > 30) return false;
        temp_ = t;
        return true;
    }
    int get() const { return temp_; }
};

int main() {
    Thermostat th;
    bool a = th.set(10), b = th.set(22), c = th.set(35);
    std::cout << a << b << c << ' ' << th.get() << '\n';
    return 0;
}
A 010 22
B 111 35
C 010 24
D 000 24
Question page & discussion
Question 10 Medium

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

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

struct Payment {
    virtual double fee(double amount) const = 0;
    virtual ~Payment() = default;
};
struct Upi  : Payment { double fee(double) const override { return 0; } };
struct Card : Payment { double fee(double a) const override { return a * 0.02; } };

int main() {
    std::vector<std::unique_ptr<Payment>> methods;
    methods.push_back(std::make_unique<Upi>());
    methods.push_back(std::make_unique<Card>());
    double total = 0;
    for (const auto& m : methods) total += m->fee(1500);
    std::cout << total << ' ' << methods.size() << '\n';
    return 0;
}
A 30 2
B 0 2
C 1530 2
D 30 1
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links