A Java Memo contains a String title and a reference page to a mutable Page object with integer field visits. A shallow copy creates a new memo and copies both reference values. Java strings are immutable. Considering each statement independently from this starting state, select every correct statement.
Keeping title contents independent requires the copied memo to identify a separately allocated string object.
Changing visits through the copied memo's page leaves the original memo's page visits unchanged.
Assigning a different string to the copied memo's title leaves the original memo's title unchanged.
Assigning a new page to the copied memo leaves the original memo's page reference unchanged.
Unlock full course notes by signing up
Counting allocations with repeated references
A mutable Batch has three reference fields: first and second identify entry E1, while third identifies a distinct entry E2. Both mutable entries identify the same mutable Counter. There are no other referenced model objects.
You create one shallow copy of the original batch and, separately, one deep copy of the original batch. The deep copy duplicates every reachable mutable object exactly once and preserves internal sharing. Count only newly allocated Batch, Entry and Counter instances, ignoring helper storage. How many model objects are allocated across both copy operations? Answer as an exact integer, to zero decimal places.
Budgeting independent snapshots
A laboratory application keeps editable snapshots of a measurement record. An independent snapshot is a deep copy that shares no mutable object with the source or with another snapshot.
The source consists of one record object, one readings array, one notes array, and four distinct mutable note objects. Each note appears once in the notes array. There are no other referenced objects. The readings array stores 40 numeric values directly; the notes array stores four references.
Use this exact storage model:
Copying a cycle without endless recursion
Two mutable nodes form the cycle shown: A's next identifies B, and B's next identifies A. Each node also has primitive data fields. You need a deep copy that preserves this cycle without sharing either original node.
A recursive helper uses one shared lookup table keyed by original object identity. The table starts empty and records each original node together with its copied counterpart. Every helper call receives a non-null source node. Arrange the helper's steps in their correct execution order; a stated early return ends that call.
Keeping snapshot memo tables separate
An editor creates morning and afternoon snapshots of two source records, a and b. Their detail fields initially refer to the same mutable object, as shown below. The source records and their detail object are all mutable.
The function copyGraph(x, memo) makes a graph-preserving deep copy. A memo table maps each source object to its replacement object. If the table already contains a replacement for an object, the function returns that replacement immediately, without rereading or updating its fields. Otherwise, the function creates and records a replacement and recursively copies its fields using that same table.
Two distinct, initially empty tables, m1 and m2, are available. Each snapshot must preserve the shared detail relationship between its two copied records. The morning and afternoon snapshots must share no mutable objects with each other or with the source. The afternoon snapshot must include edits made to the source after the morning snapshot.