Example of interface in java

Objective type questions on interface in java and answers


(1)

interface Apple{
    float cost=9.5f;
    public void display();
}

class Try implements Apple{
    
    public static void main(String[] args){
         Try t=new Try();
         t.display();
    }

    public void display(){
         System.out.print("cost of apple is :"+cost);
    }
}

What will output when you compile and run the above code?

(a) cost of apple is :9.5
(b) cost of apple is :0.0
(c) cost of apple is :9.5f
(d)Compiler error






Answer: (a)

(2)

interface Inf{
    double a,b;
    public void add(double p,double q);
}

class Try implements Inf{

    public static void main(String[] args){
         Try t=new Try();
         t.add(11.0,22.0);
    }

    public void add(double p,double q){
         a=p;
         b=q;
         System.out.print(a+b);
    }
}

What will output when you compile and run the above code?

(a)11.0
(b)22.0
(c)33.0
(d)Compiler error






Answer: (d)

(3)

interface Inf{
    public long add(int p,int q);

    public void display(long sum){
         Systm.out.print(sum);
    }
}

class Try implements Inf{

    public static void main(String[] args){
         Try t=new Try();
         long sum=t.add(130,120);
         t.display(sum);
    }

    public long add(int p,int q){
         return p+q;
    }
}

What will output when you compile and run the above code?

(a)130
(b)120
(c)250
(d)Compiler error






Answer: (d)

(4)

interface Inf{
    long cal(int p);
}

class Try implements Inf{
    public static void main(String[] args){
         Try t=new Try();
         long sum=t.cal(25);
         System.out.print(sum);
    }

    long cal(int p){
         return p++;
    }
}

What will output when you compile and run the above code?

(a)25
(b)26
(c)27
(d)Compiler error






Answer: (d)

(5)

interface Inf{
    int a=5;
}

class Test{
    int b;
}

class Try extends Test implements Inf{
    public static void main(String[] args){
         Try t=new Try();
         System.out.print(a+t.b);
    }
}

What will output when you compile and run the above code?

(a)5
(b)10
(c)Garbage value
(d)Compiler error






Answer: (a)

(6)

class Test{
    int a=0;
   
    protected interface Ni{
         short calculation(int p);
    }
}

class Try extends Test implements Test.Ni{
    
    public static void main(String[] args){
         Try t=new Try();
         short val=t.calculation(t.a);
         System.out.print(val);
    }  
   
    public short calculation(int p){
         for(int i=0;i<2;i++){
             int a=2;
             ++a;
         }
         return (short)(a+p);
    }
   
}

What will output when you compile and run the above code?

(a)0
(b)3
(c)4
(d)Compiler error





Answer: (a)

(7)

class Mango{
    final int a=5;
}

class Fruit extends Mango {
    final int a=10;
}

class DynamicDispatch  extends Fruit{
    final int a=20;
   
    public static void main(String[] args){
         Mango m=new DynamicDispatch();
         Fruit f=new DynamicDispatch();
         System.out.print(m.a|f.a);
    }
}

What will be output of above program?

(a)15
(b)20
(c)30
(d)Compiler error





Answer: (a)

(8)

class Mango{
    final int a=5;
}

class Fruit extends Mango {
    final int a=10;
}

class DynamicDispatch  extends Fruit{
    final int a=15;
   
    public static void main(String[] args){
         DynamicDispatch t=new Mango();
         System.out.print(t.a);
    }
}

What will be output of above program?

(a)5
(b)10
(c)15
(d)Compiler error





Answer: (d)

No comments: