Java interview questions and Answers

Last update: 29 Jan 2022, 49 Questions

Java is a computing platform as well as a programming language. You can download it for free. An object-oriented language, Java is a fast, secure and easily extendible language. Java is said to be more dynamic than C or C++ language because of its adaptable design. Unlike many other programming languages, Java is a platform-independent and Architecture-neutral language. Our massive collection of java interview questions will definitely help you find a great job.

Most Frequently Asked Java interview questions

Here in this article, we will be listing frequently asked Java interview questions and Answers with the belief that they will be helpful for you to gain higher marks. Also, to let you know that this article has been written under the guidance of industry professionals and covered all the current competencies.

1. Explain the features of Java?
  • Simple and easy to learn
  • Platform Independent
  • Supports is Object Oriented Programming language
  • Robust and multithreaded
  • Enables high performance
  • Java Interpreted
  • Secure and its interpreter
  • Follows WORA (write once, run anywhere) approach
3 0
2. How we can generate random numbers in Java?

In Java, there are three ways to generate random numbers:

  • java.util.Random class
  • Math.random method
  • ThreadLocalRandom class



This is done Using java.util.Random class:
import java.util.Random;
Random rand = new Random();
int n = rand.nextInt(100) + 1;
//100 is the maximum and the 1 is our minimum.

1 0
3. What is the difference between Array and Array List in Java?
  Array ArrayList
1. Basic functionality Part of collection framework.
2. Fixed sized Dynamic sized
3. Contain primitive data types as well as objects of a class Only supports object entries
0 0
5. What is Socket in Java?

It is one endpoint of two-way communication between programs that are running on the same network.

A socket is tied to a port number in such a manner that the TCP layer can recognize the application where the data needs to be sent. If you use Socket instead of native code, your programs will communicate platform-independently.

1 0
6. What is Network Programming in java?

It refers to writing programs or applications that run across multiple devices when the devices are connected on a network.

The java.net package contains a collection of interfaces and classes that communicate and allow programmers to write programs that focus on solving the current problem.

0 0
8. How we can make copy of a java object?

For copying an object in Java, there are two ways- shallow copy and deep copy.

  Shallow Copy Deep Copy
1. When you want to copy only field values All the objects are deeply copied
2. Copy is dependant on original object Copy is not dependent on earlier objects
1 0
9. What is the difference between Stack and Queue?
  Stack Queue
1. Objects accessed on Last In First Out (LIFO). Objects accessed on First In First Out (FIFO)
2. Object pushed on top of collection Object inserted at the end
3. Object removed from the top Object removed from the beginning.
4. Two operations are called push and pop Two operations are called enqueue and dequeue
0 0
10. Explain different states of a thread in Java?

In Java, there are various states of a thread. But at any point of time, it can exist in any one of these states:

  • New
  • Runnable
  • Blocked
  • Waiting
  • Timed Waiting
  • Terminated
0 0
11. What is anonymous class in Java?

An anonymous class is like a local class or an inner class, but without a name. You can use an anonymous class for declaring and instantiating a class simultaneously.

0 0
13. What is Java and why do we need it? Explain

Java is a computing platform as well as a programming language. An object-oriented language, Java is a fast, secure and easily extendible language. Java is a platform independent and Architecture-neutral language.

It is used to create complete & powerful applications that may run on a single computer or be distributed among clients and servers in a network. I also can be used on the intranet applications.

0 0
14. What is the differences between C++ and Java? Explain
  C++ Java
1. Follows WOCA approach (Write once, compile anywhere). Follows WORA approach (Write once, run anywhere).
2. Runs as code on native machine. Runs on virtual machines.
3. Multiple binary compatibility standards are included. Has single binary compatibility standard.
4. All types of Pointers and pass-by-value are supported. Primitive and reference types are only passed by value.
0 0
15. Who developed Java?

Java Programming language was created by James Gosling at sun micro systems in 1991.

0 0
16. What is Java virtual machine? Explain

It is a virtual machine that enables computers to run Java programs in addition to programs written in other languages but are compiled to Java bytecode. OpenJDK project has developed the Java virtual machine (JVM) reference as open source code. The reference includes a JIT compiler which is known as HotSpot.

0 0
17. Explain the difference between JDK, JRE, and JVM?
  Java Development Kit (JDK) JVM JRE
1. Core component of Java Environment Converts Byte code to machine-specific code Implementation of JVM.
2. Provides tools and executables required for a Java Program Platform dependent Provides a platform for executing Java programs.
3. It is platform specific Provides core functions like garbage collection, memory management and security etc. Consists of java binaries and classes.
0 0
18. What do you mean by classloader?

Class loaders are responsible for dynamically loading the classes during the runtime to the JVM. They are a part of the JRE.

Three types of Class Loaders are built-in - Bootstrap, Extensions and System.

1 0
19. Explain the advantages of Packages in Java?

Following are the advantages:

  • Allow you to define packages for bundling a group of classes or interfaces.
  • No name conflicts with other package names.
  • Packages make access control easier
  • Packages make access control easier
0 0
20. Explain the difference between an object-oriented programming language and object-based programming language?
  Object-Based Languages Object-Oriented Languages
1. Does not support inheritance or polymorphism. Supports polymorphism and inheritance
2. Does not support built-in objects. Support built-in objects.
3. Examples - Javascript and VB Examples - C# and Java
0 0
22. What do you mean by Local variable and Instance variable?
  Local Variables Instance Variables
1. Declared in constructor, method or block. Declared in a class
2. Not possible to use access modifiers Access modifiers can be given
3. Visible within the declared method Visible for all methods
4. No default value Have default values
0 0
23. What is an Class?

It is a prototype or user-defined blueprint that is used to create objects or data types. A class represents properties or methods that are common to objects of one type. All class objects possess the class properties.

0 0
24. What is an Object?

It is the core unit of Object-Oriented Programming and represents real entities. As we know, a Java program creates objects that interact by invoking various methods.

An object consists of state, behavior, and identity.

0 0
25. What is the constructor and how many types of constructors are used in Java?

The constructor is a method used to initialize an object. A normal java method has return type but the constructor does not have any explicit return type. It is called during the object creation time.

There is two type of Constructors - Default or no-arg Constructor, Parameterized Constructor.

0 0
26. What is "this" keyword in java? Explain

It is a reference variable that refers to the current object. It can be used as follows:

  • To refer instance variable of class
  • To invoke class constructor
  • Passed as an argument in method call
  • Passed as an argument in the constructor call
  • Used to return a class instance
0 0
27. Explain the Inheritance?

It is the process when one class acquires the properties or methods or fields of another class. This process is used to arrange and manage information in hierarchical order. The class that inherits the properties is called subclass. The class whose properties get inherited is called superclass.

0 0
28. What is Polymorphism in Java?

It is one of the features of object-oriented programming that allows the developers to perform one action in different ways. Developers often use Polymorphism in referencing a parent class to a child class object.

0 0
29. What is Method Overriding?

Method overriding is a feature in object-oriented programming that allows a child class to give a specific implementation method that is already given by its parent classes.

0 0
30. What is Method Overloading?

Method Overloading allows a class to have more than one methods of having the same name, but only if the argument lists are varying. This feature is similar to constructor overloading feature that allows more than one constructor to a class only if their argument lists are different.

0 0
31. What is Interface in Java? Explain

Similar to class, an interface in Java is a reference type. An interface contains abstract methods, constants, static methods, default methods and nested types.

A class implements an interface and inherits the abstract methods of the same interface. Writing an interface is like writing a class.

0 0
32. What is Abstract class? Explain

It is a class that is declared with an abstract keyword. An abstract class can have both abstract and non-abstract methods and needs to be extended. In this class, the method must be implemented and cannot be instantiated.

0 0
33. What is the difference between HashMap and HashTable in Java?
  Hashmap Hashtable
1. Nonsynchronized Synchronized
2. Not thread safe Thread-safe
3. Cannot be shared between threads without synchronization code Can be shared with many threads
4. Allows null key and multiple null values Does not allow null key or value
0 0
34. What is the difference between HashSet and TreeSet in Java?
  HashSet TreeSet
1. Offers constant time cost Offers log time cost
2. Does not maintain the order of elements By default sorts elements in ascending order.
3. Implemented using a hash table Implemented using Binary Search Tree
0 0
35. What is Collections in Java?

A Collection in Java is a group of individual objects that are represented as a single unit. The Collection Framework in Java defines classes and interfaces that represent a group of objects as a single unit.

The two main root interfaces are Collection interface and Map interface.

0 0
36. Why Java is a platform independent? Explain

By being platform independent, we mean that the java source code can be run on all OS. The presence of a machine independent code called Byte Code, which once compiled, can be run on any platform, makes Java platform independent.

The compiler feature in Java converts or translates the high-level language into a common format understood by all the machines. The only requirement is the availability of JVM.

0 0
37. What is the differences between Heap and Stack Memory in Java? Explain
  Heap space Stack Memory
1. Used to allocate memory Used for execution of the thread
2. Instance variables are created Local variables are created
3. Contains Objects and reference variables Contains methods and local variables
0 0
38. Explain the difference between abstract classes and interfaces in Java?
  Interface Abstract class
1. Can only have abstract methods Can have abstract, non-abstract methods
2. Variables are by default final. May contain non-final variables.
3. Only has static and final variables Can have non-final, final, non-static and static variables
0 0
39. What is servlet? Explain

This is a Java programming class that runs on Java-enabled server or application server and extends the capabilities of servers that host applications.

A servlet possesses all the features of Java such as platform independence, portability, security and database connectivity.

0 0
40. What is JIT compiler in Java?

JIT compiler is a component of JRE that improves the performance of applications.

JIT compiler improve the performance of JVM by compiling bytecode into machine code at runtime. Choosing the right compiler is one of the first and biggest decisions to be made when you are running a Java application.

0 0
41. Define an applet in Java?

Applet is a Java program that gets embedded into web pages using the APPLET or OBJECT tag and runs inside the browser.

It works at the client side, and is hosted on a web server. Applets make websites dynamic and entertaining.

0 0
42. What is finalize() function in Java?

This is a good question concerning core java interview questions for experienced professionals. finalize method is called by the Garbage Collector before performing the clean-up activity. Clean-up activity refers to the process of de-allocating all the resources, such as database connection and network connection, associated with an object.

0 0
43. What is instanceOf operator used in Java?

instanceof operator is used in object reference variables. In this, the operator will check the class type or interface type of an object.

It is written as follows:

( Object reference variable ) instanceof (class/interface type)

0 0
44. How we can make a read-only class in Java?

By Read-only class we are referring to the "IMMUTABLE" concept. You can begin by defining a class so that no methods can cause changes to the internal state.

In such classes, aliasing will have no impact because its internal state is read-only.

0 0
46. How we can create packages in Java?

Here are the steps for creating a package:

  • Choose a name for the package
  • On top of every source file, put the package statement with the package name. Source file must contain classes and types that you want in the package.
  • Package statement is the first line of source file.
  • You can only have one package statement in each file
  • It applies to all file types.
0 0
47. What is Exception Handling in Java?

Exception handling allows developers to handle runtime errors caused by exceptions. One way to handle exceptions is to print a simple message for users that help them correct the error that occurs due to bad data sent by the user.

Exception handling ensures the flow of the application does not break.

0 0
48. Explain the difference between throw and throws in Java?
  Throw Throws
1. Used to throw an exception. Used to declare an exception
2. Followed by Exception class instance Followed by exception class names
3. Used in the method body Used in method signature
0 0
49. What is the purpose of the System class in Java?

System class has useful fields, such as static members, that are related to the environment. This class is provided with standard streams like input, output and error.

These streams can be used to access the properties and environment variables that are externally defined.

0 0

Development History of Java Programming Language

Sun Microsystems released its first version in 1995 as a core component of its platform. Majority of candidates are asked this information in advanced java interview questions.

Latest Version: The latest version is Java SE 11 (18.9 LTS), which was released in September 2018. This information is critical if you are preparing for Java interview questions.

Advantages of Java
  • Simple and easy to learn
  • Robust and multithreaded
  • Enables high performance
  • Follows WORA (write once, run anywhere) approach
About Best Interview Question
Best Interview Question
Technical Consultant

With our 10+ experience in PHP, MySQL, React, Python & more our technical consulting firm has received the privilege of working with top projects, 100 and still counting. Our team of 25+ is skilled in distinct programming languages such as Python, Java, React.js, Angular, Node.js, PHP, HTML, CSS, Designing, iOS and Android apps, Database, .net, QA, Digital Marketing and Govt. jobs, etc. We are helping 10+ companies in solving their technical problems. When not found coding, our technical consultants can be seen helping out others.