Basic Java Interview Questions with Answers

Define JVM, JRE and JDK?

JVM stands for “Java Virtual Machine”. JVM follows the “write once, read anywhere” principle which allows Java programs to run on any device or operating system. Its implementation has been provided by Oracle and other companies. Its implementation is known as JRE. There are three notions of the JVM: Specification, Implementation, and Instance.

JRE stands for “Java Runtime Environment”. It is used for developing Java applications and to provide a runtime environment. It is the implementation of JVM.

JDK stands for “Java Development Kit”. It is used to develop Java applications and applets. JDK contains JRE + development tools. It is an implementation of Standard Edition Java Platform, Enterprise Edition Java Platform, and Micro Edition Java Platform.

How many types of memory areas are allocated by JVM?

Class Area It keeps track of per-class structures like the runtime constant pool, field, method data, and method code.

Heap It’s the runtime data area where the memory of the object is allocated.

Stack Frames are stored in Java Stack. It manages local variables and partial results, as well as invoking and returning methods. Each thread has its own JVM stack, which is built concurrently with the thread. Each time a method is called, a new frame is created. A frame is destroyed when its method invocation completes.

Program Counter Register Each JVM string that completes the undertaking of a particular strategy has a program counter register related to it. The non-local strategy has a PC that stores the location of the accessible JVM instructions while, in a local technique, the value of the program counter is unclear. PC register is capable of storing the return address or a native pointer on some specific platform.

Native Method Stack It contains all the native methods used in the application.

Which part of memory is involved in garbage collection?

Heap. When Java programs run on the JVM, objects are created on the heap, which is a portion of memory dedicated to the program.

What gives Java its ‘write once and run anywhere’ nature?

The Bytecode. Java compiler converts the Java programs into the class file (Byte Code) which is the intermediate language between source code and machine code. This bytecode is not platform-specific and can be executed on any computer.

Define various access specifiers in Java?

Public The classes, methods, or variables which are defined as public, can be accessed by any class or method.

Protected can be accessed by the class of the same package, or by the sub-class of this class, or within the same class.

Default are accessible within the package only. By default, all the classes, methods, and variables are of default scope.

Private The private class, methods, or variables defined as private can be accessed within the class only.

What is ClassLoader in java? What are the types of ClassLoader in Java?

Java ClassLoader is used to load the classes at runtime i.e. when JVM performs the linking process at runtime, classes are loaded into the JVM according to need. There are three different types of ClassLoaders: Application ClassLoader loads our own files in the classpath. Extension ClassLoader load classes that are an extension of the standard core java class. Bootstrap ClassLoader is the parent of all the other ClassLoaders.

List some advantages of packages in Java.

It avoids naming conflicts.

It provides easier access control.

It is easier to locate classes and interfaces.

It implements Data Encapsulation.

List the features of Java Programming Language.

Simple Java is easy to learn. The syntax of Java is based on C++ which makes it 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 types of objects that incorporates both data and behavior.

Robust To gain reliability, Java restricts you in a few key areas to force you to find your mistakes early in program development. At the same time, Java frees you from having to worry about many of the most common causes of programming errors, because it checks your code at compile time.

Multithreaded It allows you to write programs that do many things simultaneously. The main advantage of multi-threading is that it doesn’t occupy memory for each thread, it shares a common memory area.

Architecture-Neutral Java is architectural neutral as it is not dependent on the architecture. The main goal was to “write once, run anywhere, any time, forever”.

High Performance The java bytecode was carefully designed so that it would be easy to translate directly into native machine code for high performance by using a just-in-time compiler. Java is faster than other traditional interpreted programming languages because of bytecode.

Distributed Java is designed for the distributed environment of the Internet because it handles TCP/IP protocols. Java also supports Remote Method Invocation(RMI). This feature enables a program to invoke methods across a network.

Dynamic Java supports the dynamic loading of classes. It also supports functions from its native languages, i.e., C and C++.

Is it the correct way to write “static public void instead of public static void”?

Yes, The program compiles and runs correctly because the order of specifiers doesn’t matter in java.

For what reason is Java not a pure object-oriented language?

Because Java supports primitive data types (byte, boolean, char, int, short, float, long, and double).