Core Java API Flashcards
5 cards from real 1Z0-811 practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 5 Core Java API flashcards as text
What is the output of the following code? import java.util.*; public class Test { public static void main(String[] args) { List list = Arrays.asList("A", "B", "C"); list.add("D"); } }
Answer: Compilation error
The `Arrays.asList()` method returns a fixed-size `List` backed by the original array. This list does not support structural modifications such as adding or removing elements. Attempting to call the `add()` method on this type of list will result in an `UnsupportedOperationException` at runtime, not a compilation error, because the `add` method is syntactically valid but semantically unsupported by the specific list implementation.
Which interface in the Java Collections Framework represents a collection of elements that allows for duplicates and maintains insertion order?
Answer: List
The `List` interface in the Java Collections Framework represents an ordered collection (sequence) of elements. It explicitly allows for duplicate elements and maintains the insertion order of elements. In contrast, `Set` does not allow duplicates, `Queue` maintains order for processing, and `Map` stores key-value pairs.
Which of the following classes is part of the Java Collections Framework and provides constant-time performance for the basic operations?
Answer: HashSet
The `HashSet` class provides constant-time performance (on average) for basic operations like `add`, `remove`, and `contains`. This efficiency is achieved by using a hash table for storage. `LinkedList`, `ArrayList`, and `TreeSet` have different performance characteristics, typically involving O(n) or O(log n) for some operations.
How does the Java Streams API improve the processing of collections?
Answer: By enabling parallel processing of elements
The Java Streams API significantly improves collection processing by enabling parallel execution of operations. By calling `parallelStream()`, developers can easily process elements concurrently across multiple CPU cores, leading to performance gains for large datasets. Streams also promote a functional programming style and do not modify the underlying collection directly.
What does the "java.util.concurrent" package primarily provide?
Answer: Utilities for concurrent programming and synchronization
The `java.util.concurrent` package is specifically designed to provide a rich set of utilities for concurrent programming in Java. It includes classes for managing threads, thread pools, concurrent data structures (like `ConcurrentHashMap`), and synchronization aids (like `Semaphore` and `CountDownLatch`), making it easier to write robust multi-threaded applications.