LearnFrenzy
Sign in Take a free test

Operator Overloading · Exercise 3 of 4

Operator Overloading Find the Error

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

Each C++ program overloads an operator incorrectly, and the compiler rejects it. 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>

struct Flag {
    bool on;
    int operator?:(int a, int b) const { return on ? a : b; }
};

int main() {
    Flag f{true};
    std::cout << (f ? 1 : 2) << '\n';
    return 0;
}
A on must be private
B ?: is one of the operators that cannot be overloaded (with ::, ., .*, sizeof, typeid, alignof)
C It must be a friend
D It must return bool
Question page & discussion
Question 2 Easy

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

main.cpp
#include <iostream>

int operator+(int a, int b) { return a - b; }

int main() {
    std::cout << 5 + 3 << '\n';
    return 0;
}
A The function needs to be inline
B An overloaded operator needs at least one operand of class or enumeration type; built-in int + int cannot be redefined
C It must return long
D operator+ must be a member
Question page & discussion
Question 3 Medium

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

main.cpp
#include <iostream>

struct Money {
    long paise;
    Money operator+(const Money& a, const Money& b) const { return {a.paise + b.paise}; }
};

int main() {
    Money x{100}, y{250};
    std::cout << (x + y).paise << '\n';
    return 0;
}
A paise must be int
B As a member, binary operator+ takes ONE parameter (the left operand is *this); with two it has too many – make it a non-member or drop a parameter
C operator+ cannot be const
D Money needs a constructor
Question page & discussion
Question 4 Medium

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

main.cpp
#include <iostream>

struct Point {
    int x, y;
    std::ostream& operator<<(std::ostream& os) const { return os << x << ',' << y; }
};

int main() {
    Point p{2, 3};
    std::cout << p << '\n';
    return 0;
}
A x and y must be private
B As a member, operator<< has Point on the LEFT (p << std::cout), so std::cout << p finds no matching operator; make it a non-member taking (std::ostream&, const Point&)
C It must return void
D Point needs a destructor
Question page & discussion
Question 5 Medium

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

main.cpp
#include <iostream>

struct Scores { int data[3]{70, 80, 90}; };

int& operator[](Scores& s, int i) { return s.data[i]; }

int main() {
    Scores s;
    std::cout << s[1] << '\n';
    return 0;
}
A i must be size_t
B operator[] must be a non-static member function; it cannot be declared at namespace scope
C data must be a vector
D It must return by value
Question page & discussion
Question 6 Medium

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

main.cpp
#include <iostream>

struct Celsius {
    double v;
    explicit operator double() const { return v; }
};

int main() {
    Celsius t{36.6};
    double d = t;
    std::cout << d << '\n';
    return 0;
}
A v must be float
B The conversion operator is explicit, so double d = t is not allowed; write static_cast<double>(t)
C Celsius needs a constructor
D operator double must be static
Question page & discussion
Question 7 Easy

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

main.cpp
#include <iostream>

struct Num {
    double v;
    Num operator**(int e) const {
        double r = 1;
        for (int i = 0; i < e; ++i) r *= v;
        return {r};
    }
};

int main() {
    Num n{2};
    std::cout << (n ** 10).v << '\n';
    return 0;
}
A e must be unsigned
B C++ has no ** operator, and new operators cannot be created; use a named function such as pow()
C operator** must be a friend
D The loop is wrong
Question page & discussion
Question 8 Medium

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

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

class Row {
    std::vector<int> v_{1, 2, 3};
public:
    int& operator[](std::size_t i) { return v_[i]; }
};

int sum_first_two(const Row& r) { return r[0] + r[1]; }

int main() {
    Row r;
    std::cout << sum_first_two(r) << '\n';
    return 0;
}
A Indices must be int
B r is const inside sum_first_two, but operator[] is non-const; add a const overload returning const int& (or int)
C Row needs a destructor
D v_ must be public
Question page & discussion
Question 9 Hard

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

main.cpp
#include <iostream>

struct Clock {
    int ticks = 0;
    Clock operator++(double) { Clock old = *this; ++ticks; return old; }
};

int main() {
    Clock c;
    c++;
    std::cout << c.ticks << '\n';
    return 0;
}
A Clock must have a prefix ++ first
B The postfix form must take a parameter of type int; operator++(double) is not a valid increment operator
C It must return Clock&
D ticks must be static
Question page & discussion
Question 10 Hard

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

main.cpp
#include <iostream>

constexpr double operator""_kg(long double x) { return static_cast<double>(x); }

int main() {
    double a = 2.5_kg;
    double b = 70_kg;
    std::cout << a + b << '\n';
    return 0;
}
A _kg must be written kg
B Only the long double form is declared, so 70_kg (an integer literal) has no matching operator""_kg(unsigned long long)
C constexpr cannot be used with literals
D a and b must be long double
Question page & discussion
Up next · Exercise 4Operator Overloading Programs10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links