LearnFrenzy
Sign in Take a free test

Classes and Objects · Exercise 3 of 4

Classes and Objects Find the Error

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Each C++ program contains a class-related mistake that the compiler rejects. Choose the option that names the real problem.

Question 1 Easy

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

class Circle {
    double r_ = 2;
public:
    double area() const;
};

double area() { return 3.14 * r_ * r_; }

int main() {
    Circle c;
    std::cout << c.area() << '\n';
    return 0;
}
A r_ must be public
B Without Circle:: the definition is a free function named area, which cannot see r_ (and Circle::area stays undefined); write double Circle::area() const
C area must be static
D 3.14 must be a constant
Question page & discussion
Question 2 Easy

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

class Player {
    int score_ = 0;
public:
    void add(int pts) const { score_ += pts; }
    int score() const { return score_; }
};

int main() {
    Player p;
    p.add(10);
    std::cout << p.score() << '\n';
    return 0;
}
A score_ must be static
B add is marked const but modifies score_; remove const from add (it is a modifying operation)
C pts must be a reference
D score() cannot be const
Question page & discussion
Question 3 Easy

The program below does not compile. Which statement correctly describes the problem?

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

class Vault {
    int code_ = 4321;
public:
    bool open(int c) const { return c == code_; }
};

int main() {
    auto v = std::make_unique<Vault>();
    std::cout << v->code_ << '\n';
    return 0;
}
A -> cannot be used with unique_ptr
B code_ is private; access control applies equally through objects, references and pointers
C Vault needs a constructor
D make_unique cannot create Vault
Question page & discussion
Question 4 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

class Greeter {
public:
    void hello() const { std::cout << "hello\n"; }
};

int main() {
    Greeter::hello();
    return 0;
}
A hello must return int
B hello is a non-static member function and needs an object: Greeter g; g.hello(); (or make it static)
C Greeter must be a struct
D cout cannot be used in members
Question page & discussion
Question 5 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

class Visitor {
public:
    static int count = 0;
    Visitor() { ++count; }
};

int main() {
    Visitor a, b;
    std::cout << Visitor::count << '\n';
    return 0;
}
A count must be private
B A non-const static data member cannot have an in-class initializer unless it is declared inline: static inline int count = 0; (C++17)
C Visitor needs a destructor
D Static members cannot be incremented
Question page & discussion
Question 6 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

class Engine;

class Car {
    Engine engine_;
public:
    void start();
};

class Engine {
public:
    void run() { std::cout << "vroom\n"; }
};

int main() {
    Car c;
    return 0;
}
A Car must be declared first
B Engine is only forward-declared when Car is defined, so Car cannot contain an Engine object (its size is unknown); define Engine first, or hold a pointer
C start must be inline
D Engine needs a constructor
Question page & discussion
Question 7 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

class Meter {
    double v_ = 1.5;
public:
    double value() const;
};

double Meter::value() { return v_; }

int main() {
    Meter m;
    std::cout << m.value() << '\n';
    return 0;
}
A v_ must be an int
B The definition omits const, so it does not match the declared double value() const – const is part of the member function's signature
C value must be virtual
D Meter needs a constructor
Question page & discussion
Question 8 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

struct Window {
    int width = 800;
    int height = 600;
    bool fullscreen = false;
};

int main() {
    Window w{.height = 720, .width = 1280};
    std::cout << w.width << 'x' << w.height << '\n';
    return 0;
}
A Window needs a constructor
B C++20 designated initializers must follow the order in which the members are declared: {.width = 1280, .height = 720}
C bool members are not allowed
D Designators need parentheses
Question page & discussion
Question 9 Easy

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

struct Point {
    int x, y;
};

int main() {
    Point a{1, 2}, b{1, 2};
    std::cout << (a == b) << '\n';
    return 0;
}
A Points must be pointers
B Point defines no operator==, so objects cannot be compared; add bool operator==(const Point&) const = default;
C x and y must be private
D cout cannot print bool
Question page & discussion
Question 10 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

class List {
    struct Node {
        int value;
        Node* next;
    };
public:
    int size() const { return 0; }
};

int main() {
    List::Node n{5, nullptr};
    std::cout << n.value << '\n';
    return 0;
}
A Node must have a constructor
B Node is declared in the private section of List, so code outside List cannot name it
C next must be a reference
D List must be a struct
Question page & discussion
Up next · Exercise 4Class Programs10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links