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.
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?
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.
What Streams API operation would you use to extract only the even numbers from a list of integers?
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.
Which Java class or interface must be implemented or extended to define a new thread of execution for a background task processing system?
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.
What is the correct syntax to declare a generic method that accepts a List of any type and prints its size?
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.
When would you use the @Override annotation in Java?
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.
Which operation is possible using Java reflection, such as dynamically checking the methods available in a class at runtime?
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.
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?
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.
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?
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.
If you want to double every element in a list of integers using the Streams API, which operation should you use?
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.
Which Java service provides management of a pool of threads for executing tasks efficiently, such as using several threads to process file uploads simultaneously?
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.