java interview question answer for fresher

By: zigmoid
Posted on: 06/03/2025

1. What is Java?

Answer:
Java is a high-level, object-oriented programming language developed by Sun Microsystems. It’s platform-independent because of the JVM (Java Virtual Machine) — “write once, run anywhere.”


2. What is the difference between JDK, JRE, and JVM?

Answer:

  • JDK (Java Development Kit): Used to develop Java apps. Contains JRE + development tools.
  • JRE (Java Runtime Environment): Used to run Java apps. Contains JVM + libraries.
  • JVM (Java Virtual Machine): Executes Java bytecode, makes it platform-independent.

3. What are the main features of Java?

Answer:

  • Object-Oriented
  • Platform Independent
  • Simple and Secure
  • Robust and Portable
  • Multi-threaded
  • High Performance (thanks to JIT compiler)

4. What is the difference between == and .equals() in Java?

Answer:

  • == compares references (memory addresses)
  • .equals() compares actual values (contents)
javaCopyEditString a = new String("hello");
String b = new String("hello");
System.out.println(a == b);        // false
System.out.println(a.equals(b));   // true

5. What is a constructor in Java?

Answer:
A constructor is a special method that’s called when an object is created. It has the same name as the class and no return type.


6. What is the difference between ArrayList and LinkedList?

Answer:

  • ArrayList: Fast for searching, slow for inserting/deleting in the middle.
  • LinkedList: Fast for inserting/deleting, slower for searching.

7. What is method overloading and overriding?

Answer:

  • Overloading: Same method name, different parameters (in same class).
  • Overriding: Subclass provides specific implementation of a superclass method.

8. What are access modifiers in Java?

Answer:

  • private: Accessible within the class only.
  • default (no modifier): Accessible within the same package.
  • protected: Accessible within package and subclasses.
  • public: Accessible from anywhere.

9. What is the difference between final, finally, and finalize()?

Answer:

  • final: Keyword used to make a variable/method/class unchangeable.
  • finally: Block that always executes after try-catch, used for clean-up.
  • finalize(): Method called by GC before object is removed from memory.

10. What is inheritance in Java?

Answer:
Inheritance allows a class to inherit properties and methods from another class using the extends keyword.

javaCopyEditclass Animal {
  void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
  void bark() { System.out.println("Dog barks"); }
}

1. What is the difference between Java and C++?

Answer: Java is platform-independent and has automatic garbage collection. C++ is platform-dependent and requires manual memory management.


2. What is a class in Java?

Answer: A class is a blueprint for objects. It defines fields (variables) and methods.


3. What is an object?

Answer: An object is an instance of a class. It contains state (fields) and behavior (methods).


4. What are wrapper classes?

Answer: Java provides wrapper classes to use primitive data types as objects, e.g., int → Integer, char → Character.


5. What is autoboxing and unboxing?

Answer:

  • Autoboxing: Primitive → Object (int to Integer)
  • Unboxing: Object → Primitive (Integer to int)

6. What is a static variable?

Answer: A static variable belongs to the class, not to instances. Shared across all objects.


7. What is a static method?

Answer: A method that belongs to the class. It can be called without creating an object.


8. What is a final variable?

Answer: A final variable’s value can’t be changed once assigned.


9. What is an interface?

Answer: An interface is a contract — a collection of abstract methods that a class must implement.


10. What is an abstract class?

Answer: A class that can’t be instantiated. It can contain abstract and non-abstract methods.


11. Can Java support multiple inheritance?

Answer: Not with classes, but yes via interfaces.


12. What is polymorphism?

Answer: One interface, many forms. Achieved via method overloading and overriding.


13. What is encapsulation?

Answer: Wrapping data and code in a single unit. Achieved by using private variables and public getters/setters.


14. What is inheritance?

Answer: When one class inherits the properties of another using extends.


15. What is abstraction?

Answer: Hiding internal implementation details and showing only functionality.


16. What are the types of inheritance in Java?

Answer:

  • Single
  • Multilevel
  • Hierarchical
    (Java doesn’t support multiple inheritance via classes)

17. What is a package in Java?

Answer: A namespace that organizes classes and interfaces.


18. What is the default package in Java?

Answer: If you don’t define a package, the class belongs to the unnamed default package.


19. What is a constructor overloading?

Answer: When a class has multiple constructors with different parameter lists.


20. Can constructors be private?

Answer: Yes. Used in Singleton or Factory design patterns.


21. What is the super keyword?

Answer: Refers to the parent class. Can be used to access parent methods, variables, or constructors.


22. What is the this keyword?

Answer: Refers to the current class object.


23. What is a default constructor?

Answer: A constructor without parameters, created by compiler if not defined.


24. What is a parameterized constructor?

Answer: A constructor that accepts arguments.


25. What is garbage collection in Java?

Answer: JVM automatically deletes unused objects to free memory.


26. What is the purpose of System.gc()?

Answer: Suggests the JVM to run garbage collector (not guaranteed).


27. What is method overriding?

Answer: Subclass provides a specific implementation of a method already defined in its superclass.


28. What is method overloading?

Answer: Methods with same name but different parameters in the same class.


29. What is the difference between break and continue?

Answer:

  • break: exits loop
  • continue: skips current iteration

30. What is a StringBuffer and StringBuilder?

Answer:

  • StringBuffer: Thread-safe
  • StringBuilder: Not thread-safe, but faster

31. What’s the difference between String and StringBuilder?

Answer: String is immutable, StringBuilder is mutable.


32. What are exceptions in Java?

Answer: Abnormal conditions that disrupt program flow. Handled using try-catch blocks.


33. What’s the difference between checked and unchecked exceptions?

Answer:

  • Checked: Checked at compile time (e.g., IOException)
  • Unchecked: At runtime (e.g., NullPointerException)

34. What is the purpose of finally block?

Answer: It always executes whether an exception is thrown or not.


35. What is try-with-resources?

Answer: Introduced in Java 7, it automatically closes resources like FileReader, BufferedReader, etc.


36. What is the use of instanceof keyword?

Answer: Checks if an object is an instance of a specific class or subclass.


37. What are the types of memory areas in JVM?

Answer:

  • Heap
  • Stack
  • Code Area
  • Method Area (aka PermGen/MetaSpace)
  • Native Method Stack

38. What is the difference between throw and throws?

Answer:

  • throw: Used to explicitly throw an exception
  • throws: Declares that a method might throw an exception

39. What is synchronization?

Answer: Prevents multiple threads from accessing shared resources simultaneously.


40. What is a thread in Java?

Answer: A thread is a lightweight process. Java supports multithreading via the Thread class and Runnable interface.


41. How do you create a thread?

Answer:

  • Extend Thread class and override run()
  • Implement Runnable interface

42. What is multithreading?

Answer: Running multiple threads simultaneously to achieve multitasking.


43. What are daemon threads?

Answer: Background threads that die when the main thread finishes.


44. What is the difference between wait() and sleep()?

Answer:

  • wait(): Releases the lock, used with notify
  • sleep(): Holds the lock, pauses the thread

45. What is a volatile variable in Java?

Answer: Tells JVM to always read the variable from main memory. Ensures visibility in multi-threading.


46. What is the difference between Array and ArrayList?

Answer:

  • Array: Fixed size, faster
  • ArrayList: Dynamic size, part of Collections API

47. What is the Collections Framework?

Answer: A unified architecture for manipulating groups of objects like List, Set, Map, etc.


48. What is a HashMap?

Answer: A key-value pair collection with no guaranteed order. Allows one null key.


49. Difference between HashMap and HashTable?

Answer:

  • HashMap: Not synchronized, allows nulls
  • Hashtable: Synchronized, doesn’t allow null keys or values

50. What is the use of enum in Java?

Answer: A special class used to define collections of constants.

javaCopyEditenum Level {
  LOW, MEDIUM, HIGH
}

51. What is the use of transient keyword?

Answer: Prevents variables from being serialized.


52. What is Serializable interface?

Answer: Marker interface to allow object serialization — converting object state to byte stream.