Java Basic Interview Questions

1. What is Java, and How is it Different from Other Programming Languages?

Java is a high-level programming language developed by Sun Microsystems by James Gosling and Bill Joy. It is an independent platform.

Java is similar to C++ and supports object-oriented programming techniques, which allows many C and C++ programmers to master the language quickly. Java also has GUI features that provide a better look and feel than the C++ language.

Difference between Java and Other languages

  • In Java, it compiles as bytecode and runs on the JVM, whereas in C++, it compiles as machine code.
  • Java is platform-independent, and C++ platform-dependent
  • For memory management, Java uses automatic memory collection, whereas C++ uses manual memory management
  • Web and mobile enterprises use Java, while system-level and game development use C++.

2. Explain the features of Java (OOP, Platform Independence, Robustness, etc.).

Java is simple.

  • Java is simple to write, more readable, and eye-catching.
  • Java has features that make it simple to learn and use.
  • Most concepts are similar to C++, making it simple to learn.

Java is secure.

  • Java offers a safe ground for developing online projects.
  • Java provides a safe approach to accessing online tools.
  • Java prevents direct memory access.
  • Java prevents unauthorized code execution.

Java is portable.

  • Java is platform-independent; programs can run in any environment as long as there is a Java runtime (JVM).
  • Java programs can run on any platform (Mac, Linux, Windows)
  • Java programs can be transferred over the Web. Ex: Applets.

Java is object-oriented.

  • Java is an object-oriented programming language.
  • Like C++, it provides features similar to those of an object-oriented language.
  • Java allows data and methods to be wrapped together in an encapsulation class.
  • Java allows for the inheritance of properties and behavior from the parent class. We refer to this as inheritance.
  • Java supports method overloading and method overriding.

Java is robust.

  • Java is an error-free programming language.
  • Java is strictly typed and performs runtime checks.
  • Java prevents memory leaks. We refer to it as automatic garbage collection.

Java is multithreaded.

  • Java provides integrated support for multithreaded programming languages.

Java is architecturally neutral.

  • Java does not impose restrictions on any specific machine or operating system architecture.
  • Machine independent. i.e., Java is independent of hardware.

Java is interpreted

  • JVM can interpret Java bytecode on any platform.
  • Java bytecode helps Java support cross-platform codes.

Java’s Performance

  • In Java, bytes are Highly Optimized
  • JVM can execute them much faster.

Java is Distributed

  • Java was designed for a distributed environment.
  • You can transmit and run Java over the internet.
  • Java allows the creation of distributed programs in which components run on separate machines yet cooperate.

3. How Does Java Achieve Platform Independence?

Java achieves platform independence via bytecode and JVM. Here’s how:

Write Java Code (.java file)

Compile using Javac compiler → Produces bytecode (.class file)

JVM reads the bytecode and translates it for the underlying platform

Bytecode is an intermediate, platform-independent code, while JVM acts as an interpreter between bytecode and machine code.

4. Difference Between JDK, JRE, and JVM

The full form of JDK is Java Development Kit, JRE is Java Runtime Environment, and JVM is Java Virtual Machine.

The Purpose of JDK is to provide a Complete Package for Java development, the purpose of JRE is to run Java applications, and JVM is to Interpret and execute bytecode on any platform.

5. Explain the Java Compilation Process 

The Java compilation process is a multi-step mechanism that transforms source code into machine-executable code through an intermediate stage.

Step-by-Step Compilation Process:

Step 1: Writing Source Code

  • A.java file contains the written code.

Example: HelloWorld.java

Step 2: Compilation to Bytecode

  • javac (Java Compiler) compiles .java file into **bytecode (.class file)*`.

javac HelloWorld.java

Step 3: Bytecode Verification

  • The JVM’s class loader loads the .class file.
  • The bytecode verifier checks code integrity and security.

Step 4: Execution via JVM

  • The JVM interprets the bytecode or compiles it using a JIT (Just-In-Time) compiler into native machine code for execution.

Visualization of Compilation Process:

Java Source Code (.java) -> javac Compiler -> Bytecode (.class) -> Class Loader (JVM) -> Bytecode Verifier -> Execution (via JVM & JIT)

6. What is Bytecode in Java?

Bytecode in Java is a platform-independent, intermediate code generated after compiling a .java source file. It has a .class extension and contains instructions that can be executed by the Java Virtual Machine (JVM) on any platform.

Key Points:

  • It is not machine code, but it is close to machine-level instructions.
  • Ensures platform independence (Write Once, Run Anywhere—WORA).
  • Acts as a bridge between source code and machine code.
  • The JVM interprets or compiles bytecode into native code at runtime.

Example: If you write a program HelloWorld.java, compiling it creates HelloWorld.class (bytecode).

7. Explain the Java Virtual Machine (JVM)

The Java Virtual Machine (JVM) is a virtual runtime environment that executes Java bytecode. Abstracting underlying hardware makes the Java platform independent.

Key Responsibilities of JVM:

  1. ClassLoader is responsible for loading class files.
  2. The JVM verifies the bytecode to ensure its security.
  3. Interpreting/Compiling bytecode into native machine code (via Just-In-Time (JIT) compiler).
  4. Memory Management using Heap, Stack, Method Area, and Garbage Collection.
  5. Exception Handling and Security Checks at runtime.

8. What is a main() Method in Java?

public static void main(String[] args)

  • public static void main(String[] args) is a method. A program can execute a method by calling its name.
  • public – It is an Access modifier that determines what outside classes can use/call the method.
  • static means that the method is class-related and not instance-related. Currently, this means that you can call static methods of a class without needing to create an instance using the “new” keyword first, as is done with the main(String[] args) method.
  • “void” is the method’s return type; as expected, “void” means the method returns nothing.
  • leading is the name of the method, and it is also a unique name, as the Java Virtual Machine uses the primary method as the start of a program.
  • String[] args is an array of strings used as arguments for the primary method.

9. What are Wrapper Classes in Java?

Wrapper classes are object representations of primitive data types. Using wrapper classes, Java treats primitives as objects (necessary for collections like ArrayList and HashMap).

10. Explain the Use of the var Keyword in Java

The **var** keyword, introduced in Java 10, allows local variable type inference. This means the compiler automatically infers the type of the variable based on the value assigned.

Syntax:

var message = “Hello, World!”;

var number = 100;

Important Note:

  • Used only for local variables (inside methods, blocks).
  • Improves readability, especially for complex types.
  • Type inference happens at compile time, not runtime.
  • Once inferred, the type cannot change.
Scroll to Top
Verified by MonsterInsights