Core APIs & Data Manipulation Flashcards
9 cards from real 1Z0-819 practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 9 Core APIs & Data Manipulation flashcards as text
Which Java package is primarily used for collections?
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.
What does the List interface provide?
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.
How does the HashMap store data?
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`.
What is the function of the Stream API?
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.
Which method is used to add an element to a List?
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.
What is the default sorting method in Java?
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.
Which class is used to manipulate strings?
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.
What is the result of calling size() on an ArrayList?
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.
How do you remove an element from a List by index?
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.