Tricky Java Interview Questions

By | July 26, 2020

Introduction

In this article, I have combined all-important and tricky questions you will definitely be asked in a Java interview. 

Do we create the object of Abstract class in Java

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

Why is it not considered good practice for creating an object of Abstract class?

Since an abstract class can have abstract methods, and we all know abstract methods do not have any logic or implementation or any business logic.

So when we create an object of this class, then with the help of that object we can call the methods written in the class of that Object.

But the method does not have logic or definition, so once it gets called it will not perform any operation, So there is no use of that. Hence we can’t create the Object of Abstract Class.

Explain the final keyword in java?

There are three places where we use the final keyword in java.

  1. Along with class
  2. With Data members
  3. With Methods

final keyword with class

To prevent the class from getting inherit by the other class, we declare the class with the final keyword.

Hence the class which declares with the final keyword does not have any derived or sub or child class.

Some of the predefined final class in java

  • String
  • System
  • Integer
  • Scanner
  • Double and many more.

final keyword with data members

Once any variable or data member declared with the final keyword, then the value assigned to it will never be going to change in the further line of code. It will always be the same throughout the program.

final keyword with methods

A method declared with the final keyword cannot be overridden but can be inherited.

How to prevent the methods from getting overridden in the child or subclass?

Declare the method with the final keyword in a superclass or parent class, so that when any other class inherits the superclass, the method present inside that will not get overridden in the child class.

How to prevent the class to get inherit by the other class?

Declare class with the final keyword.

Explain the difference between final, finally and finalize() in Java?

  1. final: It is a keyword in java with use to declare with class, methods, and variable. As I explain in the above paragraph with detailed uses of the final keyword.
  2. finally: It is a block in java which used with a try-catch block, the code written in finally block will always be get executed even any exception occurred in the code written above the finally block.
  3. finalize(): It is a predefined method in java, Garbage collector always calls this method to perform the cleanup activity before deleting or removing the object which is eligible for Garbage Collection.

Clean-up activity refers to closing all associated resources such as database connection, network connection, etc which is linked with that object.

Note: Please refer to this link

Explain the Scanner class in Java?

Scanner class in java is a predefined class present in java.util package. It used to read or take the input from the end-user or from the keyword.

It contains the many predefined methods which are responsible for taking the input from the keyboard.

dataTypeMethodDescription
Stringnext()It takes the String type data from the end-user until space.
StringnextLine()It is used to take a complete line or paragraph from the end-user.
bytenextByte()Takes the byte datatype value.
shortnextShort()Used to take the short datatype value from keyboard or end-user.
intnextInt()Takes the int datatype value.
doublenextDouble()Takes the double datatype value.
floatnextFloat()It is used to input the float datatype value.
booleanhasNextLine()Used to check the other line is there in the input.  then it will return true if it is present otherwise false.
booleanhasNextInt()It will return true if another line having int value.
booleanhasNextFloat()It will return true if another line having float value.

What is the need to interface when we already have an abstract class for declaring the abstract methods?

There are two very important reasons:

  1.  Multiple Inheritance is not possible in Class. 
  2. Performance-wise Interface is a better option in some cases.

Multiple Inheritance is not possible in Class. 

Consider one scenario, you have written all abstract methods in Class A, and you created two more classes that are Class B and Class C, which contains all business logic.

Now if you already inherit the Class A in Class B and now your requirement is to inherit Class C also in Class B, but multiple inheritances are not possible in Class in java, so you lose the opportunity to inherit Class C and their business logic and it will surely impact on your Application.

Now look into other side suppose instead of Class A, you created one interface which contains all the abstract methods, now if you inherit the interface in Class B, then you still have an opportunity to inherit the Class C.

Because you are only inheriting one class that is Class C in Class B, hence there are no multiple inheritances concepts that come into the picture. So, in this case Interface is a better choice.

Performance-wise Interface is a better option in some cases.

In Class, we have a constructor, so when you inherit or extends some other class, so while executing the code, subclass constructor will call the superclass constructor, because of that it takes little more time for executing the entire code.

But if I talk about interface suppose one class inherits the interface then, while execution of entire the constructor of subclass will never class the superclass constructor because here superclass is an interface and we all know interface does not have any constructor. Hence, with Interface our code execution will be a little faster.

Conclusion

In this article, you learned the top most frequently and a tricky question asked in any of the Java Interviews.

Thank You so much for reading my article. If you have doubts or queries, please don’t hesitate, do ask in the comment section below, I will love to answer your queries as soon as possible. You can also refer to my other articles.

Leave a Reply

Your email address will not be published. Required fields are marked *