Java interview questions and Answers

Thumb

Author

BIQ

Questions

49

Last updated

Jan 29, 2022

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.

Q1. What is Java and why do we need it? Explain
Answer

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.

Q2. Explain the features of Java?
Answer
  • 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
Q3. What is the differences between C++ and Java? Explain
Answer
  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.
Q4. Who developed Java?
Answer

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

Q5. What is Java virtual machine? Explain
Answer

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.

Q6. Explain the difference between JDK, JRE, and JVM?
Answer
  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.
Q7. What do you mean by classloader?
Answer

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.

Q8. Explain the advantages of Packages in Java?
Answer

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
Q9. Explain the difference between an object-oriented programming language and object-based programming language?
Answer
  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
Q10. What are the Java IDE's? Explain
Answer
Q11. What do you mean by Local variable and Instance variable?
Answer
  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
Q12. What is an Class?
Answer

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.

Q13. What is an Object?
Answer

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.

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

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.

Q15. What is "this" keyword in java? Explain
Answer

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
Q16. Explain the Inheritance?
Answer

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.

Q17. What is Polymorphism in Java?
Answer

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.

Q18. What is Method Overriding?
Answer

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.

Q19. What is Method Overloading?
Answer

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.

Q20. What is Interface in Java? Explain
Answer

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.

Q21. What is Abstract class? Explain
Answer

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.

Q22. What is the difference between Array and Array List in Java?
Answer
  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
Q23. What is the difference between HashMap and HashTable in Java?
Answer
  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
Q24. What is the difference between HashSet and TreeSet in Java?
Answer
  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
Q25. What is Collections in Java?
Answer

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.

Q26. Why Java is a platform independent? Explain
Answer

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.

Q27. What is the differences between Heap and Stack Memory in Java? Explain
Answer
  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
Q28. Explain the difference between abstract classes and interfaces in Java?
Answer
  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
Q29. What is servlet? Explain
Answer

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.

Q30. What is JIT compiler in Java?
Answer

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.

Q31. Define an applet in Java?
Answer

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.

Q32. What is finalize() function in Java?
Answer

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.

Q33. What is instanceOf operator used in Java?
Answer

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)

Q34. How we can make a read-only class in Java?
Answer

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.

Q35. How we can make a write-only class in Java?
Answer
Q36. How we can create packages in Java?
Answer

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.
Q37. What is Exception Handling in Java?
Answer

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.

Q38. Explain the difference between throw and throws in Java?
Answer
  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
Q39. What is the purpose of the System class in Java?
Answer

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.

Q40. What is the difference between the ">>" and " >>>" operators in Java?
Answer
Q41. What is Socket in Java?
Answer

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.

Q42. What is Network Programming in java?
Answer

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.

Q43. How we can generate random numbers in Java?
Answer

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.

Q44. How we can skip Finally block of exception even if some exception occurs in the exception block in Java?
Answer
System.exit(0);
Q45. How we can make copy of a java object?
Answer

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
Q46. What is the difference between Stack and Queue?
Answer
  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
Q47. Explain different states of a thread in Java?
Answer

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
Q48. What is anonymous class in Java?
Answer

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.

Q49. What is the base class of all exception classes in Java?
Answer
Java.lang.Throwable