1Z0-819 Core APIs & Data Manipulation — Questions and Answers
Question 1: Which Java package is primarily used for collections?
- java.lang
- java.util (Correct answer)
- java.io
- java.net
Correct answer: java.util
The `java.util` package is the primary location for Java's Collections Framework, which includes interfaces and classes for storing and manipulating groups of objects. This package contains essential data structures like `List`, `Set`, `Map`, `ArrayList`, `HashMap`, and `HashSet`, among many others. It also provides utility classes for various common programming tasks.
Question 2: What does the List interface provide?
- Sorted elements with no duplicates.
- An unordered collection of unique elements.
- Ordered collection that allows duplicates (Correct answer)
- Randomized collection of values.
Correct answer: Ordered collection that allows duplicates
The `List` interface in Java represents an ordered collection of elements, meaning elements are stored and accessed by their integer index. Unlike `Set`s, `List`s explicitly allow duplicate elements. Implementations like `ArrayList` and `LinkedList` maintain the insertion order of elements.
Question 3: How does the HashMap store data?
- Sequentially with index values.
- In a tree structure.
- Using key-value pairs (Correct answer)
- As a continuous memory block.
Correct answer: Using key-value pairs
A `HashMap` in Java stores data as a collection of key-value pairs. Each key is unique and maps to a specific value. It uses a hashing mechanism to efficiently store and retrieve these pairs, providing fast average-case performance for operations like `put` and `get`.
Question 4: What is the function of the Stream API?
- To build user interfaces.
- To create threads.
- To process collections with functional-style operations (Correct answer)
- To handle network connections.
Correct answer: To process collections with functional-style operations
The Java Stream API, introduced in Java 8, provides a powerful and declarative way to process sequences of elements, such as collections or arrays. It enables functional-style operations like filtering, mapping, and reducing data, often in a parallelizable manner. Streams do not store data themselves but rather operate on data sources to produce a result.
Question 5: Which method is used to add an element to a List?
- insert()
- append()
- add() (Correct answer)
- push()
Correct answer: add()
The standard method used to add an element to a `List` in Java is `add()`. This method appends the specified element to the end of the list. There are also overloaded versions of `add()` that allow inserting an element at a specific index.
Question 6: What is the default sorting method in Java?
- Random sort.
- Lexicographical order.
- Natural order using Comparable (Correct answer)
- Reverse alphabetical.
Correct answer: Natural order using Comparable
When sorting objects in Java using methods like `Collections.sort()` or `Arrays.sort()` without a custom `Comparator`, the default sorting mechanism is based on the 'natural order' of the elements. This natural order is defined by the `compareTo()` method of the `Comparable` interface, which the objects must implement. For primitive types and `String`s, natural order is built-in.
Question 7: Which class is used to manipulate strings?
- StringBuffer
- String
- StringBuilder (Correct answer)
- CharSequence
Correct answer: StringBuilder
While `String` objects are immutable in Java, `StringBuilder` (and `StringBuffer`) classes are designed for mutable string manipulation. `StringBuilder` allows efficient modification, appending, and insertion of characters without creating new `String` objects for each change. It is generally preferred over `StringBuffer` in single-threaded environments due to better performance.
Question 8: What is the result of calling size() on an ArrayList?
- Returns -1.
- Returns the array length.
- Returns the number of stored elements (Correct answer)
- Returns the memory usage.
Correct answer: Returns the number of stored elements
Calling the `size()` method on an `ArrayList` (or any `Collection` implementation) returns the current number of elements actually stored within that collection. This is distinct from the internal array's capacity or length, which might be larger than the number of elements. The `size()` method accurately reflects the logical count of items.
Question 9: How do you remove an element from a List by index?
- delete(index)
- remove(index) (Correct answer)
- pop(index)
- unset(index)
Correct answer: remove(index)
To remove an element from a `List` at a specific position, the `remove(int index)` method is used. This method removes the element at the specified index and shifts any subsequent elements to the left. It also returns the element that was removed from the list.
Which Java package is primarily used for collections?