Java Basic Interview Questions

Here are the most asked Java basic interview questions for freshers.

Top 10 Java Basic Interview Questions for Freshers


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 to bytecode and runs on the JVM, whereas in C++, it compiles to machine code.
  • Java is platform-independent, and C++ platform-dependent
  • For memory management, Java uses automatic memory collection, whereas C++ uses manual memory management
  • Java is used in web and mobile enterprises, whereas in C++, it is used in system-level and game development.

2. Explain the features of Java (OOP, platform independence, Robustness, etc.).

Java is Simple

  • Java is easy to write, more readable, and eye-catching.
  • Java has features that make it easy to learn and use.
  • Most concepts are similar to C++, making it easy 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. It is called inheritance.
  • It can take 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. It is called automatic garbage collection.

Java is Multithreaded

  • Java supports integrated support of multithreaded programming languages.

Java is Arthcitectural Neutral

  • Java is not restricted to any specific machine or operating system architecture.
  • Machine independent. i.e., Java is independent of hardware.

Java is interpreted

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

Java’s Performace

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

Java is Distributed

  • Java was designed with a distributed environment.
  • Java can be transmitted and run 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 the purpose of 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

  • The code is written in a .java file.

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

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

Step 4: Execution via JVM

  • JVM interprets the bytecode or compiles it using 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.
  • 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. Loading class files via ClassLoader.
  2. Verifying bytecode to ensure 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 method is a block of code that can be executed in a program 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. For now, you can use it to mean that you can call static methods of a class without creating them with the “new” keyword first, like 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