JAVA Interview Questions
JAVA Interview Questions
1. What is Java?
JVM (Java Virtual Machine): Runs Java bytecode, enabling platform independence.
JRE (Java Runtime Environment): Includes JVM and libraries required to run Java
applications.
JDK (Java Development Kit): Contains JRE and development tools like the
compiler (javac) and debugger.
1. Encapsulation: Wrapping data (fields) and methods in a class, with access control.
2. Inheritance: Enabling a new class to inherit properties and behavior from an existing
class.
3. Polymorphism: Allowing methods to do different things based on the object they’re
acting upon (method overriding and overloading).
4. Abstraction: Hiding implementation details, exposing only essential features using
abstract classes and interfaces.
Overloading: Multiple methods with the same name but different parameters within a
class.
Overriding: A subclass provides a specific implementation of a method defined in its
superclass.
6. What is an abstract class, and how is it different from an interface?
Abstract Class: Cannot be instantiated, may have both abstract and concrete
methods.
Interface: Declares a set of abstract methods (Java 8+ allows default and static
methods in interfaces). It’s implemented by classes that promise to provide specific
behavior.
Memory Management
7. Explain garbage collection in Java.
Garbage collection is the process by which Java removes unreferenced objects from memory
automatically to free up space. This is managed by the JVM.
final: Keyword to restrict a variable (constant), method (no overriding), or class (no
inheritance).
finally: A block in exception handling that always executes, even if an exception is
thrown.
finalize(): A method called by the garbage collector before an object is removed
from memory (deprecated in Java 9+).
Exception Handling
9. What is an exception, and what are the types of exceptions in Java?
An exception is an abnormal event that disrupts the program's normal flow. Java has:
The Collections Framework provides a set of classes and interfaces for working with groups
of objects (data structures like List, Set, Map).
HashMap stores entries (key-value pairs) in an array of linked lists. Each key’s hash code
determines the array index, with equals() used to resolve hash collisions.
A thread is a lightweight process that enables concurrent execution within a program. Java
threads are managed by the Thread class and the Runnable interface.
A thread goes through states: New → Runnable → Blocked → Waiting → Timed Waiting
→ Terminated.
Lambda expressions are a concise way to represent functional interfaces (interfaces with one
abstract method), enabling you to write inline implementations.
java
Copy code
Runnable r = () -> System.out.println("Lambda example");
The Stream API provides methods for processing sequences of elements (like lists) with
operations such as filter, map, reduce, and collect.
java
Copy code
List<Integer> numbers = Arrays.asList(1, 2, 3, 4);
numbers.stream().filter(x -> x % 2 == 0).forEach(System.out::println);
Java 8 introduced default methods (methods with a body) and static methods in interfaces to
provide additional functionality without breaking existing implementations.
The JMM defines how Java threads interact through memory, outlining rules for reading and
writing variables to ensure consistent visibility across threads.
21. What is the difference between stack and heap memory in Java?
Stack Memory: Stores method calls, local variables, and references; faster, managed
per thread.
Heap Memory: Stores objects and class variables; shared among threads, managed
by the garbage collector.
Advanced Topics
22. What is the difference between == and equals()?
Enums define a fixed set of constants. They can have methods, constructors, and implement
interfaces.
java
Copy code
public enum Day {
MONDAY, TUESDAY, WEDNESDAY;
}
Reflection allows Java code to inspect and modify the runtime behavior of applications (e.g.,
accessing private methods), often used in frameworks like Spring.
A ClassLoader loads classes into memory at runtime. Java has different loaders, such as the
Bootstrap, Extension, and Application ClassLoader.
These questions provide a strong foundation across various Java topics and give you hands-
on coding practice with core concepts likely to be covered in interviews. Good luck!