Preview

14 - General Overview Test #1 (password protected)

 1. The three main programming constructs are often referred to as:

  Variables, Constants and Lists (Arrays)

  Iteration, String Manipulation and Arrays

  Variables, IF statements and Loops

  Sequence, Selection and Iteration

 2. Which of the following would be likely to be declared as 'constants' within a coded game, as opposed to 'variables'?
Unit of Gravity
Value of PI
Score of the player
Health of the player

  Value of PI and Score of the player

  Unit of Gravity and Health

  Unit of Gravity and Value of PI

  Score of the player and Health of the player

 3. Analyse the following code (Python). With an input of '5' at the prompt, can you predict what the output will be?
wholenumber= input("Enter a whole number please ")
answer = wholenumber *10 
print(answer)

  ERROR

  Answer is: 5555555555

  Answer is: 10

  Answer is: 50

 4. What is happening on line 2 of the following code?
pocket_money= input("How much pocket money do you have left? ")
answer = float(pocket_money) -5.00
print("Well, you owe me £5.00, so that leaves you with   £", answer)

  The constant 'pocket_money' is being floated (e.g. divided by 0) and £5.00 is subtracted from the result

  The variable 'pocket_money' is being converted into a whole number and £5.00 is then subtracted

  The variable 'pocket_money' is converted to a float data type and £5.00 is being subtracted from it

  The variable 'pocket_money' is being initialised to zero. £5.00 is being subtracted,causing an error for the value of 'answer'

 5. MOD: What is the output for print(6%4)?
print (6 % 4)

  9

  12

  2

  3

 6. FLOOR DIVISION: What is the output for print (10 //3)
print (10 //3) # floor division: always truncates fractional remainders. Note using a single '/' would produce a different result.

  2

  12

  3

  9

 7. What is the output for print (3.0**2.0)
print (3.0**2.0) 

  3

  12

  2

  9

 8. The following code makes use of ____________ and the output, for an input of '34' is ___________.
score = input("Enter score: ")
score = int(score)
if score >= 80:
    grade = 'A'
else:
    if score >= 70:
        grade = 'B'
    else:
        if score >= 55:
            grade = 'C'
        else:
            if score >= 50:
                grade = 'Pass'
            else:
                 grade = 'Fail'
print ("\n\nGrade is: " + grade) 

  Iteration and IF statements / Fail

  Nested IF statements' / Fail

  Indented iteration / Fail

  Simple IF Statements / Pass

 9. What is wrong, if anything, with this code?
password="open123"
print('Enter password:')
answer=input()
if answer==password
                print("yes")
  else:
                print("no")

  There is a missing colon on line 4 and the indentation is incorrect on line 6

  It is not in a function, so will not run. Line 3 should also be indented

  There is nothing wrong with it.

  Line 3 should be indented and Line 4 is missing a colon

 10. What do function 1 and function 2 do?
def  function1():
   i=1
   while i < 11:
      number=2
      print(i*number)
      i =i+1
    
def  function2():
   for i in range(1,11):
      number=2
      print(i*number)

function1()
function2()

  Function 1 works correctly to print the 2 times table, and function 2 produces an error as a for loop cannot be used to do this

  Function 1 uses a while loop to count from 1 to 11, going up by 2. Function 2 simply prints the numbers 1 to 11 in order

  They both use loops (function 1 - while loop, function 2 - for loop) to produce the 2 times table up to 10. (e.g. 2,4,6,8,10 etc…)

  Both functions are identical and print the number '2', 11 times.

 11. Which statement best describes the difference between a while and a for loop?

  A while loop is condition controlled and a for loop is count controlled

  A while loop may not run at all if the starting condition is not met.

  A while loop's stopping condition is defined at the start where as a for loop has a known amount of iterations

  All the statements listed here are valid differences between for and while loops

 12. Which statement best describes and provides the best solution for the following code? We would like "I love python" to print just once.
x = 1
while x<2:
    print ("I love python")

  There are no errors in the code - it will correctly print "I love python" once.

  The code produces an infinite loop. Line 4 needs the inclusion of: x=x+1

  The code prints "I love python" just once, but then produces an error. Line 1 needs to be: x=0

  The code prints "I love python" 2 times. Change line 2 to: while x <1:

 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?
number=int(input("Enter a number:"))
for i in range(number):
  print("*",end="")
  for i in range(2):
    print("*",end="")
  print()

On running the program with an input of 5
we get a 3 x 5 matrix. 

Output
=======
Enter a number: 5
***
***
***
***
***

  The first for loop in the code (for i in range(number) needs to be changed to: for i in range(number-1)

  The first for loop in the code (for i in range(number) needs to be changed to: for i in range(i)

  The second for loop in the code (for i in range(2): needs to be changed to: for i in range(number-1):

  The second for loop in the code (for i in range(2): needs to be changed to: for i in range(number):

 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.
def mysteryfunction(x,y):
  return (x+y)%5


print(mysteryfunction(2,3))

  Function creation / Output: 5

  Parameter passing / Output: 0

  Parameter passing / Output: 1

  Parameter passing / Output: 6

 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?
word = "Python"
print(word[0:3])

  pyth

  thon

  th

  pyt

 19. File Handling: In the following code, what does the 'r' on line 2 refer to?
filename = "hello.txt"
file = open(filename, "r")
for line in file:
   print line,

  r' indicates the 'mode' - in this case 'r' for reading from file

  r' indicates the letter that is being written to the file

  r' indicates the letter that will separate each line

  r' simply indicates the name of the file (extension such as hello.txt.r)

 20. The append function is used to append to the file instead of _______________ it.

  finding

  reading to

  duplicating

  over writing

 21. Analyse the code below for a Binary Search. What needs to go on the missing line 6?
def binary_search(item_list,item):
	first = 0
	last = len(item_list)-1
	found = False
	while( first<=last and not found):
		#what needs to go here?
		if item_list[mid] == item :
			found = True
		else:
			if item < item_list[mid]:
				last = mid - 1
			else:
				first = mid + 1	
	return found
	
print(binary_search([1,2,3,5,8], 6))

  mid = first /2

  mid = first + last

  mid = (first + last)//2

  mid = first * last //2

 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

  IF statements

  arrays

  variables

  functions

 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