Java Technical Interview Questions
1. Why is Java a platform independent language?
Java is platform-independent because of a brilliant trick that allows it to follow the principle
of "Write Once, Run Anywhere" (WORA).
The Problem with Other Languages
In most older languages (like C++), when you write code, it gets turned directly into
instructions for a specific computer (like a Windows PC with an Intel chip). If you want that
program to run on a Mac or a Linux machine, you have to recompile the original code using a
different compiler.
Java's Solution: The Virtual Middleman
Nikunj Jain
Java avoids this problem by using a two-step process:
1. The Universal Language (Bytecode) ^ †,z → ’
ˆ˜.̧-*’-̂˙ ⬛˘*_':
o When you compile your Java code, it isn't turned into instructions for a real computer.
Instead, it's turned into a special intermediate code called bytecode.
o Think of bytecode as a universal instruction set for a make-believe computer. This
bytecode file is exactly the same, no matter if you compiled it on Windows, Mac, or
Linux.
2. The Interpreter (JVM) '
˘⬛*_ → □
’ ;_ :
o To run the bytecode, you need a Java Virtual Machine (JVM). The JVM is the key to
platform independence.
o The JVM acts as an interpreter, or a translator. You have a different JVM for every
operating system (a Windows JVM, a Mac JVM, a Linux JVM, etc.).
o When you run your program, the JVM on that specific computer reads the universal
bytecode and instantly translates it into the native machine code that the real
computer hardware can understand.
2. What is the Significance of word public Static void main()?
The phrase public static void main(String[] args) is the entry point of any standalone Java
application — it's the method the Java Virtual Machine (JVM) looks for to start program
execution. Each keyword in it has a specific role that makes the method recognizable and
executable by the JVM.
Nikunj Jain
˙Q•Breakdown of public static void main(String[] args)
1. public
o This is an access modifier.
o It means the method is accessible from anywhere, including outside the class.
o The JVM needs to access this method from outside your class to start the program, so
it must be public.
2. static
o This means the method belongs to the class itself, not an instance of the class.
o The JVM can call this method without creating an object of the class.
o This is essential because no objects exist when the program starts.
3. void
o This is the return type of the method.
o It means the method does not return any value.
4. main
o This is the name of the method that the JVM looks for as the starting point of the
program.
o If you name it anything else, the program will compile but won’t run.
5. String[] args
o This is the parameter passed to the method.
o It allows the program to accept command-line arguments as input.
o For example, if you run java Hello, then args[0] is "Hello" and args[1] is "World".
3. What exactly is [Link] in Java?
[Link]() in Java is a command used to print text or data to the console. It’s one of
the most commonly used methods for displaying output during program execution.
Ç Breaking Down [Link]()
Nikunj Jain
Each part of [Link]() has a specific role:
System: This is a final class in the [Link] package. It provides access to system-level
resources like input, output, and error streams.
out: This is a static member of the System class. It’s an instance of PrintStream, which is
used to send output to the console. Think of it as the connection between your program
and the screen.
println(): This is a method of the PrintStream class. It prints the argument passed to it and
then moves the cursor to a new line. If you use print() instead, it won’t add a new line after
the output.
4. How does the JVM use memory for the Stack and Heap?
The Stack and Heap are the two primary memory areas in the Java Virtual Machine
(JVM) used to execute a program. They manage different kinds of data and operate
under distinct rules.
1. Method Execution (The Stack):
Whenever a method is called, a new Stack Frame is created and pushed onto the
Stack.
This frame stores the method's primitive local variables (like int x = 10;) and the
references (memory addresses) to any objects created in that method.
When the method finishes, the entire Stack Frame is immediately popped and
discarded, instantly freeing up all the memory it used.
Nikunj Jain
2. Object Storage (The Heap):
Whenever the keyword new is used (e.g., new MyObject()), the actual object data is
stored in the Heap.
A reference variable is created on the Stack to point to the object's location on the
Heap.
The Heap is constantly monitored by the Garbage Collector (GC). If the object on the
Heap loses all references pointing to it from the Stack, the GC eventually sweeps it
away to reclaim the memory. This automatic management is why the Heap is
essential to Java.
5. Why Java is not pure Object Oriented language?
Nikunj Jain
Java is not considered a pure (or "100%") Object-Oriented Programming (OOP) language
because it includes language features that violate the strict rule that everything in the
program must be an object.
This is mainly due to two key exceptions:
1. Primitive Data Types
Java isn't a "pure" Object-Oriented Programming (OOP) language primarily because it
uses primitive data types.
Non-OOP Feature: Java uses primitives like int, float, and boolean.
The Problem: In a pure OOP language, all data must be an object. Primitives are not
objects; they don't belong to a class hierarchy and can't use methods (e.g., you can't
call [Link]()).
The Reason: Primitives exist for performance and memory efficiency, as they are
faster to process than full objects.
The Compromise: Java provides Wrapper Classes (like Integer) to treat primitives as
objects when needed, but the existence of non-object primitives prevents Java from
being truly pure.
2. Static Members
The core principle of OOP is that all behavior (methods) should be tied to an instance
(an object) of a class.
Non-OOP Feature: Java allows you to use the static keyword for variables and
methods.
The Problem: Static members belong to the class itself, not to any specific object.
You can call a static method (like main() or [Link]()) without creating any
object from the class, which breaks the principle that everything must revolve around
objects and their interactions.
Because of the necessity of primitives for speed and the convenience of static features,
Java is described as a largely Object-Oriented or Hybrid language, but not a "pure" one
(unlike languages such as Smalltalk, where everything is an object).
Nikunj Jain
6. What is the importance of main method in Java?
The main() method is the entry point of any standalone Java application. The syntax
of the main method is public static void main(String args[]).
The main method is public and static so that Java can access it without initializing
the class. The input parameter is an array of String, through which we can pass
runtime arguments to the Java program.
Yes, we can have multiple methods with the name main in a single class. However, if
we run the class, the Java Runtime Environment (JRE) will look for the main method
with the exact syntax: public static void main(String args[]).
7. Can we overload main method?
Yes, you can overload the main method in Java, just like any other method. However,
there is a crucial distinction:
JVM Entry Point is Fixed: The Java Virtual Machine (JVM) will only recognize and execute
the method with the exact standard signature as the application's starting point: public
static void main(String[] args)
Nikunj Jain
Overloaded Methods Must Be Called Manually: If you create any other versions of main
(e.g., public static void main(int a) or public static void main()), the JVM will ignore them.
To execute these overloaded methods, you must explicitly call them from inside the
standard main(String[] args) method.
How to Overload main() :-
Method overloading works by changing the method's parameter list. You can overload the
main method by:
1. Changing the number of parameters:
public static void main(String[] args)
public static void main(String arg)
2. Changing the data type of the parameters:
public static void main(String[] args)
public static void main(int[] args)
8. JVM, JDK and JRE. Components Describe.
1. Java Virtual Machine (JVM)
Purpose: Executes Java bytecode and provides platform independence.
Key Components:
o Class Loader: Loads .class files (bytecode) into memory.
o Bytecode Verifier: Checks code for security and correctness.
Nikunj Jain
o Interpreter: Converts bytecode into machine code line by line.
o JIT Compiler (Just-In-Time): Improves performance by compiling bytecode into
native code at runtime.
o Garbage Collector: Automatically manages memory by removing unused objects.
The JVM is platform-specific but executes platform-independent bytecode, making Java
portable.
†5 2. Java Runtime Environment (JRE)
_—
Purpose: Provides the environment to run Java applications.
Includes:
o JVM: The engine that runs the bytecode.
o Java Class Libraries: Pre-built classes (like [Link], [Link]) used by programs.
o Supporting Files: Configuration files and resources needed during execution.
JRE is ideal for users who want to run Java programs but not develop them.
\’3. Java Development Kit (JDK)
z‘
Purpose: Full toolkit for developing Java applications.
Includes:
o JRE: So you can run and test your code.
o Development Tools:
javac: Java compiler that converts .java files to .class bytecode.
java: Launches the JVM to run programs.
javadoc: Generates documentation from comments.
jar: Packages classes into a .jar file.
debugger and profiler: For testing and performance tuning.
JDK is essential for developers — it’s the complete package for writing, compiling, and
running Java code.
G. How actually Java Code Works.
Nikunj Jain
Java code works through a two-step process involving a compiler and a virtual machine,
which is key to its "Write Once, Run Anywhere" principle.
The entire process can be divided into two main phases:
1. Compilation
Writing the Source Code: You write your Java code in a text file (the source code),
typically using an IDE or a text editor, and save it with a .java extension (e.g.,
[Link]).
Compiling to Bytecode: The javac compiler (part of the Java Development Kit, or JDK)
reads your source code. If there are no syntax errors, it translates the human-readable
Java code into an intermediate, platform-independent format called bytecode.
Generating the Class File: The bytecode is saved in a file with a .class extension (e.g.,
[Link]). This .class file contains instructions for the Java Virtual Machine (JVM).
2. Execution
The execution phase is handled by the Java Virtual Machine (JVM), which is part of the
Java Runtime Environment (JRE).
Class Loading: When you run the program (using the java command), the Class Loader
within the JVM finds the required .class file(s) and loads them into the JVM's memory.
Bytecode Verification and Linking:
o The Bytecode Verifier checks the loaded bytecode to ensure it is structurally
correct and adheres to Java's security rules (e.g., no unauthorized memory
access).
o Linking prepares the class for execution, including allocating memory for static
fields and resolving symbolic references.
Execution: The JVM's Execution Engine executes the bytecode instructions. It primarily
uses two methods:
o Interpreter: Reads and executes bytecode instructions one by one. This is
generally slower but happens immediately.
Just-In-Time (JIT) Compiler: For frequently executed blocks of code ("hot spots"), the JIT
compiler compiles the bytecode into native machine code (the specific instructions for
your computer's operating system and processor). This native code is stored in a cache,
leading to much faster subsequent execution.
10. How does garbage collection work in Java?
Java Garbage Collection (The "Memory Manager: Ramesh")
Nikunj Jain
Imagine the Java Virtual Machine (JVM) is a big office building where your software (the
app) works.
The Heap is the main storage room in the office.
Every piece of data your program creates — every object — is stored here.
Now meet Ramesh, the Garbage Collector (GC). He’s like a super-efficient Memory
Manager whose job is to keep the storage room clean and organized — without bothering
the developers.
Ramesh’s Core Job
Your program is constantly creating new objects, filling up the heap. But Ramesh doesn’t
just look for “trash.” His job is to:
Identify what’s still being used (i.e., objects that are still reachable).
Clear out everything else (i.e., unreachable objects that are no longer needed).
He does this quietly in the background, making sure your app runs smoothly without
running out of memory.
˙ How Ramesh Works (Behind the Scenes)
Q•
1. Mark Phase
Ramesh walks through the heap and marks all the objects that are still in use — those
that are reachable from active parts of the program.
2. Sweep Phase
He then clears out all the unmarked objects — the ones no longer referenced by any
part of the program.
3. Compact Phase (optional)
If the storage room is messy, Ramesh reorganizes the remaining objects to make space
more efficient and reduce fragmentation.
Nikunj Jain