Java Foundations Interview Questions
Common beginner-level Java interview questions covering syntax, variables, and the basics of running a Java program.
Video Tutorial Coming Soon — Channel Production Underway
1. Why must a public class name match its Java file name?easy
The Java compiler enforces this as a hard rule: a .java source file can contain at most one public class, and that class's name must exactly match the file name (case-sensitive). This lets the compiler and JVM locate the right .class file by name without scanning file contents.
// File: HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {}
}2. What's the difference between primitive types and reference types in Java?medium
Primitive types (int, double, boolean, char, etc.) hold their actual value directly in memory and have fixed sizes. Reference types (any class, like String, or arrays) hold a reference (memory address) pointing to an object stored elsewhere on the heap.
A key consequence: comparing two reference-type variables with == checks whether they point to the same object, not whether their contents are equal — that's what .equals() is for.
3. Why is Java described as 'statically typed'?easy
Because every variable's type is declared explicitly and checked by the compiler before the program runs, and that type cannot change afterward. This is different from dynamically-typed languages (like JavaScript or Python), where a variable's type can change at runtime and type errors often only surface while the program is running.
4. What does the JVM actually do when you run a Java program?medium
The Java compiler (javac) first compiles your .java source into platform-independent bytecode (.class files). The Java Virtual Machine (JVM) then loads that bytecode and executes it, translating it into instructions the actual operating system/hardware can run. This two-step process is what gives Java its "write once, run anywhere" portability — the same .class file runs on any machine with a compatible JVM.