Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
54 views16 pages

Java 50 Ques

The document provides a comprehensive overview of various Java concepts, including differences between operators, design patterns, data structures, and exception handling. Key topics include the distinctions between '==' and '.equals()', abstraction vs encapsulation, and the use of the volatile keyword. It also covers advanced topics like connection pooling, garbage collection, and the differences between checked and unchecked exceptions.

Uploaded by

shaikh Rizwan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views16 pages

Java 50 Ques

The document provides a comprehensive overview of various Java concepts, including differences between operators, design patterns, data structures, and exception handling. Key topics include the distinctions between '==' and '.equals()', abstraction vs encapsulation, and the use of the volatile keyword. It also covers advanced topics like connection pooling, garbage collection, and the differences between checked and unchecked exceptions.

Uploaded by

shaikh Rizwan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

1. What’s the difference between == and .equals() in Java?

Answer:
●​ == compares object references (memory addresses).
●​ .equals() compares object content (logical equality).

2. What is the difference between abstraction and encapsulation?


Answer:
●​ Abstraction hides implementation details and shows only functionality.
●​ Encapsulation binds data and methods together and restricts access.

3. What is a deadlock and how can it be prevented?


Answer:
Deadlock occurs when two or more threads are waiting for each other’s resources indefinitely.

Prevention Techniques:
●​ Avoid nested locks.
●​ Use timeout for locks.
●​ Lock ordering.
4. What is the use of volatile keyword?
Answer:
It ensures visibility of changes to variables across threads.

Without volatile, one thread might not see the updated value of flag.

5. What is the difference between throw and throws?


Answer:
●​ throw is used to explicitly throw an exception.
●​ throws declares exceptions a method might throw.

6. What is the difference between ArrayList and LinkedList?


Answer:
●​ ArrayList is faster for random access.
●​ LinkedList is better for frequent insertions/deletions.
7. What is the role of final, finally, and finalize()?
Answer:
●​ final: constant or non-overridable.
●​ finally: block that always executes.
●​ finalize(): called before garbage collection.
8. What is the Builder Pattern?
Answer:
Used to construct complex objects step-by-step.

9. What is the difference between map() and flatMap() in Java Streams?


Answer:
●​ map() transforms each element.
●​ flatMap() flattens nested structures.
10. What is connection pooling?
Answer:
It reuses database connections to improve performance.

11. How does HashMap work internally?


Answer:
HashMap uses an array of buckets. Each bucket is a linked list or tree (after Java 8). It stores
key-value pairs using the hashCode of the key.

Behind the scenes, "name".hashCode() determines the bucket index.

12. What is the difference between method overloading and overriding?


Answer:
●​ Overloading: Same method name, different parameters (compile-time).
●​ Overriding: Same method signature in subclass (runtime).
13. What is the difference between Comparable and Comparator?
Answer:
●​ Comparable: Natural ordering, implemented in the class.
●​ Comparator: Custom ordering, defined externally.

14. What is try-with-resources?


Answer:
It automatically closes resources like streams or connections.

15. What is the difference between map() and flatMap() in Streams?


Answer:
●​ map(): Transforms each element.
●​ flatMap(): Flattens nested structures.
16. What is immutability in Java?
Answer:
Immutable objects cannot be changed after creation. Helps with thread safety.

17. What is the Singleton Pattern?


Answer:
Ensures only one instance of a class exists.

18. What is the role of JVM in Java?


Answer:
JVM executes Java bytecode and provides platform independence.
Example:
Java source → .class bytecode → JVM interprets → Machine code

19. What is garbage collection?


Answer:
Automatic memory cleanup of unused objects.
20. What is the difference between HashSet and TreeSet?
Answer:
●​ HashSet: Unordered, faster.
●​ TreeSet: Sorted, slower.

21. What is the difference between synchronized method and block?


Answer:
●​ synchronized method locks the entire method.
●​ synchronized block locks only a portion of code, offering better performance.

22. What is the role of Optional in Java?


Answer:
It helps avoid NullPointerException by wrapping values that may be null.

23. What is fail-fast vs fail-safe iterator?


Answer:
●​ Fail-fast throws ConcurrentModificationException.
●​ Fail-safe works on a cloned copy.
24. What is the difference between checked and unchecked exceptions?
Answer:
●​ Checked: Must be handled (e.g., IOException).
●​ Unchecked: Runtime exceptions (e.g., NullPointerException).

25. What is the use of Stream API?


Answer:
Simplifies collection processing with functional-style operations.

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


Answer:
●​ final: Prevents modification.
●​ finally: Executes after try-catch.
●​ finalize(): Called before GC.
27. What is the Factory Pattern?
Answer:
Creates objects without exposing instantiation logic.

28. What is serialization in Java?


Answer:
Converts object state to byte stream for storage or transmission.

29. What is the difference between throw and throws?


Answer:
●​ throw: Used to throw an exception.
●​ throws: Declares exceptions a method might throw.

30. What is the role of ExecutorService?


Answer:
Manages thread pools and simplifies concurrent execution.
31. What are functional interfaces in Java?
Answer:
A functional interface has exactly one abstract method and can be used with lambda
expressions.

32. What is the Strategy Pattern?


Answer:
It allows selecting an algorithm at runtime by encapsulating behaviors.

33. What is the difference between LinkedHashMap and TreeMap?


Answer:
●​ LinkedHashMap: Maintains insertion order.
●​ TreeMap: Maintains sorted order based on keys.
34. What is the Observer Pattern?
Answer:
It defines a one-to-many dependency between objects so that when one changes, all
dependents are notified.

35. What is the difference between static and instance methods?


Answer:
●​ static: Belongs to the class, no object needed.
●​ instance: Belongs to object, requires instantiation.

36. How to prevent SQL injection in Java?


Answer:
Use PreparedStatement instead of string concatenation.

PreparedStatement ps = conn.prepareStatement("SELECT * FROM users WHERE name = ?");


ps.setString(1, "Venkat");

37. What is the difference between StringBuilder and StringBuffer?


Answer:
●​ StringBuilder: Not thread-safe, faster.
●​ StringBuffer: Thread-safe, slower.
38. What is dynamic proxy in Java?
Answer:
Allows creation of proxy instances at runtime using reflection.

39. What is the difference between List.of() and Arrays.asList()?


Answer:
●​ List.of(): Immutable list.
●​ Arrays.asList(): Fixed-size list backed by array.

40. What is the role of annotations in Java?


Answer:
Annotations provide metadata to the compiler and runtime.

41. What is the difference between interface and abstract class?


Answer:
●​ Interface: All methods are abstract by default (Java 8+ allows default/static methods).
●​ Abstract class: Can have both abstract and concrete methods.
42. What is the difference between == and equals() for objects?
Answer:
●​ ==: Compares memory addresses.
●​ equals(): Compares logical content.

43. What is the difference between HashMap and Hashtable?


Answer:
●​ HashMap: Not synchronized, allows null keys/values.
●​ Hashtable: Synchronized, doesn’t allow nulls.

44. What is the difference between throw and throws?


Answer:
●​ throw: Used to actually throw an exception.
●​ throws: Declares possible exceptions.

45. What is the difference between static and final?


Answer:
●​ static: Belongs to the class.
●​ final: Cannot be changed once assigned.
46. What is connection pooling?
Answer:
Connection pooling reuses database connections to improve performance and reduce
overhead.

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


Answer:
●​ JDK: Java Development Kit (compiler + tools + JRE).
●​ JRE: Java Runtime Environment (libraries + JVM).
●​ JVM: Executes bytecode.

48. What is the difference between synchronized and Lock?


Answer:
●​ synchronized: Simpler, implicit locking.
●​ Lock: More flexible, explicit control.

49. What is the difference between Stack and Queue?


Answer:
●​ Stack: LIFO (Last In First Out).
●​ Queue: FIFO (First In First Out).
50. What is the difference between deep copy and shallow copy?
Answer:
●​ Shallow copy: Copies references.
●​ Deep copy: Copies actual objects recursively.

You might also like