Java MCQ
Java Tricky Interfaces Questions
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();
}
}
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();
}
}
The class B overrides the default method from interface A. Runtime polymorphism ensures the class implementation is called.
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);
}
}
int VALUE = 10;
}
interface Y {
int VALUE = 20;
}
class Z implements X, Y {
void display() {
System.out.println(VALUE);
}
}
When implementing multiple interfaces with same constant name, the reference becomes ambiguous. Use X.VALUE or Y.VALUE to specify.
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();
}
}
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();
}
}
Java 9+ allows private methods in interfaces. They can only be called from default or static methods within the same interface.
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();
}
}
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();
}
}
When implementing conflicting default methods, the class must override. Using InterfaceName.super.method() allows calling specific interface's default method.
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));
}
}
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));
}
}
Static methods in interfaces are not inherited by implementing classes. They can only be called using InterfaceName.method().
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
}
default void move() { System.out.print("Walking"); }
}
interface Runnable {
default void move() { System.out.print("Running"); }
}
class Athlete implements Walkable, Runnable {
// No method override
}
When a class implements multiple interfaces with conflicting default methods, it must override the method to resolve the conflict.
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());
}
}
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());
}
}
Abstract methods with same signature in multiple interfaces are treated as a single method. The implementing class provides one implementation.
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();
}
}
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();
}
}
When an interface extends another and overrides its default method, the most specific implementation (in the child interface) is used.
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));
}
}
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));
}
}
Lambda expressions provide implementation for functional interfaces. 2+3=5 and 2*3=6, printed together as "56".
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();
}
}
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();
}
}
Static and default methods with same name don't conflict. The class inherits the default method and static method remains interface-specific.