ProgrammingVery hardProduct-company placement · C++
Which lines fail to compile?
Which lines fail to compile?
void f(int) { cout << "I"; }
void f(double) = delete;
int main() {
f(5); // L1
f('a'); // L2
f(2.5); // L3
f(2.5f); // L4
}- A. L3 only
- B. L2, L3 and L4
- C. L4 only
- D. L3 and L4
Correct answer: D. L3 and L4
Deleted functions still take part in overload resolution; choosing one is an error. char → int is a promotion (L2 picks f(int)), but float → double is also a promotion, which beats float → int, so f(2.5f) selects the deleted overload.
More OOP with C++ and Java questions
- 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 {…
- What happens when this is compiled? java interface I { default void f() { System.out.print("I"); } } abstract class A { public abstract…
- What is printed? java int[] arr = {1, 2, 3}; for (int x : arr) x = 2; System.out.print(Arrays.toString(arr));