10 Tricky Java Interview Questions and How to Answer Them

Raghu Chavva
4 min readJan 7, 2023

--

1.Can you explain the difference between “final,” “finally,” and “finalize”?

“Final” is a keyword in Java that can be used in several contexts. When applied to a variable, it means that the value of the variable cannot be changed once it has been initialized. When applied to a method, it means that the method cannot be overridden by a subclass. Finally, “finalize” is a method in the Object class that is called by the garbage collector just before an object is recycled, to allow the object to clean up any resources it is using.

2.What is the difference between a static and a non-static inner class in Java?

A static inner class is a class that is defined within another class, but has the “static” keyword applied to it. This means that the inner class can be instantiated without an instance of the outer class. A non-static inner class, on the other hand, requires an instance of the outer class in order to be instantiated.

3.Can you explain the difference between an abstract class and an interface in Java?

An abstract class is a class that cannot be instantiated, but can contain both abstract and concrete methods. An interface, on the other hand, is a purely abstract class that can only contain abstract methods. A class can implement multiple interfaces, but can only extend one abstract class.

4.How does the “instanceof” operator work in Java?

The “instanceof” operator is used to determine whether an object is an instance of a particular class or implements a particular interface. It takes the form “object instanceof class” and returns a boolean value indicating whether the object is an instance of the specified class or implements the specified interface.

5.Can you explain the difference between a “deep copy” and a “shallow copy” in Java?

A deep copy creates a new object with a new memory address, and copies the contents of the original object into the new object. A shallow copy, on the other hand, creates a new object with a new memory address, but the contents of the original object are not copied — the new object simply contains references to the original object’s data.

6.How does the Java garbage collector work?

The Java garbage collector is responsible for reclaiming memory that is no longer being used by the program. It does this by periodically scanning the heap (the area of memory where objects are stored) and identifying objects that are no longer reachable from any live thread or static field. These objects are then eligible for garbage collection and can be recycled.

7.Can you explain the difference between a “checked” and an “unchecked” exception in Java?

A checked exception is an exception that is checked at compile-time. This means that code that might throw a checked exception is required to either handle the exception or declare it in a “throws” clause. An unchecked exception, on the other hand, is not checked at compile-time and is not required to be handled or declared.

8.Can you explain the difference between a “break” and a “continue” statement in a loop?

A “break” statement is used to exit a loop early, while a “continue” statement is used to skip the rest of the current iteration of a loop and move on to the next one.

9.How does the “synchronized” keyword work in Java?

The “synchronized” keyword is used to control access to a shared resource in a multithreaded

10.Can you explain the difference between a “constructor” and a “method” in Java?

In Java, a constructor is a special type of method that is used to create and initialize an object. It has the same name as the class in which it is defined and is called when an object of that class is created.

A method, on the other hand, is a block of code that performs a specific task and may or may not return a value. Methods are used to encapsulate behavior and can be called on an object to perform that behavior.

There are a few key differences between constructors and methods in Java:

  1. Constructors do not have a return type, while methods do.
  2. Constructors are called automatically when an object is created, while methods must be called explicitly.
  3. Constructors can be overloaded (meaning there can be multiple constructors with different parameter lists), while methods must have unique signatures (meaning the parameter list must be different from any other method in the same class).
  4. Constructors cannot be overridden, while methods can be.

--

--

Raghu Chavva

Hi there!! I am Raghu Reddy, A software engineer at Accenture.Admin of the Instagram account @Java_quizs - 50k (https://www.instagram.com/java_quizs/).