Given: 10. interface Foo { int bar(); } 11. public class Sprite { 12. public int fubar( Foo foo) { return foo.bar(); } 13. public void testFoo() { 14. fubar( 15. // insert code here 16.); 17. } 18. } Which code, inserted at line 15, allows the class Sprite to compile?

  1. Foo { public int bar() { return 1; } }

  2. new Foo { public int bar() { return 1; } }

  3. newFoo() { public int bar(){return 1; } }

  4. new class Foo { public int bar() { return 1; } }


Correct Option: C

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) Foo { public int bar() { return 1; } } This option is incorrect because it is missing the keyword "new" before the Foo interface. The Foo interface needs to be instantiated with a concrete implementation.

Option B) new Foo { public int bar() { return 1; } } This option is incorrect because it is missing a set of parentheses after "new Foo". When instantiating an interface with an anonymous class, the parentheses are required.

Option C) newFoo() { public int bar(){return 1; } } This option is correct because it correctly instantiates the Foo interface with an anonymous class and provides an implementation for the "bar()" method.

Option D) new class Foo { public int bar() { return 1; } } This option is incorrect because it is attempting to create a new class named "Foo" instead of instantiating the Foo interface. The keyword "class" is not needed in this context.

The correct answer is Option C. This option is correct because it properly instantiates the Foo interface with an anonymous class and provides an implementation for the "bar()" method.

Find more quizzes: