1Z0-811 Core Java API 1 — Questions and Answers
Question 1: What is the output of the following code? import java.util.*; <br> public class Test { <br> public static void main(String[] args) { <br> List<String> list = Arrays.asList("A", "B", "C"); <br> list.add("D"); <br> } <br> }
- Compilation error (Correct answer)
- Runtime error
- The list will contain ["A", "B", "C", "D"]
- The list will contain ["A", "B", "C"] and "D" will not be added
Correct 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.
Question 2: Which interface in the Java Collections Framework represents a collection of elements that allows for duplicates and maintains insertion order?
- Set
- Queue
- List (Correct answer)
- Map
Correct 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.
Question 3: Which of the following classes is part of the Java Collections Framework and provides constant-time performance for the basic operations?
- LinkedList
- ArrayList
- HashSet (Correct answer)
- TreeSet
Correct 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.
Question 4: How does the Java Streams API improve the processing of collections?
- By allowing operations to be performed only sequentially
- By enabling parallel processing of elements (Correct answer)
- By modifying the underlying collection directly
- By storing elements in sorted order
Correct 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.
Question 5: What does the "java.util.concurrent" package primarily provide?
- Utilities for file and I/O operations
- Utilities for network communication
- Utilities for concurrent programming and synchronization (Correct answer)
- Utilities for database access
Correct 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.
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");
}
}