Java interview questions and Answers
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
- 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
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.
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 |
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.
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.
System.exit(0);
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 |
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 |
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