1. Which of the following is incorrect (False) about Python?
2. In the following code, name the created class.
3. Referring to the code above, when line 15 is executed, what is the output?
4. For the following 2d array, what is the output?
nw=[["adam","seth","enoch"],["shield","belt","sword"]]
print(nw[0][1])
5. The nested loop in the code below (lines 18-20) seeks to produce the same output as lines 5-7. Fill in the blanks to replace the question marks (line 20)
nw=[["adam","seth","enoch"],["shield","belt","sword"]]
print("Use sequence: print 2d array names with weapons")
print("============================================")
print("Player:",nw[0][0]," has a:",nw[1][0])
print("Player:",nw[0][1]," has a:",nw[1][1])
print("Player:",nw[0][2]," has a:",nw[1][2])
#Notice the patterns. the first 0,1,2 is inside the first list (0)
#The second 0,1,2 is happening inside the second list (1)
#We need a loop to loop through lists 1 and 2 (e.g 0 and 1)
#We need another loop to loop through each element 0,1,2 in each list.
print("")
print("")
print("Use nested loop: print names/weapons")
print("============================================")
for i in range(len(nw)):
for j in range(len(nw[0])):
print("Player",nw[i][j],"has a:",nw[?][j])
j
i
j+1
i+1
6. In the following code, what is happening on line 21 and line 26? (2 marks)
class Stack:
#start with this part which is like a constructor
def __init__(self):
self.items=[] #one attribute called self.items and start this as an empty stack
#method needed to push items on to the stack
def push(self,item):
self.items.append(item) #items is the attribute, and we append a new item
def pop(self):
return self.items.pop()
def size(self):
return len(self.items) #returns the length of the list
def report(self):
return self.items #returns the elements contained in the stack
def isEmpty(self):
if self.items==[]: #test to see if items is equal to empty list, return True
return True
else:
return False
#take in a word and reverse it
word=input("Enter a word:") #enter the word: "apple"
s=Stack()
for i in range(len(word)):
s.push(word[i])
print(s.report())
letter=s.pop()
print(letter)
7. Consider the following linked list based implementation of queue operations (incomplete). What is line 21-28 doing?
8. In the following pseudocode implementation of a tree, the program _______ checks every node to see if it has a right and a left child; -1 indicates that there is _______.
9. What type of sorting algorithm has been implemented here? What needs to fill in the blanks on line 9? (2 marks)
10. What is the output of the following graph?
graph = {'A': ['B', 'C'],
'B': ['A', 'D', 'E'],
'C': ['A', 'F'],
'D': ['B'],
'E': ['B', 'F'],
'F': ['C', 'E']}
def dfs_path(graph,start,end):
result = []
dfs(graph,start,end,[],result)
return result
def dfs(graph,start,end,path,result):
path+=[start]
if start == end:
result.append(path)
else:
for node in graph[start]:
if node not in path:
dfs(graph,node,end,path[:],result)
print(dfs_path(graph,'A','F'))
[['A', 'B', 'E', 'F'], ['A', 'C', 'F']]
[['C', 'B', 'E', 'F'], ['A', 'A', 'F']]
[['F', 'B', 'E', 'F'], ['A', 'C', 'A']]
[['A', 'B', 'C', 'F'], ['A', 'C', 'F']]
11. On what line in the following code is the first time you see recursion occuring?
12. With regard to the A* algorithm, decide whether the following statement is true or false.
13. What is the output of the following code and what data mapping type is being used?
14. ______ is a free, open-source, full-stack web application framework written in Python. It is a set of ready-made components that help you rapidly build websites.
15. The following code shows recursion to find the factorial of a number. There is one mistake. What is output from the code? How could you correct it to make it produce 5!=120