Java Interface Quiz

Home

Question: 1 2 3 4 5 6

Question One

A class can implement more than one interface?


A class can extend an interface?


An interface can extend an interface?


Interfaces can have constructors?


All members of an interface are public by default?


top next

Question Two

Why would you use an abstract class over an interface?

Can provide additional methods with code already implemented
You have multiple subclasses which need to perform the smae implementation of a task
Both of the above


back top next

Question Three


  interface A {

    final int k = 8;               // Line 1

    private int m;                 // Line 2

    public void aMethod();         // Line 3

    String fun(Char s);            // Line 4

  }

  

Which line will cause an error?

Line 1
Line 2
Line 3
Line 4


back top next

Question Four


  interface A {

    void go();

  }

  Class B {

    public void go() {

      System.out.println("GO!");
    
    }

  }

  Class C extends B implements A {}

  Class Main {

    publci static void main(String[] args) {

      A a = new C();
      a.go();

    }

  }

  

What is printed out?

result=


back top next


Question Five


  interface A {

    int n = 111;

  }

  Class B implements A {

    void inc() {

      n++;

    }

  }

  

Will this compile?


back top next


Question Six


  interface ABC {

    void methodOne();

  }

  interface PQR extends ABC {

    void methodTwo(int i);

  }

  abstract class XYZ implements PQR {

    public void methodOne() {

      methodTwo(2);

    }

  }

  class EFG extends XYZ {

    public void methodTwo(int i) {

      System.out.println(Math.pow(i, 2))

    }

  }

  class Main {

    public static void main(String[] args) {

      ABC abc = new EFG();
      abc.methodOne();

    }

  }

  

What is printed?


back top