ProgrammingVery hardAmazon/Microsoft SDE MCQ · Java placement

Which line fails to compile?

Which line fails to compile?
interface I { static String s() { return "I"; } default String d() { return "D"; } }
class C implements I { }
// in main:
String a = I.s();         // line 1
String b = new C().d();   // line 2
String c = C.s();         // line 3
String e = ((I) new C()).d(); // line 4
  1. A. Line 1
  2. B. Line 2
  3. C. Line 4
  4. D. Line 3

Correct answer: D. Line 3

Static methods of an interface are not inherited by implementing classes, so they must be called as I.s(); C.s() does not compile. Default methods are inherited and callable on C or through I.

More OOP with C++ and Java questions

Exams that ask this topic