🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Abstract Classes and Interfaces: all 10 questions

Quiz: Abstract Classes and Interfaces · Question 4 of 10

What does this print? (code)

What does this print?

interface Greeter { default String hi() { return "hi"; } }
class Friend implements Greeter { }
public class Main {
    public static void main(String[] args) {
        System.out.println(new Friend().hi());
    }
}
  1. A.hi
  2. B.null
  3. C.Error: hi() not implemented
  4. D.Friend
Answer & explanation

Correct answer: hi

Default methods have a body, so implementing classes get them for free.

Can you get the rest right without peeking?

Take the timed Quiz: Abstract Classes and Interfaces quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →