List Manipulation
In Java, how can you efficiently remove all occurrences of a specific element from a List?
- Using a for loop and removing elements by index.
- Using the removeAll() method with a collection containing the element.
- Using the clear() method and adding back the elements that are not the target element.
- Using the filter() method from the Streams API and collecting the result.
- Using the replaceAll() method with null.
Interface Evolution
Why were default methods introduced in interfaces in Java 8?
- To allow interfaces to have state.
- To enforce abstract class behavior on interfaces.
- To facilitate the evolution of interfaces without breaking existing implementations.
- To improve compile-time performance.
- To enable multiple inheritance of implementation.
Java 8 Interface Enhancements
Besides default methods, what other significant change was introduced to interfaces in Java 8?
- Static methods in interfaces.
- Private methods in interfaces.
- Abstract classes within interfaces.
- Sealed interfaces.
- Protected methods in interfaces.
Finding the Missing Number
Suppose you have a list of consecutive integers with one number missing. Which approach is most efficient for finding the missing number?
- Iterating through the list and checking for gaps.
- Sorting the list and then iterating to find the gap.
- Calculating the expected sum of the sequence and subtracting the actual sum of the list.
- Using a HashSet to check for the presence of each number in the expected range.
- Calculating the product of all numbers and dividing it by the product of numbers in the list.
Design Pattern Usage
Which design pattern promotes loose coupling and defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically?
- Factory Pattern
- Singleton Pattern
- Observer Pattern
- Strategy Pattern
- Template Method Pattern
Factory Design Pattern
What is the primary purpose of the Factory design pattern?
- To ensure only one instance of a class is created.
- To define a family of algorithms, encapsulate each one, and make them interchangeable.
- To provide an interface for creating families of related or dependent objects without specifying their concrete classes.
- To define the skeleton of an algorithm in an operation, deferring some steps to subclasses.
- To define a high-level interface that makes a subsystem easier to use.
Stream API Methods
Which Stream API method can be used to transform each element of a stream to another value?
- filter()
- reduce()
- map()
- sorted()
- collect()
Handling NullPointerException
What is the most effective strategy for preventing NullPointerException issues in Java?
- Catching NullPointerException using try-catch blocks.
- Relying solely on code reviews.
- Using assertions extensively.
- Adopting defensive programming practices, like null checks and Optional class.
- Increasing the JVM's memory allocation.
Immutable Objects
What characteristic defines an immutable object in Java?
- It can have its state modified after creation.
- It must extend the java.lang.Immutable class.
- It must use final methods exclusively.
- Its state cannot be modified after it is created.
- It can only be created using a builder pattern.
Method Overriding
When overriding a method in a subclass, which access modifier cannot be weaker than the access modifier of the overridden method in the superclass?
- private
- protected
- public
- default
- final