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(); }- A. Compile-time error: S::log takes no arguments
- B. G
- C. M
- 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
- What happens (C++14)? cpp struct S { static int n; }; int main() { S::n = 1; cout << S::n; }
- Which lines fail to compile? cpp void f(int) { cout << "I"; } void f(double) = delete; int main() { f(5); // L1 f('a'); // L2 f(2.5); //…
- Which lines fail to compile? cpp struct H { static H make() { return new H; } void destroy() { delete this; } private: ~H() { } }; int…
- What is printed? cpp struct B { virtual ~B() { } }; int main() { B p = nullptr; try { cout << typeid(p).name(); } catch (const badtypeid&)…
- What is printed? java class R implements AutoCloseable { String n; R(String n, boolean fail) { if (fail) throw new…
- What happens when this is compiled? java interface I { int f(); } abstract class A { abstract long f(); } class C extends A implements I {…