Java Real World Projects: A Pragmatic Guide for Building Modern Java Applications Flashcards
(28 cards)
What is the primary purpose of handling data structures in software development according to the sources?
a) To identify the most efficient and straightforward ways to handle data.
b) To make the code more complex for security.
c) To avoid using any external libraries.
d) To process data only from external sources.
a) To identify the most efficient and straightforward ways to handle data.
When choosing between different data structure implementations or algorithms, what trade-off is often considered, especially in scenarios that don’t require extreme performance?
a) The cost of hardware versus software.
b) Efficiency and simplicity versus complexity and maintainability.
c) The speed of development versus the number of bugs.
d) The user interface design versus database design.
b) Efficiency and simplicity versus complexity and maintainability.
Which three main interfaces, considered part of or treated like the Java Collections Framework, are the focus of the discussion on built-in data structures?
a) Interface, Class, Object.
b) Public, Private, Protected.
c) List, Set, Map.
d) Abstract, Final, Static.
c) List, Set, Map.
Describe the fundamental characteristics of the java.util.List
interface, particularly concerning the order of elements and the allowance of duplicates.
a) It preserves the order of elements and allows duplicate elements.
b) It does not preserve order and does not allow duplicates.
c) It preserves order but does not allow duplicates.
d) It does not preserve order but allows duplicates.
a) It preserves the order of elements and allows duplicate elements.
How does the java.util.ArrayList
implementation of the List
interface manage its size compared to traditional Java arrays?
a) Its size is fixed upon creation.
b) Its size can only be decreased, not increased.
c) Its size changes only when elements are removed.
d) Its size can be dynamically increased, unlike standard Java arrays.
d) Its size can be dynamically increased, unlike standard Java arrays.
The sources recommend relying on interfaces like List
rather than concrete implementations like ArrayList
when declaring variables. Explain why this is considered good programming practice.
a) It makes the code shorter.
b) It is a requirement for using generics.
c) It allows for easier switching between different implementations (e.g., ArrayList to LinkedList) with less code change.
d) It ensures the list is always sorted.
c) It allows for easier switching between different implementations (e.g., ArrayList to LinkedList) with less code change.
Outline the different ways to iterate over the elements of a List
as presented in the sources, including the pros/cons or typical use cases mentioned for each.
a) Using streams’ map
and filter
methods.
b) Using an index-based for loop, an enhanced for-loop, and the forEach method with a lambda expression.
c) Using iterators explicitly with hasNext()
and next()
.
d) Using while loops and custom counters.
b) Using an index-based for loop, an enhanced for-loop, and the forEach method with a lambda expression.
What happens if you attempt to modify (add, remove, or set) a list that was created using the List.of()
factory method?
a) The modification is successful.
b) The list is automatically converted to a mutable list.
c) A java.lang.UnsupportedOperationException
is thrown.
d) The operation is logged and ignored.
c) A java.lang.UnsupportedOperationException
is thrown.
How does a list created using Arrays.asList()
behave differently from one created with List.of()
when attempting modifications?
a) Arrays.asList()
allows adding and removing, List.of()
allows changing contents.
b) Both allow adding and removing elements freely.
c) List.of()
allows all modifications, Arrays.asList()
allows none.
d) Arrays.asList()
allows changing element contents but not structural changes like adding/removing, while List.of()
allows no modifications.
d) Arrays.asList()
allows changing element contents but not structural changes like adding/removing, while List.of()
allows no modifications.
Which list implementation discussed allows for complete mutability, including adding and removing elements?
a) ArrayList
b) ImmutableList
c) List.of()
lists
d) Lists created by Arrays.asList()
a) ArrayList
.
In the context of removing elements from an ArrayList
, why is removing by index position generally more performant than removing by providing the object itself?
a) Removing by index shifts all subsequent elements, which is faster.
b) Removing by object uses a binary search, which is slower.
c) Removing by index requires traversing the list.
d) Removing by object requires traversing the list to find a matching element, whereas removing by index is a direct access operation.
d) Removing by object requires traversing the list to find a matching element, whereas removing by index is a direct access operation.
What is the defining characteristic of the java.util.Set
interface in terms of element uniqueness?
a) It preserves the order of insertion.
b) It guarantees that it does not contain duplicate elements.
c) It stores elements in a sorted order.
d) It uses keys to store values.
b) It guarantees that it does not contain duplicate elements.
Explain the concept of object equality in Java, differentiating between comparison based on memory location (==
operator) and comparison based on object attributes (equals()
method), and how overriding equals()
and hashCode()
is crucial for collections like Set
that exclude duplicates.
a) ==
compares memory locations, equals()
compares object attributes (when overridden); overriding both is necessary for Sets to identify duplicates based on attributes.
b) ==
and equals()
always compare object attributes.
c) Only overriding equals()
is sufficient for Sets.
d) Sets use ==
for duplicate checking, making equals()
and hashCode()
irrelevant.
a) ==
compares memory locations, equals()
compares object attributes (when overridden); overriding both is necessary for Sets to identify duplicates based on attributes.
Why is it particularly important to override the hashCode()
method alongside equals()
when using objects in hash-based collections like HashSet
or HashMap
?
a) It improves the readability of the code.
b) It is only required for primitive types.
c) It ensures the order of elements is preserved.
d) Hash-based collections rely on the hash code for efficient storage and retrieval, requiring it to be consistent with the equals()
method.
d) Hash-based collections rely on the hash code for efficient storage and retrieval, requiring it to be consistent with the equals()
method.
How does a HashSet
determine if an object being added is a duplicate, and what happens if you try to add a duplicate object?
a) It uses only the ==
operator.
b) It uses the equals()
and hashCode()
methods; duplicate objects are not added.
c) It compares objects based on their insertion order.
d) It sorts the set and checks for identical adjacent elements.
b) It uses the equals()
and hashCode()
methods; duplicate objects are not added.
Describe the primary function of the java.util.Map
interface and when you might choose to use it over a List
or Set
.
a) To store data as key-value pairs, enabling lookup of values based on their associated keys.
b) To maintain a sorted list of unique elements.
c) To store elements in a sequence where duplicates are allowed.
d) To store primitive data types in a fixed-size structure.
a) To store data as key-value pairs, enabling lookup of values based on their associated keys.
When using a Map
, what is required of the key objects to ensure correct behavior regarding uniqueness and retrieval, as demonstrated in the Id
class example?
a) Keys must be instances of java.lang.Integer
.
b) Key objects must have correctly overridden equals()
and hashCode()
methods.
c) Keys must be unique strings generated by the JVM.
d) Key objects must implement the Serializable
interface.
b) Key objects must have correctly overridden equals()
and hashCode()
methods.
What is the outcome if you attempt to add a new entry to a Map
using a key that already exists?
a) A new entry is added, resulting in multiple entries with the same key.
b) The existing entry is removed, and no new entry is added.
c) The insertion of a new entry is ignored because the map already contains an entry with that key.
d) A runtime error is thrown indicating a duplicate key violation.
c) The insertion of a new entry is ignored because the map already contains an entry with that key.
According to the source, the NIO.2 API for file handling is located in which Java package?
a) java.io
b) java.util
c) java.nio
d) java.lang
c) java.nio
What does the source state about creating a Path object using Path.of(\"/some/path\")
?
a) The file or directory represented by the path is guaranteed to exist in the file system.
b) Creating the object automatically creates the file or directory in the operating system.
c) The path represented by the object may not exist in the operating system.
d) The method can only be used for absolute paths, not relative ones.
c) The path represented by the object may not exist in the operating system.
In the context of the NIO.2 API, what is the typical path separator character used on Windows operating systems, as mentioned in the source?
a) /
b) :
c) .
d) \
d) \
According to the source, what is the primary purpose of calling the normalize()
method on a Path object?
a) To combine the path with another path.
b) To remove redundant elements such as ./
and ../
from the path representation.
c) To check if the path exists in the file system.
d) To get the root directory of the file system.
b) To remove redundant elements such as ./
and ../
from the path representation.
When using the NIO.2 API’s Files.createFile()
method, what happens if the file specified by the Path object already exists?
a) The existing file is silently overwritten with an empty file.
b) The method returns null without creating or modifying the file.
c) A FileAlreadyExistsException
is thrown.
d) The method creates a backup of the existing file before overwriting it.
c) A FileAlreadyExistsException
is thrown.
Based on the source’s explanation of Files.writeString
, what is the effect of using the StandardOpenOption.APPEND
option?
a) It overwrites any existing content in the file.
b) It ensures the file is created if it doesn’t exist.
c) It adds the new data to the end of the file without deleting existing content.
d) It locks the file, preventing other processes from writing to it.
c) It adds the new data to the end of the file without deleting existing content.