Java MCQ

Previous Java Multiple Choice Questions Next

Java Tricky Interfaces Questions

1

What is the output?
interface A {
  default void show() {
    System.out.print("A");
  }
}
class B implements A {
  public void show() {
    System.out.print("B");
  }
}
public class Test {
  public static void main(String[] args) {
    A obj = new B();
    obj.show();
  }
}

Correct Answer: B) B

The class B overrides the default method from interface A. Runtime polymorphism ensures the class implementation is called.

2

What will be printed?
interface X {
  int VALUE = 10;
}
interface Y {
  int VALUE = 20;
}
class Z implements X, Y {
  void display() {
    System.out.println(VALUE);
  }
}

Correct Answer: C) Compilation Error - ambiguous reference

When implementing multiple interfaces with same constant name, the reference becomes ambiguous. Use X.VALUE or Y.VALUE to specify.

3

What is the output?
interface I {
  private void privateMethod() {
    System.out.print("Private");
  }
  default void defaultMethod() {
    privateMethod();
  }
}
class C implements I { }
public class Test {
  public static void main(String[] args) {
    new C().defaultMethod();
  }
}

Correct Answer: A) Private

Java 9+ allows private methods in interfaces. They can only be called from default or static methods within the same interface.

4

What will be printed?
interface P {
  default void method() { System.out.print("P"); }
}
interface Q {
  default void method() { System.out.print("Q"); }
}
class R implements P, Q {
  public void method() {
    P.super.method();
    Q.super.method();
  }
}
public class Test {
  public static void main(String[] args) {
    new R().method();
  }
}

Correct Answer: C) PQ

When implementing conflicting default methods, the class must override. Using InterfaceName.super.method() allows calling specific interface's default method.

5

What is the output?
interface Calculator {
  static int add(int a, int b) {
    return a + b;
  }
}
class MyCalc implements Calculator { }
public class Test {
  public static void main(String[] args) {
    System.out.println(MyCalc.add(5, 3));
  }
}

Correct Answer: B) Compilation Error

Static methods in interfaces are not inherited by implementing classes. They can only be called using InterfaceName.method().

6

What will be printed?
interface Walkable {
  default void move() { System.out.print("Walking"); }
}
interface Runnable {
  default void move() { System.out.print("Running"); }
}
class Athlete implements Walkable, Runnable {
  // No method override
}

Correct Answer: D) Compilation Error

When a class implements multiple interfaces with conflicting default methods, it must override the method to resolve the conflict.

7

What is the output?
interface Alpha {
  String process();
}
interface Beta {
  String process();
}
class Gamma implements Alpha, Beta {
  public String process() {
    return "Gamma";
  }
}
public class Test {
  public static void main(String[] args) {
    Alpha a = new Gamma();
    Beta b = new Gamma();
    System.out.print(a.process() + b.process());
  }
}

Correct Answer: A) GammaGamma

Abstract methods with same signature in multiple interfaces are treated as a single method. The implementing class provides one implementation.

8

What will be printed?
interface A {
  default void show() { System.out.print("A"); }
}
interface B extends A {
  default void show() { System.out.print("B"); }
}
class C implements B { }
public class Test {
  public static void main(String[] args) {
    new C().show();
  }
}

Correct Answer: B) B

When an interface extends another and overrides its default method, the most specific implementation (in the child interface) is used.

9

What is the output?
interface MathOps {
  int operate(int a, int b);
}
public class Test {
  public static void main(String[] args) {
    MathOps add = (a, b) -> a + b;
    MathOps multiply = (a, b) -> a * b;
    System.out.print(add.operate(2, 3));
    System.out.print(multiply.operate(2, 3));
  }
}

Correct Answer: A) 56

Lambda expressions provide implementation for functional interfaces. 2+3=5 and 2*3=6, printed together as "56".

10

What will be printed?
interface One {
  default void test() { System.out.print("One"); }
}
interface Two {
  static void test() { System.out.print("Two"); }
}
class Three implements One, Two { }
public class Test {
  public static void main(String[] args) {
    new Three().test();
  }
}

Correct Answer: A) One

Static and default methods with same name don't conflict. The class inherits the default method and static method remains interface-specific.

Previous Java Multiple Choice Questions Next