Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

How Do We Use Comparator And Comparable Interfaces?

Answer»

java.util. COMPARATOR

java.util.Comparator: COMPARES some other CLASSES instances.

java.lang. COMPARABLE

java.lang.Comparable: compares another object with itself.

java.util. Comparator

java.util.Comparator: compares some other classes instances.

java.lang. Comparable

java.lang.Comparable: compares another object with itself.

2.

How Can We Define An Interface?

Answer»

In Java an interface just defines the methods and not implement them. Interface can include constants.

A CLASS that implements the interfaces is BOUND to implement all the methods defined in an interface.

Example of Interface :

public interface SAMPLE Interface 

{

public void function One();

public long CONSTANT_ONE = 1000;

}

In Java an interface just defines the methods and not implement them. Interface can include constants.

A class that implements the interfaces is bound to implement all the methods defined in an interface.

Example of Interface :

public interface sample Interface 

{

public void function One();

public long CONSTANT_ONE = 1000;

}

3.

Can An Inner Class Be Built In An Interface?

Answer»

Yes, an inner class can be BUILT in an interface.

EXAMPLE:

public interface xyz

{

static int p=0;

VOID m();

class C

{

c ()

{

int q;

System.out.println("inside");

};

public static void main(String c[])

{

System.out.println("inside ");

}

}

}

Yes, an inner class can be built in an interface.

Example:

public interface xyz

{

static int p=0;

void m();

class c

{

c ()

{

int q;

System.out.println("inside");

};

public static void main(String c[])

{

System.out.println("inside ");

}

}

}

4.

What Will Happen If We Do Not Override All The Abstract Methods In Sub-class?

Answer»

It will throw compile-time error. We have to OVERRIDE all the abstract METHOD in sub-CLASS. If you do not want to override all the abstract method in sub-class then you have to make your sub-class as abstract class.

It will throw compile-time error. We have to override all the abstract method in sub-class. If you do not want to override all the abstract method in sub-class then you have to make your sub-class as abstract class.

5.

Differentiate An Interface And An Abstract Class?

Answer»
  • An abstract CLASS may have many instance methods which sport default behavior. 
  • On the other hand, an INTERFACE cannot implement any default behaviour. 
  • However, it can declare DIFFERENT constants and instance methods. 
  • Whereas an interface has all the public MEMBERS, an abstract class contains only class members like private, protected and so on.

6.

Can We Use Public, Protected And Default Modifiers With Abstract Method?

Answer»

YES, we can USE PUBLIC, protected and default modifiers with abstract METHOD.

Yes, we can use public, protected and default modifiers with abstract method.

7.

Can We Declare Abstract Method As Private?

Answer»

No, we cannot DECLARE ABSTRACT METHOD as PRIVATE.

No, we cannot declare abstract method as private.

8.

Can We Declare Abstract Method As Final?

Answer»

No, we cannot USE FINAL KEYWORD with ABSTRACT CLASS.

No, we cannot use final keyword with abstract class.

9.

Can We Declare Abstract Method As Static?

Answer»

No, we can't use static KEYWORD with ABSTRACT METHOD.

No, we can't use static keyword with abstract method.

10.

Can We Create Instance Of Interface?

Answer»

No, we cannot CREATE OBJECT of both an INTERFACE and ABSTRACT CLASS.

No, we cannot create object of both an interface and abstract class.

11.

Why We Use Interface In Java?

Answer»

In java we use interface so that we can achieve FULLY ABSTRACTION because through ABSTRACT CLASS we can't achieve FULL abstraction.

In java we use interface so that we can achieve fully abstraction because through abstract class we can't achieve full abstraction.

12.

In Which Kind Of Situation Would An Interface Be Extended By Another Interface?

Answer»

Remember that any class that implements an interface must implement the METHOD headings that are declared in that interface.

If that PARTICULAR interface extends from other interfaces, then the IMPLEMENTING class must also implement the methods in the interfaces that are being extended or derived from.

As SHOWN in the example above, if we have a class those implements the FourLegs interface, then that class must DEFINE the method headings in both the 'FourLegs' interface and the 'Body' interface.

Remember that any class that implements an interface must implement the method headings that are declared in that interface.

If that particular interface extends from other interfaces, then the implementing class must also implement the methods in the interfaces that are being extended or derived from.

As shown in the example above, if we have a class those implements the FourLegs interface, then that class must define the method headings in both the 'FourLegs' interface and the 'Body' interface.

13.

Can An Interface Be Extended By Another Interface In Java?

Answer»

An INTERFACE can be EXTENDED by ANOTHER interface in Java. 

The CODE for the same would be like as shown below: 

// this interface extends from the Body interface:

PUBLIC interface FourLegs extends Body 

{

public void walkWithFourLegs( );

}

An interface can be extended by another interface in Java. 

The code for the same would be like as shown below: 

// this interface extends from the Body interface:

public interface FourLegs extends Body 

{

public void walkWithFourLegs( );

}

14.

When Is An Abstract Method Used?

Answer»

An abstract METHOD is declared in the PARENT class when we want a class which contains a particular method but on the other hand we want its IMPLEMENTATION to be determined by CHILD class.

An abstract method is declared in the parent class when we want a class which contains a particular method but on the other hand we want its implementation to be determined by child class.

15.

What Is An Abstract Class?

Answer»

These classes cannot be instantiated and are either PARTIALLY IMPLEMENTED or not at all implemented.

This CLASS contains one or more abstract methods which are simply method declarations WITHOUT a body.

These classes cannot be instantiated and are either partially implemented or not at all implemented.

This class contains one or more abstract methods which are simply method declarations without a body.

16.

What Is Interface In Java?

Answer»

An INTERFACE in java is a blueprint of a CLASS that has static CONSTANTS and abstract method by DEFAULT.

An interface in java is a blueprint of a class that has static constants and abstract method by default.

17.

Can We Declare Abstract Method In Non-abstract Class?

Answer»

No, we can't declare abstract method in non-abstract class.

For example:

class DEMO

{

abstract void show();

}

Above example will THROW COMPILE time ERROR.

No, we can't declare abstract method in non-abstract class.

For example:

class Demo

{

abstract void show();

}

Above example will throw compile time error.

18.

Can We Define Abstract Class Without Abstract Method?

Answer»

Yes, we can define ABSTRACT CLASS WITHOUT abstract METHOD.

Yes, we can define abstract class without abstract method.

19.

Can We Create Instance Of Abstract Class?

Answer»

No, we cannot CREATE an INSTANCE of an ABSTRACT CLASS.

No, we cannot create an instance of an abstract class.

20.

What Is Abstract Class In Java?

Answer»

When we declared any CLASS with "ABSTRACT" KEYWORD is known as abstract class. In abstract class we can keep abstract method (without body) and non-abstract method (with body).

For example:

abstract class DEMO

{

abstract VOID show();//abstract method

void show(){}//non-abstract method

}

When we declared any class with "abstract" keyword is known as abstract class. In abstract class we can keep abstract method (without body) and non-abstract method (with body).

For example:

abstract class Demo

{

abstract void show();//abstract method

void show(){}//non-abstract method

}

21.

How To Achieve Abstraction In Java?

Answer»

There are two WAYS in JAVA we can ACHIEVE ABSTRACTION:

  • By USING abstract class (0 to 100%).
  • By using interface (100%).

There are two ways in java we can achieve abstraction:

22.

What Is Abstraction In Java?

Answer»

In java, Abstraction means SHOW FUNCTIONALITY and HIDE COMPLEXITY or internal details or hide implementation details to the user is known as abstraction in java.

For example:

The best example in the world of abstraction is ATM machine where we can WITHDRAW or transfer money easily but how it happens. We don't know. We don't know internal details or implementation details.

In java, Abstraction means show functionality and hide complexity or internal details or hide implementation details to the user is known as abstraction in java.

For example:

The best example in the world of abstraction is ATM machine where we can withdraw or transfer money easily but how it happens. We don't know. We don't know internal details or implementation details.