15. What is the difference between constructor and method?
Answer:
Constructor:
Same name as class.
No return type.
Called automatically when object is created.
Method:
Can have any name.
Must have a return type (or void).
Called manually.
16. What is this keyword?
Answer:
this refers to the current object.
Used to:
Differentiate between instance variables and parameters.
Call other constructors in the same class.
17. What is static keyword?
Answer:
static means belongs to the class, not to objects.
Used for:
Static variables (class level)
Static methods (can be called without object)
Static blocks
18. What is inheritance? Types of inheritance in Java?
Answer:
Inheritance allows one class (child) to reuse properties and methods of another class (parent).
Types supported in Java:
Single
Multilevel
Hierarchical
(Java does not support multiple inheritance with classes, only with interfaces.)
19. What is polymorphism? Compile-time vs runtime.
Answer:
Polymorphism: Same name, different behavior.
Compile-time polymorphism: Method overloading.
Runtime polymorphism: Method overriding (decided at runtime).
20. What is method overloading and method overriding?
Answer:
Overloading: Same method name, different parameters in the same class.
Overriding: Subclass provides its own implementation of a method from the parent class (same signature).
21. What is encapsulation?
Answer:
Encapsulation means wrapping data and methods together and hiding internal details using private variables and public getters/setters.
22. What is abstraction? Abstract class vs interface.
Answer:
Abstraction hides implementation details and shows only functionality.
Abstract class:
Can have abstract and non-abstract methods.
Can have variables, constructors.
Interface:
Only abstract methods (till Java 7), plus default/static methods (Java 8+).
Used to define a contract.
23. Difference between String, StringBuilder, and StringBuffer?
Answer:
String: Immutable (cannot be changed).
StringBuilder: Mutable, not thread-safe, faster.
StringBuffer: Mutable, thread-safe, slower than StringBuilder.
24. Why are Strings immutable in Java?
Answer:
To provide security, caching, and thread safety. Since Strings are widely used (like in class loading, URLs), immutability makes them safe and reliable.
25. What is an array? Limitations of arrays?
Answer:
Array is a fixed-size collection of elements of the same type.
Limitations:
Fixed size (cannot grow or shrink).
Only one data type.
No built-in methods like add/remove/search (use Collections for that).
26. What is ArrayList vs LinkedList?
Answer:
ArrayList:
Backed by array.
Fast for read/search.
Slow for insert/delete in middle.
LinkedList:
Doubly linked list.
Fast for insert/delete.
Slower for random access.
27. Difference between List, Set, and Map?
Answer:
List: Ordered, allows duplicates (ArrayList, LinkedList).
Set: No duplicates (HashSet, LinkedHashSet, TreeSet).
Map: Key–value pairs, keys unique (HashMap, TreeMap).
28. What is HashMap? How does it work?
Answer:
HashMap stores data as key–value pairs.
It uses hashing to put entries into buckets based on the key’s hashcode for fast retrieval (O(1) average time).
29. What is an exception?
Answer:
An exception is an unexpected event that disrupts normal program flow, e.g., division by zero, file not found.
30. Difference between checked and unchecked exceptions?
Answer:
Checked exceptions: Checked at compile time (e.g., IOException). Must be handled with try-catch or throws.
Unchecked exceptions: Runtime exceptions (e.g., NullPointerException). Not forced by compiler.
31. What is try, catch, and finally?
Answer:
try: Block where you write code that may throw exception.
catch: Block to handle the exception.
finally: Block that always runs (optional), used for cleanup like closing files.
32. What is throw vs throws?
Answer:
throw: Used inside a method to explicitly throw an exception.
throws: Used in method signature to declare that the method might throw exception(s).
33. What is stack and heap memory?
Answer:
Stack: Stores local variables and method calls.
Heap: Stores objects and instance variables. Managed by garbage collector.
34. What is garbage collection in Java?
Answer:
Garbage collection automatically removes unused objects from heap memory to free space.
35. What is JVM architecture (high-level)?
Answer:
Main parts:
Class loader
Runtime data areas (stack, heap, method area)
Execution engine (interpreter, JIT compiler)
Native method interface & libraries
36. What is package? Why do we use it?
Answer:
A package is a group of related classes and interfaces.
Used to:
Organize code
Avoid name conflicts
Provide access control
37. What is access modifier? Types?
Answer:
Access modifiers decide visibility of classes, methods, variables.
Types:
public – accessible everywhere
protected – same package + subclasses
default (no keyword) – same package only
private – same class only
38. What are wrapper classes?
Answer:
Wrapper classes convert primitive types into objects.
Example: int → Integer, double → Double, boolean → Boolean.
39. What is autoboxing and unboxing?
Answer:
Autoboxing: Automatic conversion of primitive to wrapper, e.g., int → Integer.
Unboxing: Conversion of wrapper to primitive, e.g., Integer → int.
40. What is final, finally, and finalize?
Answer:
final: Keyword used with variable (constant), method (cannot override), class (cannot extend).
finally: Block in exception handling that always runs.
finalize(): Method called by garbage collector before object is destroyed (deprecated in modern Java).

No comments:
Post a Comment