Top 21 Interview Questions for Java Developers You Totally Should Know
Tags: beginners, java, coding, learning
Published: 2019-02-19 13:45:23

---
title: Top 21 Interview Questions for Java Developers You Totally Should Know
published: true
description: There are myriads of Java interview questions and we can’t collect all of them in one article. However here you can find some very common Java interview questions, according to HR-managers of IT companies.
tags: beginners, java, coding, learning
---
<img src="https://thepracticaldev.s3.amazonaws.com/i/gjlerrkp99rfd3ksq9rw.jpeg" data-src="https://thepracticaldev.s3.amazonaws.com/i/gjlerrkp99rfd3ksq9rw.jpeg" />

There are myriads of Java interview questions and we can’t collect all of them in one article. However here you can find some very common Java interview questions, according to HR-managers of IT companies.

<h3>1. “Disassemble” <code class="markup--code markup--p-code"><strong class="markup--strong markup--p-strong">public static void main(String args[])</strong></code>.</h3>

One of the popular java interview questions for freshers, and easy one.
<ul>
<li><code>public</code> is an access modifier. We use it to specify access to this method. Here modifier is “public”, so any Class has access to this method.</li>
<li><code>static.</code> This Java keyword means that we use this method without creating a new Object of a Class.</li>
<li><code>Void</code> is the return type of the method. It means that the method doesn’t return any value.</li>
<li><code>main</code> is the name of the method. JVM “knows” it as an entry point to an application (it should have a particular signature). Main is a method where the main execution occurs.</li>
<li><code>String args[].</code> This is the parameter passed to the main method. Here we have the arguments of String type that your Java application accepts when you run it. You can type them on the terminal.</li>
</ul>
<h3>2. What is the difference between <code>equals()</code> and <code>==</code> ?</h3>

First, “<code>==</code>” is an operator whereas <code>equals()</code> is a method. We use <code>==</code> operator for reference comparison (or address comparison) and <code>equals()</code> method for content comparison. It means that <code>==</code> checks if both objects point to the same memory location while <code>equals()</code> compares values in the objects.

<h3>3. Can we execute a program without <code>main()</code> method?</h3>

Many java basic interview questions are really easy. Like this one. So short answer is: yes, we can. For example, we can do it using a static block.

You can use a static block to initialize the static data member. It is executed before the <code>main</code> method, at the time of class loading.
```
class Example{
 Static{ 
System.out.println("static block is invoked"); 
} 
  public static void main(String args[]){ 
   System.out.println("Now main method"); 
  } 
}
```
<b>The output is:</b>
```
**static** block is invoked 
Now main method
```
What about total main method absence? If you try to run an ordinary class without the main method at all, you got the next error: 

Main method not found in class Test, please define the main method as: `public static void main (String [] args)` or a JavaFX application class must extend `javafx.application.Application`. 
The error itself says that if this is a JavaFX application and the class is inherited from `javafx.application.Application`, then it is possible.

<h3>4. What is <code>immutable</code> object? Can you create <code>immutable</code> object?</h3>

You can’t modify objects of an <code>immutable</code> class after their creation. So once you create them, you can’t change them. If you try to modify <code>Immutable</code> object you get a new object (clone) and change this clone while creation.

A good example is <code>String</code>, it is <code>immutable</code> in Java. That means you cannot change the object itself, but you can change the reference to the object.
	
<h3>5. How many objects are created in the following code?</h3>

One of the Java technical interview questions that substitutes #4.

```
String s1="Hello"; 
String s2="Hello"; 
String s3="Hello";

```

The answer is “only one” because Java has a String Pool. When we create a String object using the `new()` operator, it creates a new object in heap memory. If we use String literal syntax, like in our example, it may return an existing object from the String pool, if it already exists.

<h3>6. How many objects are created in the following code?</h3>

```
String s = new String("Hello");
```

There are 2 objects. One is in string constant pool and the other in heap.

<h3>7. What is the difference between <code>String</code>, <code>StringBuilder</code> and <code>StringBuffer</code> classes in Java?</h3>

There is one of the leaders in top java interview questions.

First of all <code>String</code> is an Immutable class. That means you can’t modify its content once created. While <code>StringBuffer</code> and <code>StringBuilder</code> are mutable classes, so you can change them later. If we change the content of <code>String</code> object, it creates a new string, therefore, it doesn’t modify the original one. That’s why the performance with <code>StringBuffer</code> is better than with <code>String</code>.

The main difference between <code>StringBuffer</code> and <code>StringBuilder</code> that <code>StringBuffer</code>’s methods are synchronized while <code>StringBuilder</code>’s are not.

<h3>8. Is there any difference in <code>String</code> that was created using literal and with <code>new()</code> operator?</h3>

It is. If we create a string with <code>new()</code> operator it appears in heap and not added to string pool. If you create <code>String</code> using literal it is created in <code>String</code> pool which exists in Perm area of heap.

<h3>9. Can you override <code>private</code> or <code>static</code> method in Java?</h3>

One of java tricky interview questions for rookies. You really can’t override <code>private</code> or <code>static</code> method in Java.

You can’t override the <code>private</code> methods because the scope of the private access specifier is only within the class. When you are going to override something, we should have a parent and child class. If the method of superclass is <code>private</code>, child class can’t use it, and the methods in child class will be treated as new methods (not overridden).

<code>Static</code> methods also cannot be overridden, because <code>static</code> methods are the part of the Class itself, and not a part of any object of the class. Sure you can declare the same <code>static</code> method with the same signature in child classes, but again, they will be treated as new methods.

<h3>10. Difference between <code>Abstract Class</code> and <code>Interface</code></h3>

One of the popular java developer interview questions that bears on OOP principles. First of all, in Java <code>interface</code> defines behavior and <code>abstract class</code> creates a hierarchy.

<img src="https://thepracticaldev.s3.amazonaws.com/i/vdix55tmwjjg62juyvui.png" data-src="https://thepracticaldev.s3.amazonaws.com/i/vdix55tmwjjg62juyvui.png" />


<h3>11. Can we declare <code>static</code> variables and methods in an <code>abstract</code> class?</h3>

Yes, it is possible to declare <code>static</code> variables and methods in <code>abstract</code> method. There is no requirement to make an object to access the static context. So we are allowed to access the static context declared inside the <code>abstract</code> class by using the name of the <code>abstract</code> class.

<h3>12. What types of memory areas are allocated by JVM?</h3>

<B>Class Area</b> stores perclass structures, for example, runtime constant pool, fields, method datas, and all the code for methods.

<b>Heap</b> is a runtime data area where memory is allocated to the objects.

<b>Stack</b> stores frames. It contains local variables and partial results, and takes part in method invocation and return. Every thread has a private JVM stack, created at the same time as the thread. A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes.

<b>Program Counter Register</b> contains an address of the Java virtual machine instruction currently being executed.

<b>Native Method Stack</b> contains all the native methods that are used in the application.
	
<h3>13. Why is multiple inheritance isn’t allowed in java?</h3>

It would be really complicated. Imagine three classes <code>A</code>, <code>B</code>, and <code>C</code> and <code>C</code> inherits <code>A</code> and <code>B</code>. Now, <code>A</code> and <code>B</code> classes have the same method and you call it from a child class object... Which one? <code>A</code>’s or <code>B</code>’s? Here we have ambiguity. If you try to inherit two classes Java renders compile time error.

<h3>14. Can we overload the <code>main()</code> method?</h3>

Sure, we are allowed to have many main methods in a Java program by using method overloading. Try it out!

<h3>15. Can we declare a constructor as <code>final</code>?</h3>

Nope. A constructor can’t be declared as a <code>final</code> because it can’t be inherited. So it is senseless to declare constructors as <code>final</code>. However, if you try to do it, Java compiler throws you an error.

<h3>16. Can we declare an interface as <code>final</code>?</h3>

No, we can’t do this. An interface can’t be <code>final</code> because the interface should be implemented by some class according to its definition. Therefore, there is no sense to make an interface <code>final</code>. However, if you try to do so, the compiler will show an error.

<h3>17. What is the difference between <code>static binding</code> and <code>dynamic binding</code>?</h3>

The <code>binding</code> which can be resolved at compile time by a compiler is called <code>static</code> or early binding. <code>Binding</code> of all the <code>static</code>, <code>private</code> and <code>final</code> methods is done at compile-time.

In <code>Dynamic binding</code> compiler can’t choose a method to be called. Overriding is a perfect example of <code>dynamic binding</code>. In overriding both parent and child classes have the same method.

```
Static Binding 
class Cat{ 
 private void talk() 
{System.out.println("cat is mewing..."); 
} 
</code>
<code> public static void main(String args[]){ 
  Cat cat=new Cat(); 
  cat.talk(); 
 } 
} </code>
<code>Dynamic Binding 
class Animal{ 
 void talk(){ 
System.out.println("animal is talking..."); 
} 
} </code>
<code>class Cat extends Animal{ 
 void talk(){ 
System.out.println("cat is talking...");
} </code>
<code> public static void main(String args[]){ 
  Animal animal=new Cat(); 
  animal.talk(); 
 } 
}
```
<h3>18. How to create a read-only class in Java?</h3>

You can do it by making all of the class’ fields private. The read-only class has only getter methods which return the private property of the class to the <code>main</code> method. You are not able to modify this property, the reason is the lack of setters method.

```
public class HockeyPlayer{ 
private String team ="Maple leaf"; 
public String getTeam(){ 
return team; 
} 
}
```
<h3>19. How to create a write-only class in Java?</h3>

Again, you should make all of the class’ fields <code>private</code>. Now, your write-only class should have only setter methods and no getters. Therefore, we can’t read the properties of the class.

```
public class HockeyPlayer{ 
private String team; 
public void setTeam(String college){ 
this.team = team; 
} 
}
```
<h3>20. Each <code>try</code> block must be followed by a <code>catch</code> block, mustn’t it?</h3>

Nope. It is not a necessity. Each-try block can be without a catch block. It could be followed by either a `catchblock` or a finally block or even without them at all. 

```
public class Main{ 
     public static void main(String []args){ 
        try{ 
            int variable = 1; 
            System.out.println(variable/0); 
        } 
        finally 
        { 
            System.out.println("the other part of the program..."); 
        } 
     } 
}
```
<b>Output:</b>

```
class Main {
        public static void main(String[] args) throws IOException {
            try(InputStreamReader inputStreamReader = new InputStreamReader(System.in);
                BufferedReader reader = new BufferedReader(inputStreamReader)){
                System.out.println("test");
            }
        }
    }
```
P.S.: Before Java 8 methods in interfaces could have been abstract only. In Java 8 or newer, it became possible to define default methods and implement them directly in the interface.
<h3>21. What is the difference between <code>throw</code> and <code>throws</code> keywords?</h3>

<code>Throws</code> is used to declare an exception, so it works similar to the <code>try-catch</code> block. <code>Throw</code> keyword is used to throw an exception explicitly from a method or any other block of code.

<code>Throw</code> is followed by an instance of <code>Exception</code> class and throws is followed by exception class names.

<code>Throw</code> is used in the method body to throw an exception. <code>Throws</code> is used in a method signature to declare the exceptions that may occur in the statements present in the method.

It is allowed to throw one exception at a time but you can handle multiple exceptions by declaring them using <code>throw</code>keyword. You can declare multiple exceptions, e.g., <code>public void method()throws IOException</code>, <code>SQLException</code>.

Here we had only a few java interview questions and answers. This article is the first of interview series. The next one (coming soon) is about data structure questions.

First was published on <a href="https://codegym.cc/groups/posts/78-top-21-java-interview-questions">CodeGym blog</a>.