Preview

24 - Coding Challenge #2 & Past Paper Simulation

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

  TRUE

  FALSE

 2. The following code snippet shows a list of usernames. This list is essentially a 1d array of usernames. What is the output? (1 mark)
usernames_list=["user1","user2","user3","user4"]
print(usernames_list[2])

 3. Referring to the previous question and code snippet, what woud you need to type on line 3 in order to print: user1? (1 mark)

 4. The following code uses a for loop to iterate through the list of usernames in the list. What sort of error do you encounter and what do you need to change line 2, in order to fix it? (2 marks)
usernames_list=["user1","user2","user3","user4"]
for i in range(5):
    print(usernames_list[i])
    

 5. In the following code, the if statement is said to be ________ inside the for loop. (1 mark)
numbers = [12,33,47,53,634,734]

number = input("Enter number:")
for i in range (len(numbers)):
  found = False
  if numbers[i]==number:
    print("Your number is in position:", i)
    found = True
    break



if found == False:
  print ("Sorry,that number is not in the list")

 6. On what line, referring to the code snippet in the previous question, is a boolean flag created? (1 mark)

 7. What happens to the boolean flag when the username is found in the list? (1 mark)

 8. In the following code, what is the output if the input is: eve (1 mark)
usernames = ["adam", "eve", "cain", "abel","seth","enoch"]

find = input("Enter username:")
for i in range (len(usernames)):
  found = False
  if usernames[i]==find:
    print("Your username is in position:", i)
    found = True
    break



if found == False:
  print ("Sorry,username is not found")

 9. Referring to the previous question and code, in which line is the program checking to see if the entered input is found in the list of usernames? (1 mark)

 10. Analyse the following code. If you enter the number 33 (as input) the output is "Sorry, that number is not in the list". Can you explain why? (1 mark)
numbers = [12,33,47,53,634,734]

number = input("Enter number:")
for i in range (len(numbers)):
  found = False
  if numbers[i]==number:
    print("Your number is in position:", i)
    found = True
    break



if found == False:
  print ("Sorry,that number is not in the list")