Data Structures

Two Arrays

Determine the output of the following code.

It counts as cheating if you run the code!

The computer first runs x = [2, 4, 8]\texttt{x = [2, 4, 8]}. This tells the computer to take x\tt x and point it to the array, so we get the following picture:

Next, the computer runs y = x\texttt{y = x}. It takes y\tt y and points it wherever x\tt x points.

Next, the computer runs x[0] = 1\texttt{x[0] = 1}. It takes x[0]\texttt{x[0]} and points it to the number 1.

Finally, the computer runs print(y[0])\texttt{print(y[0])}. The computer steps to the y\tt y, and then along the 0\tt 0 pointer, and prints out 1\tt 1.

This result might have been surprising, since we changed x[0]\tt x[0], and then y[0]\tt y[0] also changed at the same time. But it's clear why this happened if you understand how pointers work in Python.

Mark as Completed:
Submits:
test
Test your code to get an output here!