Java interview questions and Answers

Last updated on Jan 29, 2022
  • Share
Java interview 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.

Q1. 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
Q2. 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
Example

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.

Q3. 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
Q4. What is the difference between the ">>" and " >>>" operators in Java?
Answer
Q5. 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.

Q6. 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.

Q7. How we can skip Finally block of exception even if some exception occurs in the exception block in Java?
Answer
System.exit(0);
Q8. 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
Q9. 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
Q10. 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

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
Reviewed and verified by Best Interview Question
Best Interview Question

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...