Java OCA OCP Practice Question 1548

Question

Consider the following interface definition:

interface Printable{
    int type = 0;
    public void jump ();
}

Now consider the following class:

public class main implements Printable{
     public main (){
            type = 1;// w  w  w. j av a  2s . c  o m
     }

     public void jump (){
            System.out.println ("jumping..."+type);
     }

     public static void main (String [] args){
            Printable b = new main ();
            b .jump ();
     }
}

What will the program print when compiled and run?

Select 1 option

  • A. jumping...0
  • B. jumping...1
  • C. This program will not compile.
  • D. It will throw an exception at runtime.


Correct Option is  : C

Note

Fields defined in an interface are ALWAYS considered as public, static, and final.

Methods are public and abstract.

Even if you don't explicitly define them as such.

You cannot even declare a field to be private or protected in an interface.

You cannot assign any value to 'type' outside the interface definition.




PreviousNext

Related