LearnFrenzy
Sign in Take a free test

Static and Friend Members · Exercise 3 of 4

Static and Friend Members Find the Error

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

Each C++ program contains a static or friend 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 Order {
    double amount_ = 499;
public:
    static double with_tax() { return amount_ * 1.18; }
};

int main() {
    std::cout << Order::with_tax() << '\n';
    return 0;
}
A 1.18 must be a constant
B with_tax is static, so there is no object whose amount_ it could read; make it a non-static member or pass an Order
C amount_ must be public
D static functions cannot return double
Question page & discussion
Question 2 Medium

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

main.cpp
#include <iostream>

class Version {
public:
    static int major() const { return 2; }
};

int main() {
    std::cout << Version::major() << '\n';
    return 0;
}
A major is a keyword
B A static member function cannot be const: const qualifies the object pointed to by this, and a static function has no this
C static functions must be private
D Version needs a constructor
Question page & discussion
Question 3 Easy

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

main.cpp
#include <iostream>

class Node {
    int value_ = 3;
public:
    static Node* self() { return this; }
};

int main() {
    std::cout << (Node::self() != nullptr) << '\n';
    return 0;
}
A Node* cannot be returned
B this is not available in a static member function, because it is not called on an object
C value_ must be static
D nullptr cannot be compared
Question page & discussion
Question 4 Medium

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

main.cpp
#include <iostream>

class Shape {
public:
    virtual static const char* kind() { return "shape"; }
    virtual ~Shape() = default;
};

int main() {
    std::cout << Shape::kind() << '\n';
    return 0;
}
A const char* cannot be returned
B A member function cannot be both static and virtual: virtual dispatch needs an object, and static functions have none
C virtual must come after static
D Shape must be abstract
Question page & discussion
Question 5 Easy

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

main.cpp
#include <iostream>

class Box {
    int volume_ = 27;
public:
    friend int volume(const Box& b);
};

int volume(const Box& b) { return b.volume_; }

int main() {
    Box b;
    std::cout << b.volume() << '\n';
    return 0;
}
A volume_ must be public
B volume is a friend, not a member, so it is called as volume(b) – Box has no member named volume
C friend functions must be static
D b must be const
Question page & discussion
Question 6 Medium

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

main.cpp
#include <iostream>

class Safe {
    int code_ = 1234;
    friend class Manager;
};

class Manager {
public:
    static int read(const Safe& s) { return s.code_; }
};

class Intern : public Manager {
public:
    static int peek(const Safe& s) { return s.code_; }
};

int main() {
    Safe s;
    std::cout << Manager::read(s) << ' ' << Intern::peek(s) << '\n';
    return 0;
}
A Manager::read is not allowed either
B Friendship is not inherited: Manager may access code_, but its derived class Intern may not
C Intern must derive from Safe
D code_ must be protected
Question page & discussion
Question 7 Medium

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

main.cpp
#include <iostream>

class A {
    int secret_ = 42;
    friend class B;
};

class C;

class B {
    friend class C;
};

class C {
public:
    static int steal(const A& a) { return a.secret_; }
};

int main() {
    A a;
    std::cout << C::steal(a) << '\n';
    return 0;
}
A B must have members
B Friendship is not transitive: C is a friend of B, and B is a friend of A, but C is not a friend of A
C secret_ must be static
D C must be declared before A
Question page & discussion
Question 8 Hard

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

main.cpp
#include <iostream>

class Auditor;

class Ledger {
    int total_ = 900;
    friend int Auditor::check(const Ledger&);
};

class Auditor {
public:
    int check(const Ledger& l) { return l.total_; }
};

int main() {
    Ledger l;
    Auditor a;
    std::cout << a.check(l) << '\n';
    return 0;
}
A check must be static
B Auditor is only forward-declared at the friend declaration, so Auditor::check is unknown; define Auditor (with check declared) before Ledger, or befriend the whole class
C total_ must be public
D friend must be at the top of the class
Question page & discussion
Question 9 Easy

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

main.cpp
#include <iostream>

class Settings {
public:
    static inline int volume = 40;
};

void louder() {
    volume += 10;
}

int main() {
    louder();
    std::cout << Settings::volume << '\n';
    return 0;
}
A volume must be const
B Outside the class, a static member must be named with its class: Settings::volume; plain volume is undeclared in louder()
C louder must be a friend
D static inline is not allowed
Question page & discussion
Question 10 Hard

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

main.cpp
#include <iostream>

class Temperature {
    double c_;
public:
    explicit Temperature(double c) : c_(c) {}
    friend double to_kelvin(double celsius) { return celsius + 273.15; }
};

int main() {
    std::cout << to_kelvin(25.0) << '\n';
    return 0;
}
A 273.15 must be an int
B to_kelvin is a hidden friend defined only inside the class; it is found only by argument-dependent lookup, and its argument is a double, not a Temperature – so it is not visible here
C Temperature has no default constructor
D friend functions cannot return double
Question page & discussion
Up next · Exercise 4Static and Friend Programs10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links