Top 50 Java Interview Questions for
Freshers
Basic Java Concepts (Questions 1-15)
1. What is Java?
Java is a high-level, object-oriented programming language developed by Sun Microsystems
(now Oracle). It follows the principle "Write Once, Run Anywhere" (WORA) because Java
code is compiled into bytecode that can run on any platform with a Java Virtual Machine
(JVM).
2. What are the main features of Java?
● Platform Independent: Code runs on any OS with JVM
● Object-Oriented: Based on objects and classes
● Simple: Easy syntax, automatic memory management
● Secure: Built-in security features
● Robust: Strong memory management, exception handling
● Multithreaded: Supports concurrent execution
3. What is JVM, JRE, and JDK?
● JVM (Java Virtual Machine): Runtime environment that executes Java bytecode
● JRE (Java Runtime Environment): JVM + libraries needed to run Java applications
● JDK (Java Development Kit): JRE + development tools (compiler, debugger)
4. What is bytecode in Java?
Bytecode is platform-independent intermediate code generated when Java source code is
compiled. It's stored in .class files and executed by the JVM. This allows Java programs to
run on any system with a JVM installed.
5. What is the difference between JDK, JRE, and JVM?
JDK is for development (includes compiler), JRE is for running Java applications (includes
JVM + libraries), and JVM is the runtime engine that executes bytecode. JDK contains JRE,
and JRE contains JVM.
6. What are the different types of memory areas allocated by JVM?
● Heap Memory: Stores objects and instance variables
● Stack Memory: Stores method calls and local variables
● Method Area: Stores class-level data like method definitions
● PC Register: Tracks currently executing instruction
● Native Method Stack: For native method calls
7. What is the difference between == and equals() method?
● == operator: Compares memory addresses (reference comparison)
● equals() method: Compares actual content/values of objects
String s1 = new String("Hello");
String s2 = new String("Hello");
System.out.println(s1 == s2); // false (different objects)
System.out.println(s1.equals(s2)); // true (same content)
8. What is the difference between String, StringBuffer, and
StringBuilder?
● String: Immutable, thread-safe, creates new object for each modification
● StringBuffer: Mutable, thread-safe (synchronized), better for multi-threading
● StringBuilder: Mutable, not thread-safe, faster performance in single-threaded
environment
9. What is autoboxing and unboxing?
● Autoboxing: Automatic conversion from primitive to wrapper class (int to Integer)
● Unboxing: Automatic conversion from wrapper class to primitive (Integer to int)
Integer i = 5; // Autoboxing
int j = i; // Unboxing
10. What are wrapper classes in Java?
Wrapper classes provide object representation for primitive data types. They allow primitives
to be used where objects are required (like collections).
● int → Integer
● double → Double
● boolean → Boolean
● char → Character
11. What is the difference between final, finally, and finalize?
● final: Keyword for constants, preventing inheritance, method overriding
● finally: Block that always executes after try-catch, used for cleanup
● finalize(): Method called by garbage collector before object destruction
12. What is static keyword in Java?
Static belongs to the class rather than instances. Static variables are shared among all
objects, static methods can be called without creating objects, and static blocks execute
when class is first loaded.
13. Can we override static methods in Java?
No, static methods cannot be overridden because they belong to the class, not the instance.
However, they can be hidden (method hiding) if a subclass defines a static method with the
same signature.
14. What is method overloading?
Method overloading means having multiple methods with the same name but different
parameters (number, type, or order). The compiler determines which method to call based
on the arguments provided.
15. What is method overriding?
Method overriding occurs when a subclass provides a specific implementation of a method
already defined in its parent class. The method signature must be identical, and it enables
runtime polymorphism.
Object-Oriented Programming (Questions 16-25)
16. What are the four pillars of OOP?
● Encapsulation: Bundling data and methods, hiding internal details
● Inheritance: Creating new classes based on existing ones
● Polymorphism: One interface, multiple implementations
● Abstraction: Hiding complex implementation details, showing only essential features
17. What is inheritance in Java?
Inheritance allows a class to acquire properties and methods from another class. The child
class (subclass) inherits from the parent class (superclass) using the extends keyword.
18. What are the types of inheritance in Java?
Java supports:
● Single Inheritance: One class extends one class
● Multilevel Inheritance: Chain of inheritance (A→B→C)
● Hierarchical Inheritance: Multiple classes inherit from one parent
Java doesn't support multiple inheritance with classes but supports it through interfaces.
19. What is polymorphism in Java?
Polymorphism allows objects of different classes to be treated as objects of a common base
class. It's achieved through method overriding (runtime polymorphism) and method
overloading (compile-time polymorphism).
20. What is encapsulation?
Encapsulation is wrapping data and methods into a single unit (class) and controlling access
through access modifiers (private, protected, public). It promotes data hiding and security.
21. What is abstraction in Java?
Abstraction hides implementation details and shows only essential features. It's achieved
using abstract classes and interfaces. For example, you know a car has start() method but
don't need to know how the engine works internally.
22. What is the difference between abstract class and interface?
● Abstract Class: Can have both abstract and concrete methods, constructors,
instance variables, supports single inheritance
● Interface: Only abstract methods (before Java 8), no constructors, variables are
public static final, supports multiple inheritance
23. What is constructor in Java?
A constructor is a special method that initializes objects when they're created. It has the
same name as the class, no return type, and is automatically called during object creation.
24. What are the types of constructors?
● Default Constructor: No parameters, provided by compiler if not defined
● Parameterized Constructor: Takes parameters to initialize object with specific
values
● Copy Constructor: Creates object by copying another object (not built-in in Java)
25. What is constructor chaining?
Constructor chaining is calling one constructor from another using this() (same class) or
super() (parent class). It must be the first statement in the constructor.
Exception Handling (Questions 26-30)
26. What is exception handling in Java?
Exception handling is a mechanism to handle runtime errors gracefully without terminating
the program abruptly. It uses try-catch-finally blocks to catch and handle exceptions.
27. What is the difference between checked and unchecked exceptions?
● Checked Exceptions: Must be handled at compile time (IOException,
SQLException)
● Unchecked Exceptions: Runtime exceptions, not mandatory to handle
(NullPointerException, ArrayIndexOutOfBoundsException)
28. What is the difference between throw and throws?
● throw: Used to explicitly throw an exception within a method
● throws: Used in method signature to declare that method might throw exceptions
29. What is try-catch-finally block?
● try: Contains code that might throw exceptions
● catch: Handles specific exceptions
● finally: Always executes, used for cleanup code (closing files, connections)
30. Can we have multiple catch blocks?
Yes, we can have multiple catch blocks to handle different types of exceptions. The order
should be from specific to general exceptions, as Java checks them sequentially.
Collections Framework (Questions 31-40)
31. What is the Collections Framework in Java?
The Collections Framework is a unified architecture for storing and manipulating groups of
objects. It includes interfaces (List, Set, Map), implementations (ArrayList, HashMap), and
algorithms (sorting, searching).
32. What is the difference between Array and ArrayList?
● Array: Fixed size, can store primitives and objects, faster access
● ArrayList: Dynamic size, only stores objects, provides utility methods, part of
Collections Framework
33. What is the difference between ArrayList and LinkedList?
● ArrayList: Uses dynamic array, fast random access, slower insertion/deletion in
middle
● LinkedList: Uses doubly linked list, slower random access, faster insertion/deletion
34. What is the difference between HashSet and TreeSet?
● HashSet: Uses hash table, no ordering, O(1) operations, allows null
● TreeSet: Uses Red-Black tree, sorted order, O(log n) operations, doesn't allow null
35. What is the difference between HashMap and Hashtable?
● HashMap: Not synchronized, allows null values, introduced in Java 1.2
● Hashtable: Synchronized (thread-safe), doesn't allow null, legacy class
36. What is the difference between HashMap and ConcurrentHashMap?
● HashMap: Not thread-safe, better performance in single-threaded environment
● ConcurrentHashMap: Thread-safe, uses segment-based locking, better
performance in multi-threaded environment than Hashtable
37. What is the difference between List and Set?
● List: Allows duplicates, maintains insertion order, indexed access
● Set: No duplicates, may or may not maintain order, no indexed access
38. What is Iterator in Java?
Iterator is an interface that provides methods to traverse collections sequentially. It has
methods like hasNext(), next(), and remove(). It's safer than traditional loops for collection
traversal.
39. What is the difference between Iterator and ListIterator?
● Iterator: Works with all collections, unidirectional, read and remove only
● ListIterator: Works only with List, bidirectional, read, write, and remove operations
40. What is comparable and comparator interface?
● Comparable: Interface implemented by class to define natural ordering, has
compareTo() method
● Comparator: External interface to define custom ordering, has compare() method
Multithreading (Questions 41-45)
41. What is multithreading in Java?
Multithreading allows concurrent execution of multiple threads within a program. Each thread
represents a separate path of execution, enabling programs to perform multiple tasks
simultaneously.
42. What is the difference between process and thread?
● Process: Independent program with separate memory space, heavy-weight
● Thread: Light-weight subprocess sharing memory space with other threads in the
same process
43. How can you create a thread in Java?
Two ways:
1. Extending Thread class: Create class extending Thread, override run() method
2. Implementing Runnable interface: Implement Runnable, define run() method, pass
to Thread constructor
44. What is synchronization in Java?
Synchronization is a mechanism to control access to shared resources in multithreaded
environment. It prevents thread interference and memory consistency errors using
synchronized keyword or locks.
45. What is the difference between wait() and sleep()?
● wait(): Object class method, releases lock, used for inter-thread communication
● sleep(): Thread class method, doesn't release lock, pauses thread execution for
specified time
Advanced Topics (Questions 46-50)
46. What is Java Memory Model?
Java Memory Model defines how threads interact through memory and what behaviors are
allowed in concurrent execution. It ensures consistency across different platforms and
defines happens-before relationships.
47. What is garbage collection in Java?
Garbage Collection is automatic memory management that reclaims memory used by
objects that are no longer reachable. It prevents memory leaks and reduces manual memory
management burden.
48. What are access modifiers in Java?
● private: Accessible only within the same class
● default (package-private): Accessible within the same package
● protected: Accessible within package and subclasses
● public: Accessible from anywhere
49. What is the difference between shallow copy and deep copy?
● Shallow Copy: Copies object but not the objects it references (references are
shared)
● Deep Copy: Copies object and all objects it references recursively (completely
independent copy)
50. What is reflection in Java?
Reflection is a feature that allows inspection and modification of classes, methods, and fields
at runtime. It enables dynamic behavior like loading classes, calling methods, and accessing
fields without knowing them at compile time.
Tips for Interview Success:
1. Practice coding: Be ready to write simple programs
2. Understand concepts: Don't just memorize, understand the logic
3. Use examples: Always provide code examples when explaining
4. Ask questions: Show your curiosity and interest
5. Be honest: If you don't know something, admit it and show willingness to learn
Remember to speak clearly, maintain confidence, and relate concepts to real-world
scenarios when possible. Good luck with your interview!