Advanced Java Concepts Quiz Quiz

Explore key topics from advanced Java, including Collections Framework, Streams API, Concurrency, Generics, Annotations, and Reflection. This quiz is designed to help learners understand important principles and best practices in advanced Java programming.

  1. Java Collections Framework Basics

    Which Java Collection type should you choose when you need to store unique values and maintain sorted order automatically, such as storing a set of unique usernames in alphabetical order?

    1. TreeSet
    2. ArrayList
    3. HashSet
    4. LinkedList

    Explanation: TreeSet stores unique values and automatically keeps them in sorted order, making it ideal for cases where sorted, non-duplicate data is required. ArrayList allows duplicates and does not provide automatic sorting. HashSet ensures uniqueness but does not maintain any order. LinkedList is not a Set and allows duplicates while maintaining insertion order.

  2. Using the Streams API

    What Streams API operation would you use to extract only the even numbers from a list of integers?

    1. filter
    2. map
    3. reduce
    4. groupBy

    Explanation: The filter operation selects elements that match a specified condition, such as even numbers from a list. The map operation transforms elements, reduce is used for aggregation, and groupBy is not a standard Streams API operation—it's a common concept but not a method in the official API.

  3. Working with Threads

    Which Java class or interface must be implemented or extended to define a new thread of execution for a background task processing system?

    1. Runnable
    2. Observable
    3. Iterable
    4. Comparable

    Explanation: Runnable is the interface that you implement to define a task for a new thread in Java. Observable is related to the observer pattern and not thread execution. Iterable allows object iteration, and Comparable is for sorting purposes, which are unrelated to threading.

  4. Java Generics Syntax

    What is the correct syntax to declare a generic method that accepts a List of any type and prints its size?

    1. public <T> void printSize(List<T> list)
    2. public void printSize<T>(List list)
    3. public void printSize(List<?> list)
    4. public <T> printSize(List)

    Explanation: The correct syntax for a generic method uses the type parameter <T> before the return type, such as public <T> void printSize(List<T> list). Using <T> after the method name or missing type parameters is incorrect. List<?> is valid for wildcards, but the question asks specifically for declaring a generic method.

  5. @Override Annotation Usage

    When would you use the @Override annotation in Java?

    1. When a subclass method redefines a parent class method
    2. When a variable is declared as final
    3. When creating an interface
    4. When importing a package

    Explanation: @Override is used to indicate that a method is intended to override a method in its superclass. Declaring a variable as final does not involve annotations. Creating interfaces and importing packages are unrelated to the use of @Override.

  6. Reflection Capabilities

    Which operation is possible using Java reflection, such as dynamically checking the methods available in a class at runtime?

    1. Inspecting class members at runtime
    2. Compiling source code
    3. Securing private data
    4. Scheduling tasks

    Explanation: Reflection allows inspection and manipulation of class members (fields, methods) at runtime. Compiling source code is done through compilers. Securing private data and scheduling tasks are unrelated to reflection, since reflection is about introspection, not security or task scheduling.

  7. HashMap Characteristics

    If you need a data structure to associate student IDs with their corresponding names, allowing for quick look-up by ID, which Java Collection should you use?

    1. HashMap
    2. LinkedList
    3. TreeSet
    4. ArrayList

    Explanation: HashMap stores key-value pairs, such as student IDs and names, and allows fast retrieval by key. LinkedList is designed for ordered data, not key-value association. TreeSet and ArrayList do not support key-value mapping and are not suitable for this purpose.

  8. Benefits of Generics

    Why should you use generics when defining a class or method to handle collections of data, such as a box that can store any type of object?

    1. To enable type safety and avoid runtime casting errors
    2. To reduce memory usage
    3. To allow code to run faster
    4. To generate random values automatically

    Explanation: Generics enforce type safety at compile time, preventing unintended types from being inserted and reducing the need for manual casting. They do not inherently reduce memory usage or runtime speed and have nothing to do with generating random values.

  9. Streams API: Transforming Elements

    If you want to double every element in a list of integers using the Streams API, which operation should you use?

    1. map
    2. filter
    3. findFirst
    4. skip

    Explanation: The map operation applies a transformation to each element, such as doubling its value. The filter operation selects elements, findFirst retrieves the first that matches a condition, and skip omits a number of elements from the beginning. Only map changes the original element values.

  10. Concurrency Handling

    Which Java service provides management of a pool of threads for executing tasks efficiently, such as using several threads to process file uploads simultaneously?

    1. ExecutorService
    2. FileReader
    3. ClassLoader
    4. ScheduledFuture

    Explanation: ExecutorService manages a pool of threads, allowing multiple tasks to run concurrently and simplifying thread management. FileReader is for reading files, not concurrency. ClassLoader is for loading classes, and ScheduledFuture is for scheduling tasks but is not itself a thread pool manager.