Q91. What is the difference between Collection and Collections in Java?
Answer:
- Collection is a core interface in the Java Collection Framework that defines common behaviors for data structures like List, Set, and Queue.
- Collections is a utility class in java.util that offers static methods for operations such as sorting, reversing, shuffling, and synchronizing collections.
Example: Collections.sort(myList);
Keywords: Java Collection vs Collections, Java utility classes
Q92. What is the use of the finalize() method in Java?
Answer:
- The finalize() method is called by the Garbage Collector just before an object is removed from memory.
- Used to release system resources like file handles or database connections.
- Deprecated in Java 9, as it’s unpredictable and can delay garbage collection.
It’s recommended to use try-with-resources or explicit resource management instead.
Keywords: Java finalize method, garbage collection cleanup
Q93. What is Java 8 Optional and how is it used?
Answer:
- Optional<T> is a container object that may or may not hold a non-null value.
- Prevents NullPointerException by explicitly checking for presence.
- Common methods: of(), ofNullable(), isPresent(), orElse(), map().
java
Optional<String> name = Optional.ofNullable(user.getName());
System.out.println(name.orElse(“Anonymous”));
Keywords: Java Optional example, avoid null checks in Java
Q94. What is the Java Collection Framework?
Answer:
- A comprehensive architecture for storing and manipulating groups of objects.
- Includes interfaces like List, Set, Map, and classes like ArrayList, HashSet, HashMap.
- Supports sorting, searching, iteration, and thread-safe collections via Concurrent APIs.
Keywords: Java Collection Framework overview, Java data structures
Q95. What is CompletableFuture in Java 8?
Answer:
- CompletableFuture is part of Java’s asynchronous programming model.
- Enables non-blocking execution of tasks with methods like thenApply(), thenAccept(), and thenCompose().
- Internally uses ForkJoinPool for efficient thread management.
java
CompletableFuture.supplyAsync(() -> “Data”)
.thenApply(data -> data + ” processed”)
.thenAccept(System.out::println);
Keywords: Java CompletableFuture example, asynchronous programming Java
Q96. What is multithreading in Java?
Answer:
- Multithreading allows the parallel execution of tasks, improving performance and responsiveness.
- Threads can be created using:
- Thread class
- Runnable interface
- ExecutorService or ForkJoinPool
- Thread class
- Java provides synchronization tools to handle shared resources safely.
Keywords: Java multithreading tutorial, creating threads in Java
Q97. What is the purpose of the strictfp keyword in Java?
Answer:
- Ensures platform-independent floating-point calculations by enforcing IEEE 754 standards.
- Can be applied to classes or methods but not variables.
- Guarantees consistent results across different systems.
java
strictfp class Calculator { /* consistent float logic */ }
Keywords: strictfp keyword in Java, float consistency Java
Q98. What is the difference between sleep() and wait()?
Answer:
Feature | sleep() | wait() |
Lock Handling | Retains the lock | Releases the lock |
Belongs to | Thread class | Object class |
Wake Mechanism | Auto after timeout | Must be notified using notify() |
sleep() is used to pause execution, while wait() is used for thread communication.
Keywords: Java wait vs sleep, thread synchronization
Q99. Difference between LinkedHashSet and TreeSet?
Answer:
- LinkedHashSet maintains insertion order using a linked list.
- TreeSet maintains sorted order (natural or via Comparator).
- Performance:
- LinkedHashSet: O(1) for basic operations.
- TreeSet: O(log n) due to Red-Black Tree.
- LinkedHashSet: O(1) for basic operations.
- Nulls:
- LinkedHashSet: allows null.
- TreeSet: does not allow null.
- LinkedHashSet: allows null.
Keywords: LinkedHashSet vs TreeSet, Java Set types comparison
Q100. What is synchronization in Java?
Answer:
Synchronization is a concurrency control mechanism that ensures thread safety by restricting access to shared resources.
- Prevents race conditions.
- Achieved using the synchronized keyword:
- Synchronized blocks
- Synchronized methods
- Synchronized blocks
- Alternatives include:
- ReentrantLock
- volatile variables
- AtomicInteger, ConcurrentHashMap
- ReentrantLock
java
synchronized void update() {
// only one thread can enter
}
Keywords: Java synchronization, thread safety techniques