215 views

1 Answer

1 1 vote

This question tests the understanding of shallow copies and mutable nested objects in Python.

Shallow Copy:

$\verb|L2 = list(L1)|$

  • L2 points to a new list object $\verb|[1, 2, Y]|$.
     
  • The elements are copied by reference:
     
    • $\verb|L2[0]|$ and $\verb|L2[1]|$ are copies of the integers (which are immutable).
       
    • L2[2] points to the SAME nested list object $\verb|[3, 4]|$ (labeled $X$ ) as $\verb|L1[2]|$ This is the crucial point of a shallow copy.
       

$\verb|L3 = L1|$

  • L3 points to the SAME list object as $\verb|L1|$

\[
\begin{array}{lll}
\text{Variable} & \text{Points to} & L1[2]\ \text{points to} \\
\verb|L1| & \verb|[1, 2, X]| & \verb|[3,4]|\ (\text{Object X}) \\
\verb|L2| & \verb|[1, 2, X]| & \verb|[3,4]|\ (\text{Object X}) \\
\verb|L3| & \verb|[1, 2, X]| & \verb|[3,4]|\ (\text{Object X})
\end{array}
\]

$\verb|L1[0] = 99|$

  • The list object pointed to by L1 and L3 is mutated at index 0 .
     
  • State of L1/L3: $\verb|[99, 2, [3, 4]]|$
     

$\verb|L2[2][0] = 88|$

  • $\verb|L2[2]|$ is the nested list object $\verb|[3,4]|$ (Object $X$ ), which is shared by L1 and L2. .
     
  • The shared nested list is mutated at index $0$.
     
  • State of Nested List (Object X): $\verb|[88, 4]|$
     
  • State of L1/L3: $\verb|[99, 2, [88, 4]]|$
     
  • State of L2: $\verb|[1, 2, [88,4]]|$
     

$\verb|L3.append(5)|$

  • $\verb|L3|$ and $\verb|L1|$ are the same object. An element is appended to this shared object.
     
  • State of L1/L3: $\verb|[99, 2, [88, 4], 5]|$
     
  • State of L2: $\verb|[1,2,[88,4]]|$ (L2 is a separate outer list and is unaffected).


$L1[2][0]$ is $88.$

The length of L2 is $\mathbf{3}$.

Result: $88+3=91$.

Answer:
Position:
Show:

Related questions

2 2 votes
2 2 answers
355
355 views
GO Classes asked Dec 19, 2025
355 views
A Perfectly balanced binary search tree (BST) contains $15$ distinct integers.The largest element is stored at the rightmost node of the tree.Which of the following eleme...
2 2 votes
1 1 answer
207
207 views
GO Classes asked Dec 19, 2025
207 views
Consider An array of $n$ Comparable objects that is sorted except for $k$ randomly located pairs of adjacent elements that have been swapped (each element is part of at m...
0 0 votes
1 1 answer
202
202 views
GO Classes asked Dec 19, 2025
202 views
What is the time complexity of the function log_plus_linear (n) in terms of the input size $n$ (where $n$ is a positive integer)?def log_plus_linear(n): count = 0 ...
0 0 votes
1 1 answer
193
193 views
GO Classes asked Dec 19, 2025
193 views
What is the final value printed by the following Python code snippet? def mutate_dict(d): d['a'] = 20 d['c'] = 40 d1 = {'a': 10, 'b': 20} d2 = d1 d2['b'] = 30...