Lab 7: Part 1. Memory Diagrams

Why Memory Diagrams?

Aliasing is when the same memory location can be acccessed using different variable names. In other words, aliasing occurs when two different variables point to the same location in memory. The best way to see and understand this is by drawing memory diagrams.

Prefer a digital version of these tasks? Click here and make a copy.

Task 1.

Below is a snippet of code. Write the output that is generated by the print statements.

a = [2, 4, 6]
b = [1, 3, 5]
b[2] = a[0] * a[1]
a.insert(1,'water')
print('---- 1st print ----')
print(a)
print(b)
a = b
print('---- 2nd print ----')
print(a)
print(b)
b.pop()
b.append('blue')
print('---- 3rd print ----')
print(a)
print(b)

Task 2.

Below is another snippet of code. Write the output that is generated by the print statements. Draw a memory diagram to visualize the code.

x = [80, 75, 50]
y = x
z = [x, y[1]]
x[0] = 100
y.append('hi')
z.insert(1, x[1] + y[1])
z[0].pop(1)
print(x)
print(y)
print(z)

Table of Contents