Interview Questions

Q:

What is the difference between an Interface and an Abstract class ?

Answer

1.Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.


2.Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.


3.Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc..


4.Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.


5.An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.


6.A Java class can implement multiple interfaces but it can extend only one abstract class.


7.Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.


8.In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.

Report Error

View answer Workspace Report Error Discuss

Subject: Java

0 1797
Q:

Which of the following statements are equal for a variable declared in the interface ?

1. int X=10
2. public int X=10
3. public static final int X=10

A) Only 1 and 2 are equal B) Only 2 and 3 are equal
C) All are unequal D) All are equal
 
Answer & Explanation Answer: D) All are equal

Explanation:

All the statements are equal because inside an interface a variable declared will be public static final by default 

Report Error

View Answer Report Error Discuss

Filed Under: Java
Job Role: IT Trainer

0 13178
Q:

Inside an interface when should the variables be initialized?

A) Globally B) Whenever required
C) Inside a function D) During the time of declaration
 
Answer & Explanation Answer: D) During the time of declaration

Explanation:

They should be initialized during the time of declaration itself otherwise it is a compilation eror

Report Error

View Answer Report Error Discuss

Filed Under: Java
Job Role: IT Trainer

0 4014
Q:

Which of the following method declarations are allowed inside interface?

A) public void m1() B) private void m1()
C) protected void m1() D) abstract public void m1()
 
Answer & Explanation Answer: D) abstract public void m1()

Explanation:

Inside an interface every method will be by default abstract and public

Report Error

View Answer Report Error Discuss

Filed Under: Java
Job Role: IT Trainer

0 3937
Q:

Why interface method is always public?

Answer

To make this method available to every implementation class

Report Error

View answer Workspace Report Error Discuss

Subject: Java
Job Role: IT Trainer

0 2122
Q:

Which among the following is a scope resolution operator?

A) : B) ::
C) :? D) None
 
Answer & Explanation Answer: B) ::

Explanation:

:: is the scope resolution operator

Report Error

View Answer Report Error Discuss

Filed Under: Java
Job Role: Software Architect

0 4988
Q:

Which kind of inheritance is not supported directly through classes in java?

A) Multilevel B) Multiple
C) Single D) Hirarcheal
 
Answer & Explanation Answer: B) Multiple

Explanation:

Multiple inheritance is not supported directly but it is achieved through the concept of Interface

Report Error

View Answer Report Error Discuss

Filed Under: Java
Job Role: Software Architect

0 2869
Q:

public class Threads2 implements Runnable {

public void run() {

System.out.println("run.");

throw new RuntimeException("Problem");

Which among the following is true?

}

public static void main(String[] args) {

Thread t = new Thread(new Threads2());

t.start();

System.out.println("End of method.");

}

}

A) java.lang.RuntimeException: Problem B) run. java.lang.RuntimeException: Problem
C) End of method. java.lang.RuntimeException: Problem D) End of method. run. java.lang.RuntimeException: Problem
 
Answer & Explanation Answer: D) End of method. run. java.lang.RuntimeException: Problem

Explanation:

Only D can be true

Report Error

View Answer Report Error Discuss

Filed Under: Java

0 22523