# 07 - Tutorial #5+Past Paper Qs -nested arrays

1. Work through the following presentation that takes you through 2d arrays, for loops and a practice exam paper question. Select 'Yes' when done. (1 mark)

Yes

No

2. The following code shows a 1d array (list in Python). What is the output of this code? (1 mark)
```weapons1d=["gun","knife","love","truth","crossbow"]
print(weapons1d[3])
```

3. What is the output of this code? (1 mark)
```weapons1d=["gun","knife","love","truth","crossbow"]
print(weapons1d[0])
```

4. The following code shows a 2d array and a print statement. What is the output of this code? (1 mark)
```weapons2d=[["gun","knife","AK47"],["Adam","Eve","Cain"]]
print(weapons2d[1])```

Out of range error

It will print the contents of the second list (that is list 1) which is Adam,Eve,Cain

5. What is the output of this code? (1 mark)
```weapons2d=[["gun","knife","AK47"],["Adam","Eve","Cain"]]
print(weapons2d[0][1])```

6. What is the output of this code? (1 mark)
```weapons2d=[["gun","knife","AK47"],["Adam","Eve","Cain"]]
print(weapons2d[2][1])```

7. What is the output of this code? (1 mark)
```weapons2d=[["gun","knife","AK47"],["Adam","Eve","Cain"]]
weapons2d[0][1]="mercy"
print(weapons2d[1][2])```

8. In the above code, what is happening on line 2? (1 mark)

9. In the following code, on line 2, what is the value of len(weapons2d)? (1 mark)
```weapons2d=[["gun","knife","AK47"],["Adam","Eve","Cain"]]
for i in range(len(weapons2d)):
for j in range(len(weapons2d[0])):
print(weapons2d[i][j])```

10. In the above code, on line 3, what is the value of len(weapons2d[0]) ? (1 mark)

11. Analyse the following code. What line do you need to change, and what do you need to change it to, in order to produce the desired output? (2 marks)
```weapons2d=[["gun","knife","AK47"],["Adam","Eve","Cain"]]
for i in range(1):
for j in range(2):
print("This player:",weapons2d[1][j],"has:",weapons2d[0][j])

#Desired output is below
"""
This player: Adam has: gun
This player: Eve has: knife
This player: Cain has: AK47
"""
```

12. Name two searching methods that could be used to search an array for a specific number or name. (assume it is ordered) (2 marks)

13. Which of the searching method (select from the ones you mentioned above) would you use to search through the following list? (1 mark)
`mylist=[2,7,24,1,7,9,12,5]`

14. A __________ could be implemented using a 2d array. A ________ involves the updating of pointers so that data can be stored dynamically (1 mark)

15. If you need to iterate over every element in a multi-dimensional array you will almost certainly need to use _______ loops ? one loop for every dimension. (1 mark)