Java Interview Questions For Experienced With Answers

Java Interview Questions For Experienced With Answers

“Java Interview Questions For Experienced With Answers”

Below you will get JAVA Interview questions for experienced people with answers. This page will help you in preparation for the Interview. On this page, you will get to know the commonly asked questions in JAVA Programming.

Java is important for both freshers and Experienced. There’s a lot to study in Java and one will must get confused where to start and how much will be sufficient to clear placements. Don’t worry PrepInsta is here to help you out.

This page contains commonly asked Java Interview Questions For Experienced With Answers.

java interview questions for experienced with answers

More About Java

Java is an object-oriented, high-level, general-purpose programming language originally designed by James Gosling and further developed by the Oracle Corporation. It is one of the most popular programming languages in the world.

Features of JAVA:-

  • High Performance– Using a JIT (Just-In-Time) compiler allows high performance in Java. The JIT compiler converts the Java bytecode into machine language code, which then gets executed by the JVM
  • Multi-threading– A thread is a flow of execution. The JVM creates a thread which is called the main thread. Java allows the creation of several threads using either extending the thread class or implementing the Runnable interface
  • OOPS Concepts– Java follows various OOPS concepts, namely abstraction, encapsulation, inheritance, object-oriented, and polymorphism
  • Platform Independency– Java makes use of the Java Virtual Machine or JVM which allows a single Java program to operate on multiple platforms without any modifications

Commonly Asked java interview questions for experienced with answers

 1. What is the difference between JVM and JRE?

Solution:- Java Runtime Environment (JRE) is the implementation of JVM. JRE consists of JVM and java binaries and other classes to execute any program successfully. JRE doesn’t contain any development tools like java compiler, debugger, etc. If you want to execute any java program, you should have JRE installed.

2. Can we have multiple public classes in a java source file?

Solution:- We can’t have more than one public class in a single java source file. A single source file can have multiple classes that are not public.

3.What are access modifiers?

Solution:- Java provides access control through public, private and protected access modifier keywords. When none of these are used, it’s called default access modifier.
A java class can only have public or default access modifier. Read Java Access Modifiers to learn more about these in detail.

4.What is multi-catch block in java?


Java 7 one of the improvement was a multi-catch block where we can catch multiple exceptions in a single catch block. This makes our code shorter and cleaner when every catch block has a similar code.

If a catch block handles multiple exceptions, you can separate them using a pipe (|) and in this case, the exception parameter (ex) is final, so you can’t change it.

5. What do you know about Interface in Java?


A Java interface is a template that has only method declarations and not method implementations. It is a workaround for achieving multiple inheritances in Java. Some worth remembering important points regarding Java interfaces are:

  • A class that implements the interface must provide an implementation for all methods declared in the interface
  • All methods in an interface are internally public abstract void
  • All variables in an interface are internally public static final
  • Classes do not extend but implement interfaces

6. How is an Abstract class different from an Interface?


There are several differences between an Abstract class and an Interface in Java, summed up as follows:

  • Constituents – An abstract class contains instance variables, whereas an interface can contain only constants.
  • Constructor and Instantiation – While an interface has neither a constructor nor it can be instantiated, an abstract class can have a default constructor that is called whenever the concrete subclass is instantiated.
  • Implementation of Methods – All classes that implement the interface need to provide an implementation for all the methods contained by it. A class that extends the abstract class, however, doesn’t require implementing all the methods contained in it. Only abstract methods need to be implemented in the concrete subclass.
  • Type of Methods – Any abstract class has both abstract as well as non-abstract methods. Interface, on the other hand, has only a single abstract method.

7. Why do we use the yield() method?

Solution:- The yield() method belongs to the thread class. It transfers the currently running thread to a runnable state and also allows the other threads to execute. In other words, it gives equal priority threads a chance to run. Because yield() is a static method, it does not release any lock.

8. Can you explain the thread lifecycle in Java?


The thread lifecycle has the following states and follows the following order:

  • New – In the very first state of the thread lifecycle, the thread instance is created, and the start() method is yet to be invoked. The thread is considered alive now.
  • Runnable – After invoking the start() method, but before invoking the run() method, a thread is in the runnable state. A thread can also return to the runnable state from waiting or sleeping state.
  • Running – The thread enters the running state after the run() method is invoked. This is when the thread begins execution.
  • Non-Runnable – Although the thread is alive, it is not able to run. Typically, it returns to the runnable state after some time.
  • Terminated – The thread enters the terminated state once the run() method completes its execution. It is not alive now.

9. How do you make a thread stop in Java?


There are three methods in Java to stop the execution of a thread:

  • Blocking – This method is used to put the thread in a blocked state. The execution resumes as soon as the condition of the blocking is met. For instance, the ServerSocket.accept() is a blocking method that listens for incoming socket connection and resumes the blocked thread only when a connection is made.
  • Sleeping – This method is used for delaying the execution of the thread for some time. A thread upon which the sleep() method is used is said to enter the sleep state. It enters the runnable state as soon as it wakes up i.e., the sleep state is finished. The time for which the thread needs to enter the sleep state is mentioned inside the braces of the sleep() method. It is a static method.
  • Waiting – Although it can be called on any Java object, the wait() method can only be called from a synchronized block.

10. How does the throw keyword differ from the throws keyword?

Solution:- While the throws keyword allows declaring an exception, the throw keyword is used to explicitly throw an exception. Checked exceptions can’t be propagated with throw only, but throws allow doing so without the need for anything else. The throws keyword is followed by a class, whereas the throw keyword is followed by an instance. The throw keyword is used within the method, but the throws keyword is used with the method signature. Furthermore, it is not possible to throw multiple exceptions, but it is possible to declare multiple exceptions.

11.What do you mean by Collections in Java? What are the constituents of Collections in Java?


A group of objects in Java is known as collections. Java.util package contains, along with date and time facilities, internationalization, legacy collection classes, etc., the various classes and interfaces for collecting. Alternatively, collections can be considered as a framework designed for storing the objects and manipulating the design in which the objects are stored. You can use collections to perform the following operations on objects:

  • Deletion
  • Insertion
  • Manipulation
  • Searching
  • Sorting

Following are the various constituents of the collections framework:

  • Classes – Array List, Linked List, Lists, and Vector
  • Interfaces – Collection, List, Map, Queue, Set, Sorted Map, and Sorted Set
  • Maps – HashMap, HashTable, LinkedHashMap, and TreeMap
  • Queues – Priority Queue
  • Sets – Hash Set, Linked Hash Set, and Tree Set

12. What do you mean by Priority Queue in Java?

Solution:-Priority queue, like a regular queue, is an abstract data type except having a priority associated with each element contained by it. The element with the high priority is served before the element with low priority in a priority queue. Elements in a priority queue are ordered either according to the comparator or naturally. The order of the elements in a priority queue represents their relative priority.

13.What do you understand by Synchronization in Java? What is its most significant disadvantage?

Solution:-If several threads try to access a single block of code, then there is an increased chance of producing inaccurate results. Synchronization is used to prevent this. Using the synchronization keyword makes a thread need a key to access the synchronized code. Simply, synchronization allows only one thread to access a block of code at a time. Each Java object has a lock, and every lock has only one key. A thread can access a synchronized method only if it can get the key to the lock of the object. Following example demonstrates synchronization:

public class ExampleThread implements Runnable {
public static void main (String[] args){
Thread t = new Thread();
public void run(){

14.Could you demonstrate how to delete a cookie in JSP with a code example?


Following code demonstrates deleting a cookie in JSP:

Cookie mycook = new Cookie("name1","value1");
Cookie killmycook = new Cookie("mycook1","value1");
killmycook . set MaxAge ( 0 );
killmycook . set Path ("/");
killmycook . addCookie ( killmycook 1 );

15. Write a Program for string reversal without using inbuilt function.

public class Reversal
public static void main(String args[])
String input = "Java Interview";
System.out.println("Given String -> " + "Java Interview");
char charArray[] = input.toCharArray();
System.out.println("Reversed String -> ");
for(int i = charArray.length-1;i>=0; i--)

16. Write a Program to delete duplicate from an array.


import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
class RemoveDuplicates
public static void main(String args[])
/*create ArrayList with duplicate elements*/
ArrayList duplicate = new ArrayList();
System.out.println("Given array: "+ duplicate);
Set withoutDuplicates = new LinkedHashSet(duplicate)
System.out.println("Array without duplicates: "+ duplicate);

17. Can we modify the throws clause of the superclass method while overriding it in the subclass?


Yes, we can modify the throws clause of the superclass method while overriding it in the subclass. However, there are some rules which are to be followed while overriding in case of exception handling.

  • If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception, but it can declare the unchecked exception.
  • If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.

18. Can we change the scope of the overridden method in the subclass?


Yes, we can change the scope of the overridden method in the subclass. However, we must notice that we cannot decrease the accessibility of the method. The following point must be taken care of while changing the accessibility of the method.

  • The private can be changed to protected, public, or default.
  • The protected can be changed to public or default.
  • The default can be changed to public.
  • The public will always remain public.

19. What is aggregation?


Aggregation can be defined as the relationship between two classes where the aggregate class contains a reference to the class it owns. Aggregation is best described as a has-a relationship. For example, The aggregate class Employee having various fields such as age, name, and salary also contains an object of Address class having various fields such as Address-Line 1, City, State, and pin-code. In other words, we can say that Employee (class) has an object of Address class.

20. Why is Inheritance used in Java?


There are various advantages of using inheritance in Java that is given below.

  • Inheritance provides code reusability. The derived class does not need to redefine the method of base class unless it needs to provide the specific implementation of the method.
  • Runtime polymorphism cannot be achieved without using inheritance.
  • We can simulate the inheritance of classes with the real-time objects which makes OOPs more realistic.
  • Inheritance provides data hiding. The base class can hide some data from the derived class by making it private.
  • Method overriding cannot be achieved without inheritance. By method overriding, we can give a specific implementation of some basic method contained by the base class.

21. What are the advantages of passing this into a method instead of the current class object itself?


As we know, that this refers to the current class object, therefore, it must be similar to the current class object. However, there can be two main advantages of passing this into a method instead of the current class object.

  • this is a final variable. Therefore, this cannot be assigned to any new value whereas the current class object might not be final and can be changed.
  • this can be used in the synchronized block.