1. The three main programming constructs are often referred to as:
2. Which of the following would be likely to be declared as 'constants' within a coded game, as opposed to 'variables'?
3. Analyse the following code (Python). With an input of '5' at the prompt, can you predict what the output will be?
4. What is happening on line 2 of the following code?
5. MOD: What is the output for print(6%4)?
6. FLOOR DIVISION: What is the output for print (10 //3)
7. What is the output for print (3.0**2.0)
8. The following code makes use of ____________ and the output, for an input of '34' is ___________.
9. What is wrong, if anything, with this code?
10. What do function 1 and function 2 do?
11. Which statement best describes the difference between a while and a for loop?
12. Which statement best describes and provides the best solution for the following code? We would like "I love python" to print just once.
13. What needs to change in the code if we wish to produce a matrix output with the dimensions of the input number. E.g. if 5 is input 5 x 5, or if 3 is input 3 x 3?
14. _________________ result in a value of either True or False. In the case of the examples below, the output is ___________________________.
print (7 < 10)
print (4 < 16)
print (4 == 4)
print (4 <= 4)
print (4 >= 4)
print (4 != 3)
Boolean expressions / 'False' in all cases
Integer division expressions / True, False, True, True, True, True
Boolean expressons / 'True' in all cases
Boolean Expressions / True, True, True, False, False, True
15. Explain what is happening on lines 1 and 5, and predict the output of the code.
16. Analyse the code below. What is the new value (output) of 'matrix' when it is printed on line 3?
matrix = [1,2,3,4,5,6,7]
matrix[3]="x"
print(matrix)
[1,2,x,4,5,6,7]
[x,x,x,x,x,x,x]
Error
[1, 2, 3, 'x', 5, 6, 7]
17. Analyse the code below and predict the output. Pay particular attention to line 6.
def main():
#1. Create a nested list (each list is a row on the matrix)
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#2. print the matrix so it displays row by row in a square grid
for i in matrix:
print(i[2])
main()
1,4,7
2,5,8
1,2,3
3,6,9
18. String Manipulation: What is the output of the following code?
19. File Handling: In the following code, what does the 'r' on line 2 refer to?
20. The append function is used to append to the file instead of _______________ it.
21. Analyse the code below for a Binary Search. What needs to go on the missing line 6?
22. The following code shows a sequential (also called a linear) search. What is the output of this program?
def Sequential_Search(dlist, item):
pos = 0
found = False
while pos < len(dlist) and not found:
if dlist[pos] == item:
found = True
else:
pos = pos + 1
return found, pos
print(Sequential_Search([11,23,58,31,56,77,43,12,65,19],31))
(True, 3)
(False, 3)
FOUND
TRUE
23. In the following code, what does len(nlist) do?
def bubbleSort(nlist):
for passnum in range(len(nlist)-1,0,-1):
for i in range(passnum):
if nlist[i]>nlist[i+1]:
temp = nlist[i]
nlist[i] = nlist[i+1]
nlist[i+1] = temp
nlist = [14,46,43,27,57,41,45,21,70]
bubbleSort(nlist)
print(nlist)
It performs string manipulation on the list to retreive the 9th element
It finds the maximum index number of the list (in this case 10)
It gives the length of the list 'nlist' in this case 9
It finds the length of the list and adds 1 to it. In this case len(nlist) would give: 10
24. The use of ___________ reduces code redundancy. Hence increases reusability
25. In the following code (see if you can spot the pattern), if the start number is: 1 and the end number is: 5, what four numbers are ouput?
startNumber = int(raw_input("Enter the start number here "))
endNumber = int(raw_input("Enter the end number here "))
def fib(n):
if n < 2:
return n
return fib(n-2) + fib(n-1)
print map(fib, range(startNumber, endNumber))
1,1,3,5
1,2,3,4
1,1,2,3
1,3,5,7