LearnFrenzy
Sign in Take a free test

Classes and Objects · Exercise 2 of 4

Classes and Objects 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 Counter {
    int n_ = 0;
public:
    void tick();
    int value() const;
};

void Counter::tick() { n_ += 2; }
int Counter::value() const { return n_; }

int main() {
    Counter a, b;
    a.tick(); a.tick(); b.tick();
    std::cout << a.value() << ' ' << b.value() << '\n';
    return 0;
}
A 4 2
B 2 2
C 6 6
D 4 4
Question page & discussion
Question 2 Medium

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

main.cpp
#include <iostream>

class Fan {
    int speed_ = 3;
    bool on_ = false;
public:
    Fan() = default;
    explicit Fan(int s) : speed_(s), on_(true) {}
    void show() const { std::cout << speed_ << on_ << ' '; }
};

int main() {
    Fan a, b(5);
    a.show();
    b.show();
    std::cout << '\n';
    return 0;
}
A 30 51
B 00 51
C 30 30
D 31 51
Question page & discussion
Question 3 Easy

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

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

struct Team {
    std::string name;
    std::vector<std::string> members;
};

int main() {
    Team a{"Alpha", {"Asha", "Ravi"}};
    Team b = a;
    b.name = "Beta";
    b.members.push_back("Meera");
    std::cout << a.name << ' ' << a.members.size() << ' ' << b.name << ' ' << b.members.size() << '\n';
    return 0;
}
A Alpha 2 Beta 3
B Beta 3 Beta 3
C Alpha 3 Beta 3
D Alpha 2 Alpha 3
Question page & discussion
Question 4 Easy

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

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

struct Student {
    std::string name;
    int marks;
    bool passed() const { return marks >= 40; }
};

int main() {
    Student cls[] = {{"Asha", 82}, {"Ravi", 35}, {"Meera", 40}, {"Kabir", 18}};
    int pass = 0;
    std::string top = cls[0].name;
    int best = cls[0].marks;
    for (const auto& s : cls) {
        if (s.passed()) ++pass;
        if (s.marks > best) { best = s.marks; top = s.name; }
    }
    std::cout << pass << ' ' << top << '\n';
    return 0;
}
A 2 Asha
B 1 Asha
C 3 Asha
D 2 Meera
Question page & discussion
Question 5 Easy

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

main.cpp
#include <iostream>

struct Wallet { int money = 100; };

void spend_copy(Wallet w)      { w.money -= 30; }
void spend_real(Wallet& w)     { w.money -= 30; }
Wallet refill(Wallet w)        { w.money += 50; return w; }

int main() {
    Wallet w;
    spend_copy(w);
    spend_real(w);
    Wallet x = refill(w);
    std::cout << w.money << ' ' << x.money << '\n';
    return 0;
}
A 70 120
B 40 90
C 70 70
D 100 150
Question page & discussion
Question 6 Easy

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

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

class Lamp {
    bool on_ = false;
public:
    void toggle() { on_ = !on_; }
    bool on() const { return on_; }
};

int main() {
    Lamp a;
    Lamp* p = &a;
    p->toggle();
    auto q = std::make_unique<Lamp>();
    q->toggle();
    q->toggle();
    (*p).toggle();
    std::cout << a.on() << q->on() << '\n';
    return 0;
}
A 00
B 11
C 10
D 01
Question page & discussion
Question 7 Medium

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

main.cpp
#include <iostream>

class Sensor {
    int value_ = 42;
    mutable int reads_ = 0;
public:
    int read() const { ++reads_; return value_; }
    int reads() const { return reads_; }
};

int main() {
    const Sensor s;
    int sum = s.read() + s.read() + s.read();
    std::cout << sum << ' ' << s.reads() << '\n';
    return 0;
}
A 126 3
B 126 0
C 42 3
D Compilation error
Question page & discussion
Question 8 Medium

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

main.cpp
#include <iostream>

class Order {
    static inline int next_ = 100;
    int id_;
public:
    Order() : id_(next_++) {}
    int id() const { return id_; }
    static int next() { return next_; }
};

int main() {
    Order a, b;
    Order c = a;
    Order d;
    std::cout << a.id() << ' ' << c.id() << ' ' << d.id() << ' ' << Order::next() << '\n';
    return 0;
}
A 100 100 102 103
B 100 102 103 104
C 100 100 103 104
D 100 101 102 103
Question page & discussion
Question 9 Medium

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

main.cpp
#include <iostream>

class Box {
    int width = 1;
public:
    void bad(int width)  { width = width; }
    void good(int width) { this->width = width; }
    int get() const { return width; }
};

int main() {
    Box b;
    b.bad(5);
    std::cout << b.get() << ' ';
    b.good(7);
    std::cout << b.get() << '\n';
    return 0;
}
A 1 7
B 5 7
C 1 1
D 5 5
Question page & discussion
Question 10 Hard

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

main.cpp
#include <iostream>

class Acc {
    int v_ = 0;
public:
    Acc& add_ref(int n) { v_ += n; return *this; }
    Acc  add_val(int n) { v_ += n; return *this; }
    int v() const { return v_; }
};

int main() {
    Acc a, b;
    a.add_ref(1).add_ref(2).add_ref(3);
    b.add_val(1).add_val(2).add_val(3);
    std::cout << a.v() << ' ' << b.v() << '\n';
    return 0;
}
A 6 1
B 6 6
C 1 1
D 6 3
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links