The Ultimate Java Knowledge Quiz Quiz

  1. List Manipulation

    In Java, how can you efficiently remove all occurrences of a specific element from a List?

    1. Using a for loop and removing elements by index.
    2. Using the removeAll() method with a collection containing the element.
    3. Using the clear() method and adding back the elements that are not the target element.
    4. Using the filter() method from the Streams API and collecting the result.
    5. Using the replaceAll() method with null.
  2. Interface Evolution

    Why were default methods introduced in interfaces in Java 8?

    1. To allow interfaces to have state.
    2. To enforce abstract class behavior on interfaces.
    3. To facilitate the evolution of interfaces without breaking existing implementations.
    4. To improve compile-time performance.
    5. To enable multiple inheritance of implementation.
  3. Java 8 Interface Enhancements

    Besides default methods, what other significant change was introduced to interfaces in Java 8?

    1. Static methods in interfaces.
    2. Private methods in interfaces.
    3. Abstract classes within interfaces.
    4. Sealed interfaces.
    5. Protected methods in interfaces.
  4. 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?

    1. Iterating through the list and checking for gaps.
    2. Sorting the list and then iterating to find the gap.
    3. Calculating the expected sum of the sequence and subtracting the actual sum of the list.
    4. Using a HashSet to check for the presence of each number in the expected range.
    5. Calculating the product of all numbers and dividing it by the product of numbers in the list.
  5. 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?

    1. Factory Pattern
    2. Singleton Pattern
    3. Observer Pattern
    4. Strategy Pattern
    5. Template Method Pattern
  6. Factory Design Pattern

    What is the primary purpose of the Factory design pattern?

    1. To ensure only one instance of a class is created.
    2. To define a family of algorithms, encapsulate each one, and make them interchangeable.
    3. To provide an interface for creating families of related or dependent objects without specifying their concrete classes.
    4. To define the skeleton of an algorithm in an operation, deferring some steps to subclasses.
    5. To define a high-level interface that makes a subsystem easier to use.
  7. Stream API Methods

    Which Stream API method can be used to transform each element of a stream to another value?

    1. filter()
    2. reduce()
    3. map()
    4. sorted()
    5. collect()
  8. Handling NullPointerException

    What is the most effective strategy for preventing NullPointerException issues in Java?

    1. Catching NullPointerException using try-catch blocks.
    2. Relying solely on code reviews.
    3. Using assertions extensively.
    4. Adopting defensive programming practices, like null checks and Optional class.
    5. Increasing the JVM's memory allocation.
  9. Immutable Objects

    What characteristic defines an immutable object in Java?

    1. It can have its state modified after creation.
    2. It must extend the java.lang.Immutable class.
    3. It must use final methods exclusively.
    4. Its state cannot be modified after it is created.
    5. It can only be created using a builder pattern.
  10. 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?

    1. private
    2. protected
    3. public
    4. default
    5. final