1. Why is Java called platform-independent, and how does bytecode make this possible?
Answer: Java is platform-independent because source code (.java) is compiled into intermediate bytecode (.class) rather than native CPU machine code. Bytecode acts as a universal set of instructions. Any operating system with a compatible Java Virtual Machine (JVM) installed can interpret and run this bytecode. While the JVM itself is platform-specific (a Windows JVM differs from a macOS JVM), the compiled bytecode remains identical everywhere, fulfilling Java’s “Write Once, Run Anywhere” (WORA) design.
Â
2. What are the main functional differences between the JDK, JRE, and JVM?
Answer:Â
-
JVM (Java Virtual Machine): The core execution engine. It loads
.classfiles, verifies bytecode, and converts it into native system instructions at runtime. -
JRE (Java Runtime Environment): The software bundle required to run Java programs. It contains the JVM along with standard class libraries (
rt.jar/java modules) and configuration files. -
JDK (Java Development Kit): The full software environment for developers. It includes the entire JRE plus development tools like
javac(compiler),jdb(debugger), andjavadoc.
Â
3. What happens behind the scenes when you run javac Application.java followed by java Application?
Answer:
-
Compilation (
javac Application.java): The Java compiler checksApplication.javafor syntax errors. If valid, it translates human-readable source code into intermediate bytecode and saves it asApplication.class. -
Execution (
java Application): The JVM starts, loadsApplication.classinto memory via its ClassLoader, verifies the bytecode for security compliance, and passes it to the execution engine (using JIT compilation and interpretation) starting directly at thepublic static void mainmethod.
Â
4. Why must the main method signature be strictly written as public static void main(String[] args)?
Answer:
-
public: Allows the JVM to call the method from outside the class context during launch. -
static: Enables the JVM to invoke the method directly on the class without allocating or initializing an object instance first. -
void: Tells the JVM that the program execution terminates without returning a status value back to the method caller. -
main: The exact identifier string the JVM searches for as the application entry point. -
String[] args: Accepts command-line parameters passed in during program startup.
Â
5. What is the primary role of the Java Garbage Collector, and how does it benefit developers?
Answer: The Garbage Collector (GC) is an automated system process within the JVM that manages heap memory. It tracks objects in memory and automatically deletes those that are no longer reachable or referenced anywhere in running code. This prevents memory leaks and saves developers from having to manually allocate and free memory (unlike languages like C/C++).
Â
6. If a file contains public class StudentData, what must the file be named, and why?
Answer: The file must be named StudentData.java. Java enforces a strict rule: if a class is declared as public, its file name must match the class name character-for-character (including exact casing). This structure allows the compiler and JVM to quickly map class references directly to file paths on disk.
Â
7. What is the JIT (Just-In-Time) compiler, and how does it speed up Java program execution?
Answer: The JIT compiler is an optimization module inside the JVM. Instead of line-by-line interpretation of bytecode throughout the entire program run, the JIT monitors code performance and identifies “hot spots” (frequently executed sections like loops). It compiles these specific bytecode sections into native machine code once and caches them in memory for direct execution, significantly improving runtime speed.
Â
8. What was the original name of Java, and who led the team that created it?
Answer:Â Java was originally named Oak (named after an oak tree outside the creator’s office). It was created by James Gosling and his team (known as the Green Team) at Sun Microsystems in 1995.
Â
9. Can you compile a .java file on Windows and run the generated .class file on Linux without recompiling? Explain.
Answer: Yes. Because compilation generates platform-neutral bytecode rather than OS-specific machine instructions, a .class file created on Windows can be copied straight to a Linux machine and run on a Linux JVM without any changes or recompilation.
Â
10. What error will occur if you try to run a program where the main method is missing static?
Answer:Â The code will compile cleanly using javac, but when you attempt to run it using java, the JVM will throw a runtime error: NoSuchMethodError: main or report Main method is not static in class.... This happens because the JVM tries to invoke main directly without instantiating the surrounding class.
Â