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 at 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 C++ is used in system-level and game development.
2. Explain the features of Java (OOP, platform independence, Robustness, etc.).
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
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.
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:
Loading class files via ClassLoader.
Verifying bytecode to ensure security.
Interpreting/Compiling bytecode into native machine code (via Just-In-Time (JIT) compiler).
Memory Management using Heap, Stack, Method Area, and Garbage Collection.
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.
The method is named leading, and it has a unique designation because the Java Virtual Machine uses the primary method as the starting point 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;
Note
Used only for local variables (inside methods, blocks).
Improves readability, especially for complex types.
Type inference happens at compile-time, not runtime.
Used by Google Analytics to determine which links on a page are being clicked
30 seconds
_ga_
ID used to identify users
2 years
_gid
ID used to identify users for 24 hours after last activity
24 hours
_gat
Used to monitor number of Google Analytics server requests when using Google Tag Manager
1 minute
_gac_
Contains information related to marketing campaigns of the user. These are shared with Google AdWords / Google Ads when the Google Ads and Google Analytics accounts are linked together.
90 days
__utma
ID used to identify users and sessions
2 years after last activity
__utmt
Used to monitor number of Google Analytics server requests
10 minutes
__utmb
Used to distinguish new sessions and visits. This cookie is set when the GA.js javascript library is loaded and there is no existing __utmb cookie. The cookie is updated every time data is sent to the Google Analytics server.
30 minutes after last activity
__utmc
Used only with old Urchin versions of Google Analytics and not with GA.js. Was used to distinguish between new sessions and visits at the end of a session.
End of session (browser)
__utmz
Contains information about the traffic source or campaign that directed user to the website. The cookie is set when the GA.js javascript is loaded and updated when data is sent to the Google Anaytics server
6 months after last activity
__utmv
Contains custom information set by the web developer via the _setCustomVar method in Google Analytics. This cookie is updated every time new data is sent to the Google Analytics server.
2 years after last activity
__utmx
Used to determine whether a user is included in an A / B or Multivariate test.