Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Tuesday, 24 October 2017

Interface Programming Interview Questions in Java

Java Interface Programming Interview Questions

Interface Programming in Java

Now, Here we are going to discuss some important interface programming interview questions in java. Java interface is also a most important topic and mostly asked in any core java interviews.

Let's see some questions related to interface in java programming which is frequently asked in java interviews.


(1) What is the output of following program?

interface My
{
void fun();
}
class My1 implements My
{
void fun()
{
System.out.println("hi");
}
public static void main(String args[])
{
My1 m = new My1();
m.fun();
}
}

Output: compile time error

Explanation: By default all the methods of an interface are public and abstract and when we will implement or override a method in a class which implements interface then we have to use public modifier with method e.g public void fun(){} in My1 class.


(2) What is the output of the following program?

interface Demo
{
void show()
{
System.out.println("hi, i am good");
}
void run();
}
class Test implements Demo
{
public void run()
{
System.out.println("run fast");
}
public static void main(String args[])
{
Test t = new Test();
t.run();
}
}

Output: compile time error

Explanation: We can't keep method with body in interface before java 8 version but it possible to keep method body in interface of java 8 by using 'default' keyword.


(3) Can you identify the error in below program?

interface Demo
{
private int b;
}

Output: Illegal modifier for field b

Explanation: By default all the fields of an interface is public, static and final. 


(4) What is the output of the following program?

interface First
{
void test();
}
class Second implements First
{
public void test()
{
System.out.println("pass");
}
public static void main(String args[])
{
First f = new First();
f.test();
}
}

Output: compile time error

Explanation: We can't instantiate interface in java.


(5) What is the output of following program?

interface First
{
First()
{
System.out.println("hello");
}
void show();
}
class Test implements First
{
public void show()
{
System.out.println("how are you");
}
public static void main(String args[])
{
Test t = new Test();
t.show();
}
}


Output: compile time error

Explanation: We can't keep constructor in an interface.


(6) What is the output of a below program?

interface My
{
int i = 10;
}
class My1 implements My
{
void change()
{
i = 40;
System.out.println(i);
}
public static void main(String args[])
{
My1 m = new My1();
m.change();
}
}

Output: compile time error


(7) What is the output of following program?

interface First
{
void show();
}
interface Second
{
void display();
}
class Test implements First,Second
{
public void show()
{
System.out.println("first interface");
}
public void display()
{
System.out.println("second interface");
}
public static void main(String args[])
{
Test t = new Test();
t.show();
t.display();
}
}

Output: first interface
             second interface


(8) Declaration of below code is correct or incorrect?

class A
{
//statements
}
interface B extends A
{}

Output: incorrect because interface cannot extend a class

(9) Declaration of below code is correct or incorrect?

interface A
{
//statements
}
interface B extends A
{}

Output: correct because an interface can extend one or multiple interface at a time.


(10) What is the output of a following program?


interface First

{

void show();

}
interface Second
{
void display();
}
class Test implements First,Second
{
public void show()
{
System.out.println("first interface");
}
public void display()
{
System.out.println("second interface");
}
public static void main(String args[])
{
First f = new Test();
f.show();
f.display();
}
}

Output: compile time error

Explanation: To run the above program you can create....

1) Test t = new Test();
     t.show();
     t.show();

2) First f = new Test();
    Second s = new Test();
    f.show();
    s.display();


You can check String programming interview questions and inheritance programming interview questions in java.

This is the basic and important java interface programming interview questions and answers.  
Share:

2 comments:

  1. Hi,
    This is really a nice blog by you. I really appreciate your efforts for this blog. Keep it up and keep posting such blogs.
    Exam time is always a hectic time for students. It is seen that most of the students feel anxiety at the time of exams and it is so normal. There are some doubts of students which are unanswered in the classroom that bother students at the time of exams. This is why most of the students prefer to take online tuition for class 9 to class 12th from the beginning.
    The Ultimate Guide to Stars and the Milky Way

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate