Preview

02 - Python (Intermediate)

 1. Which of the following is correct about Python?

  Python is a high-level, interpreted, interactive and object-oriented scripting language.

  All the answers listed in this selection are correct.

  It uses English keywords frequently where as other languages use punctuation.

  Python is designed to be highly readable.

 2. Analyse the following code that uses operators. What is the output for lines 4,5,6?
def main():
  x=3
  y=2
  print(x//y)
  print(x%y)
  print(x**y)

main()

  1.5,1,5

  1,0,6

  1,1,9

  1,2,6

 3. In Python ________ are immutable while a list is mutable. It means once you create ________you cannot edit or make changes in the values while in a list you can.

  variables

  constants

  matrices

  tuples

 4. What is the output of the following code?
tinylist = [123, 'john']
print(tinylist * 2)
 

  [tinylist,tinylist]

  [123, 'john', 123, 'john']

  [246,johnjohn]

  Error

 5. What is the output of len([1, 2, 3])?

  3

  2

  1

  6

 6. What is the output of the following code? (refer particularly to line 5?what does this code do?)
def mysteryfunction(numbers):
   x = 0
   for num in numbers:
      x+=num
   print("x:", x)

mysteryfunction((1,2,3,4))

  10 > It simply adds up all the numbers that are passed to the function, in this case: 1,2,3,4

  5 > It increments (using +1) the last number passed to the function. E.g. 4+1 = 5

  0 > It disregards the variables passed to it, and sets x to 0, returning 0

  8 > It takes the last parameter that is passed to the function and doubles it (4 x 2 = 8)

 7. What is occuring on line 2 of the following code?
list = ['physics', 'chemistry', 2019, 2020];
list[2] = 2021;
print(list)

  The value "chemistry" in the list is being replaced with "2"

  The value "2019" in the list is being replaced by "2021"

  The value "2019" in the list is being replaced by the value "2"

  The second item in the list is being deleted

 8. The following code should output 0,1,2 but as it stands, it does not produce an output. What needs to go in line 3 for it to output 0,1,2?
for i in range(7):
  if i==3: 
    #What needs to go here?
  print(i)

  i=i-1

  continue

  i=i+1

  break

 9. The output of the following code (that should simulate output for the fibonacci sequence) is 1,1,1,1,1, but we want it to be, 1,1,2,3,5. What one thing in the code needs to change to correct the code?
a = 1
b = 1
for c in range(1,6):
    print (a)
    n = a + b
    a = b
    b = a
print ("")

  Line 5 should be: n = a + n

  Line 7 should be : b = n

  Line 6 should be : a = n

  Line 7 should be: b = n + a

 10. What will the following code output?
for x in range(3,4):
    for y in range(1, 11):
        print ('%d * %d = %d' % (x, y, x*y))

  An infinite loop that prints the numbers 3 and 4 ten times repeatedly

  The addition of all numbers between 3 and 4 and 1 and 11

  The four times table up until 11 (e.g 4 x 1 = 4 up to 4 x 11 = 44)

  The three times table from 1 to 10 (e.g. 3 x 1 = 3 up to 3 x 10 = 30)

 11. Fill in the blanks. What needs to be added on line 3 (after the second 'next') in order to get rid of any blank lines?
next=f.readline()
while next !="":
        next=next.___________()
        print(next)
        next=f.readline()

  newline

  blank

  strip

  line

 12. Fill in the blanks with the code that ought to go on line 4 (to complete the Bubble sort).
def bubbleSort(nlist):
    for passnum in range(len(nlist)-1,0,-1):
        for i in range(passnum):
            ___________________________
                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)

  if nlist[0]>nlist[1]:

  if nlist[i]=nlist[i+1]:

  if nlist[i+1]>nlist[i]:

  if nlist[i]>nlist[i+1]:

 13. In the following binary search, searching for the string "anton" returns 'false' even though "anton" is indeed in the list. Why?
def binary_search(item_list,item):
 first = 0
 last = len(item_list)-1
 found = False
 while( first<=last and not found):
  mid = (first + last)//2
  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(["adam","beth","seth","xavier","anton"], "anton"))

  Because the list is not in alphabetical order and a binary search requires this to work.

  Because line 6 has been coded incorrectly. It should be: mid = (first + last)

  Because line 10 should be: if item != item_list[mid]:

  Because line 7 should be: if item_list[mid] > item :

 14. The following code shows the creation of a 2d array in Python. What is the output of the code?
matrix=[[1,2,3],[10,20,30]]
i=0
j=2
print(matrix[i][j])

 15. What is the output of the following code?
for x in range(2, 6):
  print(x) 

  2,4,6

  2,4,6,8

  2,6,16,60

  2,3,4,5