The Java Interview Questions And Answers For 2021

Most asked Java interview questions and answers for freshers in campus placement drive. Here we have categorized most asked 50+ Top Java based interview question and answers.

In every technical campus interview round you may asked questions on Java programming language, We have collected and short answered the Java interview question that you can remember easily.

So the Basic Java interview question as follows.

1. Distinguish  between JDK, JRE, and JVM?


1. It stands for Java Development Kit.

2. It is the tool necessary to compile, document and package Java programs.

3. It contains JRE + development tools.


1. It stands for Java Runtime Environment.

2. JRE refers to a runtime environment in which java bytecode can be executed.

3. It’s an implementation of the JVM which physically exists.


1. It Stands for Java Virtual Machine.

2.It is an abstract machine. It is a specification that provides a run-time environment in which Java bytecode can be executed.

3. JVM follows three notations: Specification, Implementation and Runtime Instance

2. Why is Java not 100% Object-oriented?

Java is not 100% Object-oriented because it makes use of eight primitive data types such as boolean, byte, char, int, float, double, long, short which are not objects.

3.  Why are pointers not used in Java?

Java doesn’t use pointers because they are unsafe and increases the complexity of the program. Since Java is known for its simplicity of code, adding the concept of pointers will be contradicting. Moreover, since JVM is responsible for implicit memory allocation, thus in order to avoid direct access to memory by the user,  pointers are discouraged in Java.

4. Why is Java platform independent?

Java is called platform independent because of its byte codes which can run on any system irrespective of its underlying operating system.

5.  What is the difference between an Inner Class and a Sub-Class?

  Inner class:

1. Is a class which is nested within another class.

2. An Inner class has access rights for the class which is nesting it and it can access all variables and methods defined in the outer class.


1. Is a class which inherits from another class called super class.

2. Subclass can access all public and protected methods and fields of its super class.

6. What is data encapsulation and what’s its significance?

1.  Encapsulation is a concept in Object Oriented Programming for combining properties and methods in a single unit.

2. Encapsulation helps programmers to follow a modular approach for software development as each object has its own set of methods and variables and serves its functions independent of other objects.

3. Encapsulation also serves data hiding purposes.

7. What is the difference between double and float variables in Java?

1. Float takes 4 bytes in memory while Double takes 8 bytes in memory.

2. Float is single precision floating point decimal number while Double is double precision decimal number

8. How can you generate random numbers in Java?

1.Using Math.random() you can generate random numbers in the range greater than or equal to 0.1 and less than 1.0

2. Using Random class in package java.util

9. Can the main() method in Java return any data?

 In java, the main() method can’t return any data and hence, it’s always declared with a void return type.

10. Can we declare a class as Abstract without having any abstract method?

Yes we can create an abstract class by using an abstract keyword before the class name even if it doesn’t have any abstract method. However, if a class has even one abstract method, it must be declared as abstract otherwise it will give an error.

11. How can we pass arguments to a function by reference instead of pass by value?

 In java, we can pass arguments to a function only by value and not by reference.

12. Can a class have multiple constructors?

Yes, a class can have multiple constructors with different parameters. Which constructor gets used for object creation depends on the arguments passed while creating the objects.

13.What’s the difference between an array and Vector?

 An array groups data of the same primitive type and is static in nature while vectors are dynamic in nature and can hold data of different data types.

14. What is multi-threading?

Multi threading is a programming concept to run multiple tasks in a concurrent manner within a single program. Threads share the same process stack and running in parallel. It helps in performance improvement of any program.

15.What’s the purpose of using Break in each case of Switch Statement?

 Break is used after each case (except the last one) in a switch so that code breaks after the valid case and doesn’t flow in the proceeding cases too.

If break isn’t used after each case, all cases after the valid case also get executed resulting in wrong results

16. How objects of a class are created if no constructor is defined in the class?

Even if no explicit constructor is defined in a java class, objects get created successfully as a default constructor is implicitly used for object creation. This constructor has no parameters.

17. Name the superclass in Java.

Java.lang. All different non-primitive are inherited directly or indirectly from this class.

18. Explain the difference between ‘finally’ and ‘finalize’ in Java?

Used with the try-catch block, the ‘finally’ block is used to ensure that a particular piece of code is always executed, even if the execution is thrown by the try-catch block.

In contrast, finalize() is a special method in the object class. It is generally overridden to release system resources when garbage value is collected from the object.

19. Does Java support global variables? Why/Why not?

No, Java doesn’t support global variables. This is primarily because of two reasons:

● They create collisions in the namespace.

● They break the referential transparency.

20. Garbage collector thread is what kind of a thread?

It is a daemon thread.

21. State the methods used during deserialization and serialization processes.


Reads the file and deserializes the object.


Serialize the object and write the serialized object to a file.

22. What are wrapper classes in Java?

All primitive data types in Java have a class associated with them – known as wrapper classes. They’re known as wrapper classes because they ‘wrap’ the primitive data type into an object for the class. In short, they convert Java primitives into objects.

23. What is Java String Pool?

A String Pool in Java is a distinct place that has a pool of strings stored via the Java Heap Memory. Here, String represents a special class in Java, and string objects can be created using either a new operator or using values in double-quotes.

The String is immutable in Java, thus, making feasibility of String pool and then the further implementation via String interning concepts.

24. What is the default size of the load factor in the hashing-based collection?

Default size = 0.75

Default capacity = initial capacity * load factor

25. What is clipping?

Clipping is the process of confining paint operations to a limited area or shape.

26. What is an object in Java and how is it created?

An object is a real-world entity that has a state and behavior. An object has three characteristics:

  • State
  • Behavior
  • Identity

An object is created using the ‘new’ keyword. For example:

ClassName obj = new ClassName();

27. What is the final keyword in Java?

final is a special keyword in Java that is used as a non-access modifier. A final variable can be used in different contexts such as:

  • final variable

When the final keyword is used with a variable then its value can’t be changed once assigned. In case the no value has been assigned to the final variable then using only the class constructor a value can be assigned to it.

  • final method

When a method is declared final then it can’t be overridden by the inheriting class.

  • final class

When a class is declared as final in Java, it can’t be extended by any subclass class but it can extend another class.

28. What is a classloader in Java?

The Java ClassLoader is a subset of JVM (Java Virtual Machine) that is responsible for loading the class files. Whenever a Java program is executed it is first loaded by the classloader. Java provides three built-in classloaders:

  • Bootstrap ClassLoader
  • Extension ClassLoader
  • System/Application ClassLoader

29.  Why are Java Strings immutable in nature?

In Java, string objects are immutable in nature which simply means once the String object is created its state cannot be modified. Whenever you try to update the value of that object instead of updating the values of that particular object, Java creates a new string object. Java String objects are immutable as String objects are generally cached in the String pool. Since String literals are usually shared between multiple clients, action from one client might affect the rest. It enhances security, caching, synchronization, and performance of the application.

30. What is abstraction in Java?

Abstraction refers to the quality of dealing with ideas rather than events. It basically deals with hiding the details and showing the essential things to the user. Thus you can say that abstraction in Java is the process of hiding the implementation details from the user and revealing only the functionality to them. Abstraction can be achieved in two ways:

  • Abstract Classes (0-100% of abstraction can be achieved)
  • Interfaces (100% of abstraction can be achieved)

31. What is method overloading and method overriding?

Method Overloading :

  • In Method Overloading, Methods of the same class share the same name but each method must have a different number of parameters or parameters having different types and order.
  • Method Overloading is to “add” or “extend” more to the method’s behavior.
  • It is a compile-time polymorphism.
  • The methods must have a different signature.
  • It may or may not need inheritance in Method Overloading.

Method Overriding : 

  • In Method Overriding, the subclass has the same method with the same name and exactly the same number and type of parameters and same return type as a superclass.
  • Method Overriding is to “Change” existing behavior of the method.
  • It is a run time polymorphism.
  • The methods must have the same signature.
  • It always requires inheritance in Method Overriding.

32. What are the steps to connect to a database in java?

  • Registering the driver class
  • Creating connection
  • Creating statement
  • Executing queries
  • Closing connection

33. Is it compulsory for a Try Block to be followed by a Catch Block in Java for Exception handling?

 Try block needs to be followed by either Catch block or Finally block or both. Any exception thrown from try block needs to be either caught in the catch block or else any specific tasks to be performed before code abortion are put in the Finally block.

34. Why Runnable Interface is used in Java?

 Runnable interface is used in java for implementing multi threaded applications. Java.Lang.Runnable interface is implemented by a class to support multi threading.

35. In multi-threading how can we ensure that a resource isn’t used by multiple threads simultaneously?

 In multi-threading, access to the resources which are shared among multiple threads can be controlled by using the concept of synchronization. Using synchronized keywords, we can ensure that only one thread can use a shared resource at a time and others can get control of the resource only once it has become free from the other one using it.

36. How can we make a copy of a java object?

 We can use the concept of cloning to create a copy of an object. Using clone, we create copies with the actual state of an object.

Clone() is a method of Cloneable interface and hence, Cloneable interface needs to be implemented for making object copies.

37. Which types of exceptions are caught at compile time?

Checked exceptions can be caught at the time of program compilation. Checked exceptions must be handled by using try catch block in the code in order to successfully compile the code.

38. Can variables be used in Java without initialization?

 In Java, if a variable is used in a code without prior initialization by a valid value, the program doesn’t compile and gives an error as no default value is assigned to variables in Java.

39. Can a dead thread be started again?

 In java, a thread which is in a dead state can’t be started again. There is no way to restart a dead thread.

40. Is JDK required on each machine to run a Java program?

JDK is a development Kit of Java and is required for development only and to run a Java program on a machine, JDK isn’t required. Only JRE is required.

41. Can we have any other return type than void for the main method?

 No, Java class main method can have only void return type for the program to get successfully executed.

Nonetheless , if you absolutely must return a value to at the completion of main method , you can use System.exit(int status)

42. What happens if an exception is not handled in a program?

 If an exception is not handled in a program using try catch blocks, the program gets aborted and no statement executes after the statement which caused exception throwing.

43. String and StringBuffer both represent String objects. Can we compare String and StringBuffer in Java?

 Although String and StringBuffer both represent String objects, we can’t compare them with each other and if we try to compare them, we get an error.

44. What’s the base class of all exception classes?

In Java, Java.lang.Throwable is the super class of all exception classes and all exception classes are derived from this base class.

45. What is the purpose of JDBC ResultSet interface?

The ResultSet object represents a row of a table. It can be used to change the cursor pointer and get the information from the database.

46. What is the difference between execute, executeQuery, executeUpdate?

Statement execute(String query) is used to execute any SQL query and it returns TRUE if the result is a ResultSet such as running Select queries. The output is FALSE when there is no ResultSet object such as running Insert or Update queries. We can use getResultSet() to get the ResultSet and getUpdateCount() method to retrieve the update count.

Statement executeQuery(String query) is used to execute Select queries and returns the ResultSet. ResultSet returned is never null even if there are no records matching the query. When executing select queries we should use executeQuery method so that if someone tries to execute insert/update statement it will throw java.sql.SQLException with message “executeQuery method can not be used for update”.

Statement executeUpdate(String query) is used to execute Insert/Update/Delete (DML) statements or DDL statements that return nothing. The output is int and equals the row count for SQL Data Manipulation Language (DML) statements. For DDL statements, the output is 0.

You should use the execute() method only when you are not sure about the type of statement, else use executeQuery or executeUpdate method.

47. What is encapsulation in Java?

Encapsulation is a mechanism where you bind your data(variables) and code(methods) together as a single unit. Here, the data is hidden from the outer world and can be accessed only via current class methods. This helps in protecting the data from any unnecessary modification. We can achieve encapsulation in Java by:

  • Declaring the variables of a class as private.
  • Providing public setter and getter methods to modify and view the values of the variables.

48. What is meant by Ordered and Sorted in collections?

Ordered: It means the values that are stored in a collection is based on the values that are added to the collection. So we can iterate the values from the collection in a specific order.

Sorted: Sorting mechanisms can be applied internally or externally so that the group of objects sorted in a particular collection is based on the properties of the objects.

49. What is a copy constructor in Java?

Copy constructor is a member function that is used to initialize an object using another object of the same class. Though there is no need for a copy constructor in Java since all objects are passed by reference. Moreover, Java does not even support automatic pass-by-value.

50. What is Request Dispatcher?

RequestDispatcher interface is used to forward the request to another resource that can be HTML, JSP or another servlet in the same application. We can also use this to include the content of another resource to the response.

There are two methods defined in this interface:

1.void forward()

2.void include()

Join Our Telegram Channel For Daily Jobs UpdateJoin Now
#Free Udemy Certification CoursesRegister Free
Join Our WhatsApp Group For Daily UpdatesJoin Now