Preview

05 - Tutorial #3+Past Paper Qs -2d loops,lists

 1. Please work through the following presentation which takes you through solving a programming challenge (using for loops and 2d arrays (lists) step by step. Select 'True' when done. (1 mark)

  TRUE

  FALSE

 2. What is the output of the following code? (1 mark)
usernamesandpasswords = [["marvR","open123"],["benjR","pass123"]]
print(usernamesandpasswords[x])

 3. What is the output of the following code? (1 mark)
usernamesandpasswords = [["marvR","open123"],["benjR","pass123"]]
print(usernamesandpasswords[0][1])

 4. What is the output of the following code? (1 mark)
usernamesandpasswords = [["marvR","open123"],["benjR","pass123"]]
print(usernamesandpasswords[1][1])

 5. What code do you need to write on the next line to ensure that the output is benjR?. Refer to the previous examples and write your answer in that format (python) (1 mark)
usernamesandpasswords = [["marvR","open123"],["benjR","pass123"]]

 6. In a 2d array you require two index numbers to access elements in the array. Using a loop (iteration) would also require two index numbers. (1 mark)

  FALSE

  TRUE

 7. What will the following code output? (1 mark)
usernamesandpasswords = [["marvR","open123"],["benjR","pass123"]]
for i in range(2):
  for j in range(2):
    print(usernamesandpasswords[i][j])

 8. In the following code you will note that a username and password has been added to the 2d list. Will they be printed? (1 mark)
usernamesandpasswords = [["marvR","open123"],["benjR","pass123"],["john","john123"]]
for i in range(2):
  for j in range(2):
    print(usernamesandpasswords[i][j])

 9. Line 3 has been changed to for j in range(3) instead of in range(2). Will this print every element in the array? Why or why not? (2 marks)

 10. Instead of specifying a number in for i in range(2) you could find out the length of the list, so that if the list grows, the number would always be right. What is the output of this code? (1 mark)
usernamesandpasswords = [["marvR","open123"],["benjR","pass123"],["john","john123"]]
print(len(usernamesandpasswords))

 11. This code will print the first three elements on the list and then produce an error. On which line is the error? (1 mark)
usernamesandpasswords = [["marvR","open123"],["benjR","pass123"],["john","john123"]]
print(usernamesandpasswords[0][0])
for i in range(len(usernamesandpasswords)):
  for j in range(len(usernamesandpasswords)):
    print(usernamesandpasswords[i][j])

 12. Analyse the code below. len(usernamesandpasswords)-1 is essentially the same,given the current elements in the list, as writing what number? (1 mark)
usernamesandpasswords = [["marvR","open123"],["benjR","pass123"],["john","john123"]]
print(usernamesandpasswords[0][0])
for i in range(len(usernamesandpasswords)):#this is the outer loop which iterates through the different lists (in this case there are 3 lists)
  for j in range(len(usernamesandpasswords)-1): #this is the inner loop which iterates through the different elements in each list, in this case there are 2 elements
    print(usernamesandpasswords[i][j])

 13. The double loop shown in the code above is also called: (1 mark)

 14. On what line does the programming construct SELECTION begin in this program? (1 mark)
usernamesandpasswords = [["marvR","open123"],["benjR","pass123"]]
username = input("Enter a username: ")
password = input("Enter a password: ")
found = False
while found == False:
  for i in range(0,len(usernamesandpasswords)):
    for j in range(0,len(usernamesandpasswords[i])-1):
      if username == usernamesandpasswords[i][j] and password == usernamesandpasswords[i][j+1]:
        print("Access granted")
      found = True
  else:
      pass
  if found == False:
    print("Access denied")
    found = True

 15. In the code above a while loop is used in line 5. What needs to happen for the user to break out of this loop? (1 mark)