Java Interview Questions and Answers
1) What is Java? Java is a high-level, object-oriented, robust, secure programming language, platform-independent, high-performance, Multithreaded, and portable programming language. It was developed by James Gosling and his team at Sun Microsystems in June 1991. It can also be known as the platform, as it provides its own JRE and API. Java is designed to be versatile, secure, and portable, allowing developers to write code that can run on various platforms without modification. 2) List the features of Java Programming language. • There are the following features in the Java Programming Language. • Simple: Java is easy to learn. The syntax of Java is based on C++, which makes it easier to write programs in it. • Object-Oriented Programming (OOP): Java follows the object-oriented paradigm, which allows us to maintain our code as a combination of different types of objects that incorporate both data and behavior. Everything in Java is treated as an object, promoting modularity and code reusability. • Portable: Java supports a read-once-write-anywhere approach. We can execute the Java program on every machine. A 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 need a platform to be executed. Java comes with its own platform on which its code is executed. Java doesn't depend upon the operating system to be executed. • Secured: Java is secured because it does not use explicit pointers. Java also provides the concept of ByteCode and Exception handling, which makes it more secured. The Java Runtime Environment (JRE) includes a security manager and a set of APIs to control access to resources and prevent malicious activities. • 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 architecture-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 does not 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 (for example, C++). While interpreted, Java's Just-In-Time (JIT) compilation translates bytecode into machine code for improved performance. • 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 does not occupy memory for each thread. It shares a common memory area. Threads are important for multi-media, Web applications, etc. Java's bytecode can be executed on any device with a JVM, ensuring portability across different platforms. • 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++. 3) What do you understand by Java Virtual Machine (JVM)? Java Virtual Machine is a virtual machine that enables a computer to run Java programs. It is a virtualized execution environment that allows Java programs to run on any device with a compatible JVM. JVM is the specification that must be implemented in the computer system. It interprets Java bytecode and translates it into machine code for the underlying hardware. The JVM performs the following main tasks: • Loads code • Verifies code • Executes code • Provides runtime environment JDK 24 is the latest release of the Java SE Platform. 4) How many types of memory areas are allocated by the JVM? Class (Method) Area: Class Area stores per-class structures such as the runtime constant pool, field, method data, and the code for methods. Heap: It is the runtime data area in which memory is allocated to the objects Stack: Java Stack stores frames. Holds local variables and partial results, supporting method execution. Each thread has a private JVM stack, created at the same time as the thread. A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes. PC Register (Non-Heap): Keeps track of the current execution position for each thread. Native Method Stack (Non-Heap): It contains native method information and supports native code execution. 5) What is JIT compiler? Just-In-Time (JIT) Compiler: It is used to improve the

1) What is Java?
Java is a high-level, object-oriented, robust, secure programming language, platform-independent, high-performance, Multithreaded, and portable programming language. It was developed by James Gosling and his team at Sun Microsystems in June 1991. It can also be known as the platform, as it provides its own JRE and API. Java is designed to be versatile, secure, and portable, allowing developers to write code that can run on various platforms without modification.
2) List the features of Java Programming language.
• There are the following features in the Java Programming Language.
• Simple: Java is easy to learn. The syntax of Java is based on C++, which makes it easier to write programs in it.
• Object-Oriented Programming (OOP): Java follows the object-oriented paradigm, which allows us to maintain our code as a combination of different types of objects that incorporate both data and behavior. Everything in Java is treated as an object, promoting modularity and code reusability.
• Portable: Java supports a read-once-write-anywhere approach. We can execute the Java program on every machine. A 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 need a platform to be executed. Java comes with its own platform on which its code is executed. Java doesn't depend upon the operating system to be executed.
• Secured: Java is secured because it does not use explicit pointers. Java also provides the concept of ByteCode and Exception handling, which makes it more secured. The Java Runtime Environment (JRE) includes a security manager and a set of APIs to control access to resources and prevent malicious activities.
• 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 architecture-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 does not 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 (for example, C++). While interpreted, Java's Just-In-Time (JIT) compilation translates bytecode into machine code for improved performance.
• 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 does not occupy memory for each thread. It shares a common memory area. Threads are important for multi-media, Web applications, etc. Java's bytecode can be executed on any device with a JVM, ensuring portability across different platforms.
• 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++.
3) What do you understand by Java Virtual Machine (JVM)?
Java Virtual Machine is a virtual machine that enables a computer to run Java programs. It is a virtualized execution environment that allows Java programs to run on any device with a compatible JVM. JVM is the specification that must be implemented in the computer system. It interprets Java bytecode and translates it into machine code for the underlying hardware.
The JVM performs the following main tasks:
• Loads code
• Verifies code
• Executes code
• Provides runtime environment
JDK 24 is the latest release of the Java SE Platform.
4) How many types of memory areas are allocated by the JVM?
Class (Method) Area: Class Area stores per-class structures such as the runtime constant pool, field, method data, and the code for methods.
Heap: It is the runtime data area in which memory is allocated to the objects
Stack: Java Stack stores frames. Holds local variables and partial results, supporting method execution. Each thread has a private JVM stack, created at the same time as the thread. A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes.
PC Register (Non-Heap): Keeps track of the current execution position for each thread.
Native Method Stack (Non-Heap): It contains native method information and supports native code execution.
5) What is JIT compiler?
Just-In-Time (JIT) Compiler: It is used to improve the performance. The compiler is part of the Java Runtime Environment (JRE).
It translates Java bytecode into native machine code at runtime. Therefore, 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 the platform?
A platform refers to the combination of hardware and software on which programs run. In the context of Java, the platform includes the operating system and the Java Virtual Machine (JVM).
There are two types of platforms:
- Software-Based
- Hardware-Based
7) What are the main differences between the Java platform and other platforms?
There are the following differences between the Java platform and other platforms.
- Java is platform-independent due to its "Write Once, Run Anywhere" nature.
- Other platforms may require platform-specific code and compilation for each target environment.
8) What gives Java its 'write once and run anywhere' nature?
Java compiler converts the Java programs into a class file (bytecode), which is the intermediate language between source code and machine code. Code is compiled to bytecode, which is platform-independent, and the JVM executes this bytecode on any device with a compatible JVM.
9) What is a ClassLoader?
Classloader is a subsystem of the JVM that is used to load class files. It loads Java classes into memory during runtime. Responsible for finding and loading class files needed by a Java program.
10) Is an empty .java file name a valid source file name?
Yes, in Java, the source file can be saved with the .java extension, and then it can be compiled using the javac command followed by the file name (including the .java extension). Finally, the program can be executed using the Java command followed by the name of the class that contains the main() method.
Compile the code using the javac command:
javac Main.java
Run the program using the java command:
java Main
11) Is delete, next, main, exit, or null keyword in Java?
No, in Java, delete, next, main, exit, and null are not keywords.
12) If I do not provide any arguments on the command line, then what will the value stored in the String array passed into the main() method, empty or NULL?
It is empty, but not null. If we do not provide any command-line arguments when running a Java program, the String array passed into the main() method will be an empty array, not null. The args parameter of the main() method is an array of strings, and it is always initialized by Java, even if no command-line arguments are provided.
13) What if we write static public void instead of public static void?
The program compiles and runs correctly because the order of specifiers does not matter in Java.
In Java, the placement of access modifiers (public, private, protected) and other specifiers (static, final, etc.) before the return type (void in this case) is flexible. Both public static void and static public void are accepted and considered correct syntax.
14) What is the default value of the local variables?
In Java, local variables are not assigned default values by the compiler. They must be explicitly initialized before they are used, or a compilation error will occur.
15) What are the various access specifiers in Java?
In Java, access specifiers are the keywords that are used to define the access scope of a method, class, or variable. There are two types of modifiers in Java: access modifiers and non-access modifiers.
Access Specifiers
public: public classes, methods, or variables that are defined as public can be accessed by any class or method.
protected: The protected classes, methods, or variables can be accessed by the class of the same package, or by a subclass of this class, or within the same class.
private: The private class, methods, or variables defined as private can be accessed within the class only.
default (package-private): If no access specifier is used, it is considered package-private. Members with default access are accessible only within the same package.
Non-Access Specifier
It includes static, synchronized, abstract, native, volatile, transient.
16) What is the purpose of static methods and variables?
The methods or variables defined as static are shared among all the objects of the class. Static is the part of the class and not of the object. Static methods and variables in Java are associated with the class rather than with an instance of the class. Therefore, static is used in the case where we need to define variables or methods that are common to all the objects of the class.
For example, in the class simulating the collection of the students in a college, the name of the college is a common attribute to all the students. Therefore, the college name will be defined as static.
Class-level Access: Static members belong to the class itself rather than to instances of the class. It means they can be accessed using the class name without creating an instance of the class.
Constants: Static variables are commonly used to define constants in a class. These values are shared among all instances of the class and can be accessed using the class name.
17) What are the advantages of packages in Java?
Packages in Java provide a way to organize and structure classes and interfaces into a hierarchical structure. They offer several advantages:
• Packages avoid the name clashes.
• The Package provides easier access control.
• We can also have the hidden classes that are not visible outside and used by the package.
• It is easier to locate the related classes.