38. Identify the error in the below code. If no error, what will be the output of the program?
interface A {
void m1();
interface B {
void m2();
}
}
interface C extends A, A.B {
strictfp default void m1(){
System.out.println("Green");
}
strictfp default void m2(){
System.out.println("Red");
}
}
public class Test implements C{
public static void main(String[] args){
Test t = new Test();
t.m1();
t.m2();
}
}
Ans: No error. The result is Green, Red.
39. What will be the output of following program if no error?
public class A {
void m1() {
System.out.println("One");
}
interface B {
void m2();
}
}
class C extends A {
void m1(){
System.out.println("Two");
super.m1();
}
}
public class Test implements A.B{
public void m2(){
System.out.println("Three");
}
public static void main(String[] args){
Test t = new Test();
t.m2();
C c = new C();
c.m1();
}
}
Ans: No error. The output is Three, Two, One.
40. What is a Functional interface in Java 8?
Ans: An interface that has exactly one abstract method is called functional interface in java. It is also known as single abstract method interface.
Functional interface can have three kinds of methods: abstract, default, and static methods.
Functional interface can have default methods with implementation. A default method cannot be abstract. For example, java.lang.Runnable and java.util.concurrent.Callable are two very popular Functional interfaces in java 8.
41. How to define a Functional interface in Java 8?
Ans: To define a Functional interface in Java 8, we can create an interface with exactly one abstract method like this:
interface A {
void m1();
}Another way is to define an Interface with annotation @FunctionalInterface. This annotation gives an instruction to the java compiler to verify that the interface has only one abstract method.
42. Is it mandatory to use @FunctionalInterface annotation with Functional interface in Java 8?
Ans: No, it is not mandatory to define a Functional interface with @FunctionalInterface annotation. Java does not impose this rule.
But, if we define an interface with @FunctionalInterface annotation, Java Compiler will give an error in case we define more than one abstract method within that interface.
43. What is default method in Java 8?
Ans: A method that is defined inside an interface with the default keyword and concrete method body is called default method. It provides a default implementation for classes that implements an interface.
This method can be overridden by a class that implements interface. A default method is public by definition and cannot be declared with private, protected, static, final, or abstract modifiers.
44. Which of the following are valid or non-valid default methods defined in an interface?
a) interface A {
abstract void m1();
default int m2() { return 10; }
}
b) interface A {
abstract void m1();
public default void m2(){ System.out.println("Hello Java");}
}
c) interface A {
static default m1() { }
}
d) interface A {
abstract void m1();
static void m2() {
System.out.println("Hello Java");
}
public default void m3();
}
Ans: a and b are valid codes. c and d are non-valid codes.
45. What is the output of the following program?
interface A {
abstract void m1();
static void m2(){
System.out.println("Static method");
}
default void m3(){
System.out.println("Default method");
}
}
class B implements A {
public void m1(){
System.out.println("Overridden m1 method");
}
}
public class Test{
public static void main(String[] args){
A a = new B();
a.m1();
A.m2();
a.m3();
}
}
Ans: The result is Overridden m1 method, Static method, Default method.
46. Will the code successfully compile? If yes, what will be the output of the program?
interface A {
abstract void m1();
public static void m2(){
System.out.println("One");
}
default void m3(){
System.out.println("Two");
}
}
class B implements A {
public void m1(){
System.out.println("Three");
}
void m3(){
System.out.println("Four");
}
}
public class Test{
public static void main(String[] args){
A a = new B();
a.m1();
A.m2();
a.m3();
}
}
Ans: No, the code will not be compiled successfully because we cannot reduce the visibility of inherited m3() method from interface A.
47. Identify the error in the following code and correct it.
interface A {
abstract void m1();
abstract default void m2(){
System.out.println("Two");
}
}
class B implements A {
public void m1(){
System.out.println("Three");
}
}
public class Test{
public static void main(String[] args){
A a = new B();
a.m1();
a.m2();
}
}
Ans: Default method cannot be abstract. Remove it and then compile.
48. Why do we need Functional interface in Java 8?
Or, what is the use of Functional interface in Java 8?
Ans: A Functional interface is mainly used in Lambda expressions, method reference, and constructor references.
49. Can we declare private method inside an interface in JDK 9?
Ans: Before JDK 9, all methods defined in an interface were implicitly public. But JDK 9 allow to declare private method inside an interface.
The private method defined in an interface cannot be called or overridden by implementing classes.
50. Can we declare a method in an interface with combinations of modifiers like abstract, public, static, and private in JDK 9?
Ans: No, because they do not make sense.
Table: Supported Modifiers in Method Declaration in Interface
| Modifiers | Supported? | Description |
|---|---|---|
| public static | Yes | Supported since JDK 8. |
| public abstract | Yes | Supported since JDK 1. |
| public default | Yes | Supported since JDK 8. |
| private static | Yes | Supported since JDK 9. |
| private | Yes | Supported since JDK 9. It is a non-abstract instance method. |
| private abstract | No | This combination does not make sense because a private method is not inherited. So, it cannot be overridden. |
| private default | No | It is also not possible. |
Recommended Interview Questions
- Top 85++ OOPs Interview Questions with Answers
- Top 32 Interview Questions on Polymorphism
- Top 65 Interview Questions on Exception Handling
- Exception Handling Programming Questions in Java for best Practice
Hope that these 50 Java interface interview questions and answers will help you to understand the level of questions asked by interviewers in different companies. I hope that you will have practiced interface interview programming questions and enjoyed them. Please, share it on social networking sites for your friends.
All the best!!!





