50 Java Interface Interview Programming Questions

32. What will be the output of the following programs?

a)

interface A {	
 void m1(int x, double y);	
}
abstract class B implements A {
public void m1(double x, int y){
  System.out.println("One");	 
 }
}
public class Test extends B {
public void m1(double x, int y){
 System.out.println("Two");
 super.m1(20.5, 20);
}
public void m1(int x, double y){
	System.out.println("Three");
}
public static void main(String[] args){
 Test t = new Test();
 t.m1(20.5, 10);
 t.m1(10, 5.5);
 }
}

Ans: Output: Two, One, Three

b)

interface A {	
 void m1(A a);	
}
abstract class B implements A {
  void m1(B b){
  System.out.println("One");	 
 }
}
public class Test extends B {
public void m1(A a){
 System.out.println("Two");
}
public void m1(B b){
	System.out.println("Three");
}
public static void main(String[] args){
 A a = new Test();
 a.m1(new Test());
 }
}

Ans: Output: Two

c)

interface A {	
 void m1(A a);	
}
abstract class B implements A {
 void m1(B b){
  System.out.println("One");	 
 }
}
public class Test extends B {
 void m1(A a){
 System.out.println("Two");
}
 void m1(B b){
	System.out.println("Three");
}
public static void main(String[] args){
 A a = new Test();
 a.m1(null);
 }
}

Ans: No, IllegalAcceessError exception because we cannot reduce the visibility of inherited method from interface A.

d)

interface A {	
 void m1();	
}
abstract class B implements A {
public void m1(){
  System.out.println("One");	 
 }
}
public class Test extends B {
public void m1(){
 System.out.println("Two");
 super.m1();
}
public static void main(String[] args){
 A a = new Test();
 a.m1();
 B b = new Test();
  b.m1();
 }
}

Ans: Output: Two, One, Two, One

e)

interface A {	
 int m1();	
}
class B implements A {
public int m1(){
  return 20;	 
 }
}
public class C implements A {
 public int m1(){
  return 30;	 
 }
}
public class Test {
public static void main(String[] args){
 A a = new B();
int aa =  a.m1();
System.out.println(aa);
 
C c = new C();
int cc = c.m1();
System.out.println(cc);
 }
}

Ans: Output: 20, 30.

f)

interface A {	
 A m1();	
}
class B implements A {
public A m1(){
  System.out.println("Red");	
  return null;	 
 }
}
public class C implements A {
 public A m1(){
  System.out.println("Orange");	 
  return null;	 
  }
}
public class Test {
public static void main(String[] args){
 A a = new B();
  a.m1();
 C c = new C();
  c.m1();
 }
}

Ans: Output: Red, Orange.

33. Will the below code compile successfully? If yes, what will be the output of the following program?

interface A {	
 A m1();	
}
class B implements A {
public B m1(){
  System.out.println("Red");	
  return new B();	 
 }
}
public class C implements A {
 public C m1(){
  System.out.println("Orange");	 
  return new C();	 
 } 
}
public class Test {
public static void main(String[] args){
 A a;
  a = new B();
  a.m1();
  a = new C();
  a.m1();
 }
}

Ans: Yes, the code will be successfully compiled. The result is Red, Orange.

34. What will be the output of the following program?

interface A {	
  int x = 20;	 
  interface B {
    int x = 30;  
  }
}
class C implements A {
 int x = 40;
}
public class D implements A.B {
 int x = 50;
}
public class Test {
public static void main(String[] args){
 System.out.println(A.x);
 System.out.println(A.B.x);
 
 C c = new C();
 System.out.println(c.x);
 
 D d = new D();
 System.out.println(d.x);
 }
}

Ans: Output: 20, 30, 40, 50.

35. Identify the error inside the following code and correct it. What will be the output after the correction of error?

interface A {	
  void m1();	 
  interface B {
    void m1();  
  }
}
class C implements A.B {	
 public void m1(){
  System.out.println("Green");	 
 }
}
public class Test {
public static void main(String[] args){
 A a = new C();
 a.m1();
 }
}

Ans: This code will generate compilation error because class C is implementing inner interface, not outer interface. But the reference of a is a type of outer interface. The correct code is as follows:

A.B ab = new C();

After correction, the output is Green.

36. Is there any error in the following code? If yes, correct it.

interface A {	
  void m1();	 
  interface B {
    void m2();  
  }
}
class C implements A, A.B {	
 public void m1(){
  System.out.println("Green");	 
  }
 }

Ans: Yes, there is an error in the above code because class C must implement inherited abstract method from inner interface A.B.

37. Will the code compile successfully? If yes, what will be the output?

interface A {	
  void m1();	 
  interface B {
    void m2(); 
  }
}
class C implements A, A.B {	
 public void m1(){
  System.out.println("Green");	 
  }
public void m2(){
  System.out.println("Red");	 
  }
 }
public class Test {
public static void main(String[] args){
 C c = new C();
 c.m1();
 c.m2();
 }
}

Ans: Yes, the above code will be compiled successfully. The output is Green, Red.

Please share your love