Courseiva
Working with Arrays and CollectionsmediumMultiple ChoiceObjective-mapped

1Z0-829 Working with Arrays and Collections Practice Question

A financial application processes transactions as List<Transaction> objects. The application runs on a server with limited memory (2 GB heap). The development team observes that after processing a large number of transactions (over 10 million), heap usage spikes to near 1.8 GB and garbage collection pauses become frequent (over 5 seconds). The Transaction class is defined as public record Transaction(LocalDateTime timestamp, double amount, String category) {}. The current processing code reads all transactions from a database result set into an ArrayList<Transaction> using a loop with list.add(). Then the list is sorted by timestamp using Collections.sort(list, Comparator.comparing(Transaction::timestamp)). The sorted list is then iterated multiple times to generate various reports. The code runs in a single-threaded context. Which change would most effectively reduce peak memory usage while preserving the sorted report output?

⚠ Common exam trap

It's easy for candidates to assume a sorted collection (TreeSet, TreeMap) is memory-efficient because it avoids explicit sorting, but they overlook the deduplication behavior of Set and the per-entry overhead of map structures, which actually increase memory usage and can corrupt data.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

Collect transactions into an array (Transaction[]) and sort using Arrays.sort().

Using an array (Transaction[]) with Arrays.sort() avoids the per-element overhead of ArrayList's internal Object[] and the additional memory consumed by the ArrayList object itself (e.g., capacity tracking, modCount). In a memory-constrained environment with 10 million transactions, the ArrayList wrapper adds roughly 40–80 MB of overhead (object header, internal array pointer, size, capacity fields), whereas a plain array has only the object header and the contiguous element references. This reduction in memory footprint directly lowers peak heap usage and reduces GC pause frequency.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • Use a TreeMap<LocalDateTime, List<Transaction>> to group by timestamp, then flatten on iteration.

    Why it's wrong here

    This increases memory due to Map and List overhead, and grouping duplicates adds complexity.

  • Use a SortedSet<Transaction> (java.util.TreeSet) with a comparator to store transactions in sorted order.

    Why it's wrong here

    TreeSet does not allow duplicate elements, but transactions may have identical timestamps but different amounts/categories, so duplicates could be lost.

  • Use a parallel stream with .sorted() and collect to a ConcurrentLinkedDeque.

    Why it's wrong here

    Parallel streams introduce overhead for thread management and concurrent collections have additional memory overhead, increasing memory usage.

  • Collect transactions into an array (Transaction[]) and sort using Arrays.sort().

    Why this is correct

    An array avoids the overhead of ArrayList's internal array expansion (which may overallocate up to 50%) and uses contiguous memory with no wrapper objects, reducing memory footprint.

About these practice questions

This 1Z0-829 question is part of Courseiva's 513-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This 1Z0-829 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-829 exam.