Java Beginner Interview Questions
If you are preparing for a Java developer interview, mastering the basics is crucial. Interviewers look for a strong foundation in Object-Oriented Programming (OOP) principles, core Java concepts, and an understanding of the Java Virtual Machine (JVM).
1. Core Java & JVM
Q: What is the difference between JDK, JRE, and JVM?
- JVM (Java Virtual Machine): An abstract machine that executes Java bytecode. It is platform-dependent.
- JRE (Java Runtime Environment): Contains the JVM and the core libraries necessary to run a Java application. It does not contain development tools.
- JDK (Java Development Kit): A full-featured software development kit. It contains the JRE, along with development tools like
javac(the compiler),javadoc, andjdb.
Q: Why is Java considered platform-independent?
Java code is compiled into an intermediate form called bytecode (.class files). This bytecode is not native to any specific processor architecture. Instead, it is executed by the JVM, which is tailored to the specific underlying operating system. Hence, "Write Once, Run Anywhere" (WORA).
Q: Explain public static void main(String[] args).
public: Access modifier allowing the JVM to invoke the method from outside the class.static: Allows the JVM to invoke the method without instantiating the class.void: The method does not return any value.main: The name of the method configured as the entry point in the JVM.String[] args: Command-line arguments passed to the program as an array of Strings.
2. Object-Oriented Programming (OOP)
Q: What are the four pillars of OOP?
- Encapsulation: Wrapping data (variables) and code (methods) together into a single unit (class). Controlled via access modifiers (e.g.,
private,public). - Inheritance: A mechanism where a new class inherits properties and behaviors from an existing class using the
extendskeyword. - Polymorphism: The ability of an object to take on many forms. Achieved via method overloading (compile-time) and method overriding (runtime).
- Abstraction: Hiding complex implementation details and showing only the essential features of the object, achieved using
abstractclasses and interfaces.
Q: What is the difference between method overloading and method overriding?
- Method Overloading: Having multiple methods with the same name but different parameters (type, number, or both) in the same class. Resolved at compile-time.
- Method Overriding: A subclass providing a specific implementation of a method already defined in its superclass. Must have the exact same signature. Resolved at runtime (Dynamic Method Dispatch).
Q: Difference between Abstract Class and Interface?
| Feature | Abstract Class | Interface |
|---------|----------------|-----------|
| Methods | Can have both abstract and concrete methods. | Methods are implicitly public abstract (pre-Java 8). Java 8+ introduced default and static methods. |
| Variables | Can have state (instance variables). | Variables are implicitly public static final (constants). |
| Inheritance| A class can extend only one abstract class. | A class can implement multiple interfaces. |
| Constructors| Can have constructors. | Cannot have constructors. |
3. Data Types and Variables
Q: Difference between == and .equals()?
==: An operator that checks for reference equality (whether both references point to the exact same object in memory) for objects, and value equality for primitives..equals(): A method used to check for value equality between objects (logically equivalent). Must be overridden in custom classes; otherwise, it defaults to==behavior from theObjectclass.
Q: What is the String Pool?
A special storage area in the Java heap where string literals are stored. When a new string literal is created, the JVM checks the pool. If the string already exists, a reference to the pooled instance is returned, saving memory. Strings created with the new keyword bypass the pool unless intern() is called.
Q: Why is String immutable in Java?
Security, synchronization, caching, and performance. Because Strings are immutable, they are inherently thread-safe and can be safely shared among multiple threads. It also allows the JVM to safely cache string literals in the String Pool.
4. Exceptions
Q: Difference between Checked and Unchecked Exceptions?
- Checked Exceptions: Inherit from
Exception(but notRuntimeException). Checked at compile-time. You must either catch them using atry-catchblock or declare them in the method signature usingthrows. Example:IOException,SQLException. - Unchecked Exceptions: Inherit from
RuntimeException. Not checked at compile-time. Usually indicate programming errors. Example:NullPointerException,ArrayIndexOutOfBoundsException.
Q: Can a finally block not execute?
Yes, in rare scenarios:
System.exit()is called in thetryorcatchblock.- The JVM crashes or the system loses power.
- An infinite loop occurs in the
tryorcatchblock.