Java Interview Questions and Answers

Java is known for its versatility, portability, and wide range of applications. It is a high-level programming language that was developed by James Gosling in the year 1982. It is based on the principles of object-oriented programming and can be used to develop large-scale applications.

In this article, we will cover all the popular Core Java interview questions for both freshers and experienced professionals. If there is any core Java interview question that has been asked to you, kindly post it in the comment section. We assure that you will get here the 95% frequently asked interview questions and answers.

1. Is Java Platform Independent?

Yes, Java is a Platform Independent language. Unlike many programming languages javac compiler compiles the program to form a bytecode or .class file. This file is independent of the software or hardware running but needs a JVM(Java Virtual Machine) file preinstalled in the operating system for further execution of the bytecode.

Although JVM is platform dependent, the bytecode can be created on any System and can be executed in any other system despite hardware or software being used which makes Java platform independent.

2. What is JVM?

JVM(Java Virtual Machine) acts as a run-time engine to run Java applications. JVM is the one that actually calls the main method present in a Java code. JVM is a part of JRE(Java Runtime Environment).

Java applications are called WORA (Write Once Run Anywhere). This means a programmer can develop Java code on one system and can expect it to run on any other Java-enabled system without any adjustment. This is all possible because of JVM.

When we compile a .java file, .class files(contains byte-code) with the same class names present in .java file are generated by the Java compiler. This .class file goes into various steps when we run it. These steps together describe the whole JVM.

3. Features of Java?

Simple: Java is easy to learn. The syntax of Java is based on C++ which makes easier to write the program in it.

Object-Oriented: Java follows the object-oriented paradigm which allows us to maintain our code as the combination of different type of objects that incorporates both data and behavior.

Portable: Java supports read-once-write-anywhere approach. We can execute the Java program on every machine. Java program (.java) is converted to bytecode (.class) which can be easily run on every machine.

Platform Independent: Java is a platform independent programming language. It is different from other programming languages like C and C++ which needs a platform to be executed. Java comes with its platform on which its code is executed. Java doesn’t depend upon the operating system to be executed.

Secured: Java is secured because it doesn’t use explicit pointers. Java also provides the concept of ByteCode and Exception handling which makes it more secured.

Robust: Java is a strong programming language as it uses strong memory management. The concepts like Automatic garbage collection, Exception handling, etc. make it more robust.

Architecture Neutral: Java is architectural neutral as it is not dependent on the architecture. In C, the size of data types may vary according to the architecture (32 bit or 64 bit) which doesn’t exist in Java.

Interpreted: Java uses the Just-in-time (JIT) interpreter along with the compiler for the program execution.

High Performance: Java is faster than other traditional interpreted programming languages because Java bytecode is “close” to native code. It is still a little bit slower than a compiled language (e.g., C++).

Multithreaded: We can write Java programs that deal with many tasks at once by defining multiple threads. The main advantage of multi-threading is that it doesn’t occupy memory for each thread. It shares a common memory area. Threads are important for multi-media, Web applications, etc.

Distributed: Java is distributed because it facilitates users to create distributed applications in Java. RMI and EJB are used for creating distributed applications. This feature of Java makes us able to access files by calling the methods from any machine on the internet.

Dynamic: Java is a dynamic language. It supports dynamic loading of classes. It means classes are loaded on demand. It also supports functions from its native languages, i.e., C and C++.

4. What is the difference between JDK, JRE, and JVM?

JVM is an acronym for Java Virtual Machine; it is an abstract machine which provides the runtime environment in which Java bytecode can be executed. It is a specification which specifies the working of Java Virtual Machine. Its implementation has been provided by Oracle and other companies. Its implementation is known as JRE.

JVMs are available for many hardware and software platforms (so JVM is platform dependent). It is a runtime instance which is created when we run the Java class. There are three notions of the JVM: specification, implementation, and instance.

JRE stands for Java Runtime Environment. It is the implementation of JVM. The Java Runtime Environment is a set of software tools which are used for developing Java applications. It is used to provide the runtime environment. It is the implementation of JVM. It physically exists. It contains a set of libraries + other files that JVM uses at runtime.

JDK is an acronym for Java Development Kit. It is a software development environment which is used to develop Java applications and applets. It physically exists. It contains JRE + development tools. JDK is an implementation of any one of the below given Java Platforms released by Oracle Corporation:

  • Standard Edition Java Platform
  • Enterprise Edition Java Platform
  • Micro Edition Java Platform

5. What is JIT?

Just-In-Time(JIT) compiler: It is used to improve the performance. JIT compiles parts of the bytecode that have similar functionality at the same time, and hence reduces the amount of time needed for compilation. Here the term “compiler” refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.

6. What is a ClassLoader?

A classloader in Java is a subsystem of Java Virtual Machine, dedicated to loading class files when a program is executed; ClassLoader is the first to load the executable file.

Java has Bootstrap, Extension, and Application classloaders.

7. What are the Memory Allocations available in JavaJava?

Java has five significant types of memory allocations.

  • Class Memory
  • Heap Memory
  • Stack Memory
  • Program Counter-Memory
  • Native Method Stack Memory

8. What are the differences between Heap and Stack Memory in Java?

Stack memory in data structures is the amount of memory allocated to each individual programme. It is a fixed memory space. Heap memory, in contrast, is the portion that was not assigned to the Java code but will be available for use by the Java code when it is required, which is generally during the program’s runtime.

9. Is Data Passed by Reference or by Value in Java?

Passing by value – means that we pass a copy of an object as a parameter into a method.
Passing by reference – means that we pass a reference to an object as a parameter into a method.
They represent two types of data that we can pass to a method: a primitive and an object.

When we pass primitives to a method, its value is copied into a new variable. When it comes to objects, the value of the reference is copied into a new variable. So we can say that Java is a strictly pass-by-value language.

10. What Is the Difference Between Import and Static Imports?

We can use regular imports to import a specific class or all classes defined in a different package:

import java.util.ArrayList; //specific class
import java.util.*; //all classes in util package

We can also use them to import public nested classes of an enclosing class:

import com.weblogiks.model.*

There are also static imports that enable us to import static members or nested classes:
import static java.util.Collections.EMPTY_LIST;

11. Which Access Modifiers Are Available in Java and What Is Their Purpose?

There are four access modifiers in Java:

default (package)
The private modifier assures that class members won’t be accessible outside the class. It can be applied to methods, properties, constructors, nested classes, but not to top-level classes themselves.

Unlike the private modifier, we can apply the default modifier to all types of class members and to the class itself. We can apply default visibility by not adding any access modifier at all. If we use default visibility our class or its members will be accessible only inside the package of our class. We should keep in mind that the default access modifier has nothing in common with the default keyword.

Similar to the default modifier, all classes within one package can access protected members. What’s more, the protected modifier allows subclasses to access the protected members of a superclass, even if they are not within the same package. We can’t apply this access modifier to classes, only to class members.

The public modifier can be used together with the class keyword and all class members. It makes classes and class members accessible in all packages and by all classes.

12. Which Other Modifiers Are Available in Java and What Is Their Purpose?

There are five other modifiers available in Java:

These do not control visibility.

First of all, we can apply the static keyword to fields and methods. Static fields or methods are class members, whereas non-static ones are object members. Class members don’t need an instance to be invoked. They are called with the class name instead of the object reference name.

Then, we have the final keyword. We can use it with fields, methods, and classes. When final is used on a field, it means that the field reference cannot be changed. So it can’t be reassigned to another object. When final is applied to a class or a method, it assures us that that class or method cannot be extended or overridden.

The next keyword is abstract. This one can describe classes and methods. When classes are abstract, they can’t be instantiated. Instead, they are meant to be subclassed. When methods are abstract, they are left without implementation and can be overridden in subclasses.

The synchronized keyword may be the most advanced. We can use it with the instance as well as with static methods and code blocks. When we use this keyword, we make Java use a monitor lock to provide synchronization on a given code fragment.
The last keyword we’re going to discuss is volatile. We can only use it together with instance fields. It declares that the field value must be read from and written to the main memory – bypassing the CPU cache. All reads and writes for a volatile variable are atomic.