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
Example
This is done Using
java.util.Randomclass:
import java.util.Random;//100 is the maximum and the 1 is our minimum.
Random rand = new Random();
int n = rand.nextInt(100) + 1;.
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.
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
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.
Java.lang.Throwable
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.
Java Programming language was created by James Gosling at sun micro systems in 1991.
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.
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.
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
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.
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.
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.
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.
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.
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..
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.
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.
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..
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.
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.
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..
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)
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.
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..
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.
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