ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
void f(int x) { cout << "int"; }
void f(double x) { cout << "double"; }
int main() { f('a'); }- A. double
- B. Ambiguous call: compile error
- C. int
- D. Both overloads run
Correct answer: C. int
A char argument undergoes integral promotion to int, which ranks as an exact match in overload resolution; converting char to double is a floating-point conversion, a lower rank. So f(int) wins unambiguously and "int" is printed.
More OOP with C++ and Java questions
- What happens when this is compiled? java class A { final void m() { System.out.println("A"); } } class B extends A { void m() {…
- What happens when this is compiled? java class Picker { static void f(String s) { System.out.print("String"); } static void…
- What is printed? java class A { static void m() { System.out.println("A.static"); } } class B extends A { static void m() {…
- What happens when this is compiled? cpp struct A { virtual void f() final { cout << "A"; } }; struct B : A { void f() override { cout <<…
- What happens when this is compiled? java class A { int m() { return 1; } double m() { return 1.0; } }
- What is printed? cpp void f(int p) { cout << "nonconst "; } void f(const int p) { cout << "const "; } int main() { int x = 5; int p = &x…