ProgrammingVery hardProduct-company placement · C++

What is printed?

What is printed?
void log(int) { cout << "G"; }
struct S {
    void log()  { cout << "M"; }
    void run()  { log(5); }
};
int main() { S s; s.run(); }
  1. A. Compile-time error: S::log takes no arguments
  2. B. G
  3. C. M
  4. D. GM

Correct answer: A. Compile-time error: S::log takes no arguments

Unqualified name lookup inside a member function finds S::log in class scope and stops there, hiding the global log. Overload resolution then fails because S::log() takes no arguments; ::log(5) would work.

More OOP with C++ and Java questions

Exams that ask this topic