Comp150 Python Exam 2009

60 Questions | Total Attempts: 65

SettingsSettingsSettings
Please wait...
Python Programming Quizzes & Trivia

Questions and Answers
  • 1. 
    What is the output of the following code? x =  'Hello'y = 'there'print  x+y
    • A. 

      'Hello there'

    • B. 

      Hello there

    • C. 

      Hellothere

    • D. 

      Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for +: 'str' and 'str'

    • E. 

      None of the above.

  • 2. 
    Which of the following assignment statements are syntactically valid? w   -   my_name = "Brendan" x   -    76trombones = "big parade"y   -    more$ = 10000z   -    my_salary = 10, 000  
    • A. 

      W

    • B. 

      W and z

    • C. 

      X and y

    • D. 

      X, y and z

    • E. 

      All of w, x, y and z

  • 3. 
    Which of the following is not a Python basic type? 
    • A. 

      Bool

    • B. 

      Float

    • C. 

      List

    • D. 

      Variable

    • E. 

      They are all basic types

  • 4. 
    What is the output of the following code?max(3, 1, abs(-11), 7, 16-10) 
    • A. 

      16-10

    • B. 

      10

    • C. 

      7

    • D. 

      1

    • E. 

      None of the above

  • 5. 
    What is the value of the parameter in the call to print_twice in the following code?def print_twice(phrase):print phrase, phrasedef print_four_times(phrase):print_twice(phrase +phrase)print_four_times('there') 
    • A. 

      'there'

    • B. 

      'therethere'

    • C. 

      'therethere therethere'

    • D. 

      'there there there there'

    • E. 

      None of the above

  • 6. 
    • A. 

      Hello Hello

    • B. 

      Hello Goodbye

    • C. 

      Goodbye Goodbye

    • D. 

      Goodbye Hello

    • E. 

      None of the above

  • 7. 
    Which of the following boolean expressions are True?w    -   5 == 5 and 5 == 6x   -    5 == 6 or 5 == 5y    -   25%2 == 10%3 z    -   'compl50' == 'easy' 
    • A. 

      W and x

    • B. 

      W and y

    • C. 

      X and y

    • D. 

      X and z

    • E. 

      All of w,x,y and z

  • 8. 
    Given the following code:varl =  Truevar2 =  Falsevar3 =  Truewhich of the following boolean expressions are False?w   -   varl and (var2 or var3) and (not var2)x   -   varl or var2y   -   not (varl or var2)z   -   varl and (var2 or var3) 
    • A. 

      X and y

    • B. 

      W

    • C. 

      Y and z

    • D. 

      Y

    • E. 

      None of w, x, y and z

  • 9. 
    Given the following code:var1 = 10var2 = 5var3 = 7which of the following boolean expressions are True?w   -   (var1<var2) or (var2<var3)x   -   (var2<var3) and (var1<var2)y   -   (var2<var1) and (var1<var2) z   -   ((var1<var2) or (var2<var3)) and ((var1<20) and (var2>5)) 
    • A. 

      W and x

    • B. 

      X

    • C. 

      Y and z

    • D. 

      Z

    • E. 

      None of the above

  • 10. 
    Which of the following code snippets produce the same output regardless of the value of n?xif 0<n:if n<10:print 'n is a positive single digit' yif 0<n and n<10: print 'n is a positive single digit' zif 0 < n < 10:print 'n is a positive single digit' 
    • A. 

      X and y

    • B. 

      X and z

    • C. 

      Y and z

    • D. 

      X, y and z

    • E. 

      They all produce different Output

  • 11. 
    In regards to programming style, which of the following statements are true?w   -   you should use 4 spaces for indentationx   -   imports should go at the top of the filey   -   variable names should be as short as possiblez   -   you should keep function definitions together 
    • A. 

      W, x and z

    • B. 

      W, y and z

    • C. 

      X, y and z

    • D. 

      All of w, x, y and z

    • E. 

      None of w, x, y and z

  • 12. 
    In regards to unit testing, which of the following statements are true?w - you can't test a function if you don't know what the output should bex - unit testing guarantees the correctness of a functiony - unit testing improves the likelihood that a function is correctz - unit testing solves syntax errors 
    • A. 

      W and x

    • B. 

      X and y

    • C. 

      W and z

    • D. 

      Y and z

    • E. 

      W and y

  • 13. 
    Which of the following statements are true?w - to access random access memory, programs must read and write filesx - random access memory is volatiley - a hard drive is an example of non-volatile memoryz - data on non-volatile storage is stored in named locations called files 
    • A. 

      X, y and z

    • B. 

      W, x and y

    • C. 

      W, y and z

    • D. 

      W, y and z

    • E. 

      All of w, x, y and z

  • 14. 
    Which of the following Python programs run without producing an error? wimport mathprint pi ximport mathprint math.pi yfrom math import *print math.pi zfrom math import piprint pi 
    • A. 

      W and x

    • B. 

      W and y

    • C. 

      W and z

    • D. 

      X and y

    • E. 

      X and z

  • 15. 
    A namespace is: 
    • A. 

      A module.

    • B. 

      A special Python file where all the keywords are defined.

    • C. 

      A type, like a list, that is used to store names.

    • D. 

      A syntactic container which permits the same name to be used in different modules or functions.

    • E. 

      None of the above.

  • 16. 
    What is the output of the following program?a = 5b = aa = 3print a, b 
    • A. 

      3 5

    • B. 

      3 3

    • C. 

      5 3

    • D. 

      5 5

    • E. 

      None of the above

  • 17. 
    What is the best explanation for the following code (assume x is an int)?x = x + 1 
    • A. 

      This code always returns False.

    • B. 

      This code always returns True

    • C. 

      The code has a syntax error.

    • D. 

      The code has a semantic error.

    • E. 

      The variable x is incremented by 1.

  • 18. 
    What is the output of the following code? def countdown(n):while n>=0:print n,n =  n-1print ncountdown(5)  
    • A. 

      1 2 3 4 5

    • B. 

      5 4 3 2 1

    • C. 

      5 4 3 2 1 0 -1

    • D. 

      5 4 3 2 1 0

    • E. 

      0 1 2 3 4 5

  • 19. 
    What is the output of the following code?def collatz_sequence(n):while n != 1:print n,if n%2 == 0: n = n/2else:n = n*3+1 collatz_sequence(5) 
    • A. 

      5 16 8 4 2

    • B. 

      5 16 8 4 2 1

    • C. 

      5 2 7 3

    • D. 

      16 8 4 2

    • E. 

      None Of the above

  • 20. 
    What is the output of the following code?def test_while(n):i = nnum_zeros = 0while i>0:remainder = i%10if remainder ==0:num_zeros = num_zeros+1if num_zeros==3:return Truei = i/10return Falseprint test_while(123456), test_while(1002)  
    • A. 

      False False

    • B. 

      True False

    • C. 

      False True

    • D. 

      True True

    • E. 

      None of the above

  • 21. 
    • A. 

      X

    • B. 

      X and y

    • C. 

      X and z

    • D. 

      Y and z

    • E. 

      X and y and z

  • 22. 
    Consider the following function definition:def read_value_type(prompt='Enter a value> ', convert=float)val= input(prompt)return convert(val)  Which of the following are valid calls to read_value_type? x val = read_value_type ()  y val read_value_type(prompt='Enter a float> ') zval read_value_type(convert=bool, prompt='Enter a boolean> ') 
    • A. 

      X

    • B. 

      X and y

    • C. 

      X and z

    • D. 

      Y and z

    • E. 

      X and y and z

  • 23. 
    Consider the following code:from Tkinter import *def say_hi () :print "hi there"root = Tk ()hello button = Button(root, text="Hello", command=say_hi)hello_button.grid(row=O, column=O) root. mainloop () Which of the following statements are true? xsay_hi is the callback that gets called when the hello_button is pressed. yThe message "hi there" is output to the console when the hello...button is pressed.  zA dialog box with the message "hi there" is popped up when the hello_button is pressed.  
    • A. 

      X

    • B. 

      X and y

    • C. 

      X and z

    • D. 

      Y and z

    • E. 

      X and y and z

  • 24. 
    What is the output of the following code?def global_variable_func():global gvargvar = "changed"1var = "changed"  gvar =  "not changed"1var = "not changed"global_variable_func()print "gvar has", gvarprint "1var has", 1var 
    • A. 

      Gvar has not changed 1var has not changed

    • B. 

      Gvar has changed 1var has changed

    • C. 

      Gvar has not changed 1var has changed

    • D. 

      Gvar has changed 1var has not changed

    • E. 

      None of the above

  • 25. 
    What is the effect of the following code?from Tkinter import *root = Tk ()gameCanvas = Canvas(root, width=200, height=200)gameCanvas.grid(row=0,column=0)x = 10y = 10dx = 1dy = 1ball gameCanvas.create_oval(x, y, x+10, y+10, fill='blue')i = 1while i<100: x += dxy += dygameCanvas.coords(ball, x, y, x+10, y+10 )i += 1 root . mainloop () 
    • A. 

      The ball appears on the gameCanvas at the position (109, 109).

    • B. 

      The ball initially appears on gameCanvas at the position (10, 10) and slowly moves across and down the canvas until it comes to rest at (109, 109).

    • C. 

      The ball initially appears on gameCanvas at the position (11, 11) and slowly moves across and down the canvas until it comes to rest at (109, 109).

    • D. 

      The ball initially appears on gameCanvas at the position (11, 11) and slowly moves across and down the canvas until it comes to rest at (110, 110).

    • E. 

      None of the above.

  • 26. 
    In regard to random numbers, which of the following statements are true?xRandom numbers are generated in most computers using a deterministic pseudo-random number generator.  yRandom numbers are generated in most computers with radioactive isotopes.  zGenuine random numbers can be produced by the decay of radioactive iso-topes. 
    • A. 

      X

    • B. 

      X and y

    • C. 

      X and z

    • D. 

      Y and z

    • E. 

      X and y and z

  • 27. 
    Which of the following statements are true?
    • x  - Using symbolic constants makes code easier to read.
    • y -  Using symbolic constants usually makes programs shorter.
    • z -  Using symbolic constants makes code easier to modify. 
    • A. 

      X

    • B. 

      X and y

    • C. 

      X and z

    • D. 

      Y and z

    • E. 

      X and y and z

  • 28. 
    Which of the following statements are true with respect to the following code frag-ment using Tkinter? root .bind ( 'k', do_something)  
    • x -  The function do-something is bound to the event caused by the user pressing the k key. 
    • y -  The variable do-something is set to 'k' within the root window. 
    • z -  do-something and 'k' are aliases of each other. 
    • A. 

      X

    • B. 

      X and y

    • C. 

      X and z

    • D. 

      Y and z

    • E. 

      X and y and z

  • 29. 
    In the context of programming, refactoring is: 
    • A. 

      Converting a number into a product of prime factors.

    • B. 

      The process of reorganising code to make it easier to understand, read and maintain.

    • C. 

      Splitting a large section of source code into multiple smaller functions.

    • D. 

      Combining multiple smaller functions into a single large section of source code.

    • E. 

      None of the above.

  • 30. 
    In the context of programming, abstraction is: 
    • A. 

      A Python function that converts a value into a variable.

    • B. 

      The process of revealing the details of a function.

    • C. 

      The process of making functions less general and more specific.

    • D. 

      A data structure for associating keys with values.

    • E. 

      None of the above

  • 31. 
    What is the output of the following code? fruit = 'banana'print fruit [2]
    • A. 

      B

    • B. 

      A

    • C. 

      N

    • D. 

      Bananabanana

    • E. 

      None of the above

  • 32. 
    What is the output of the following code?fruit = 'banana'print fruit[-2] 
    • A. 

      Traceback (most recent call last): File "", line1, in IndexError: string index out of range

    • B. 

      A

    • C. 

      B

    • D. 

      Bana

    • E. 

      None of the above

  • 33. 
    What is the output of the following code?fruit = 'banana'n = 0 for char in fruit:if char==' a':n += 1else:n -= 1print n 
    • A. 

      -1

    • B. 

      0

    • C. 

      1

    • D. 

      2

    • E. 

      None of the above

  • 34. 
    What is the output of the following code?fruit = 'banana'print fruit[1:5] 
    • A. 

      Banan

    • B. 

      Bana

    • C. 

      Anana

    • D. 

      Nana

    • E. 

      None of the above

  • 35. 
    What is the output of the following code?v1 = 'Zebra'v2 = 'aardvark'if v1<v2:print v1, 'comes before', v2else:print v2, 'comes before', v1
    • A. 

      Zebra equals aardvark

    • B. 

      Aardvark comes before Zebra

    • C. 

      Zebra comes before aardvark

    • D. 

      Traceback (most recent call last): File "", line 1, in TypeError: can't compare type str and type str

    • E. 

      None of the above

  • 36. 
    • A. 

      Banana

    • B. 

      Traceback (most recent call last): File "", line 2, in IndexError: string index out of range

    • C. 

      Traceback (most recent call last): File "", line 2, in TypeError: 'str' object does not support item assignment

    • D. 

      B

    • E. 

      None of the above

  • 37. 
    What is the output of the following code?fruit = 'banana'fruit = 'orange'print fruit 
    • A. 

      Banana

    • B. 

      Orange

    • C. 

      Traceback (most recent call last): File "", line 2, in TypeError: 'str' object does not support item assignment

    • D. 

      Traceback (most recent call last): File "", line 2, in TypeError: 'str' variables are immutable

    • E. 

      None of the above

  • 38. 
    What is the output of the following code?name = 'Alice'age = 10 print 'I am %s and I am %d years old.' % (name, age) 
    • A. 

      I am Alice and I am 10 years old.

    • B. 

      I am %s and I am %d years old. % (Alice, 10)

    • C. 

      I am %s and I am %d years old. % (name, age)

    • D. 

      I am name and I am age years old.

    • E. 

      None of the above

  • 39. 
    What is the meaning of the string format conversion specification' %-5d' % n? 
    • A. 

      Create a string which is at most 5 characters wide, is left justified, and contains the number n.

    • B. 

      Create a string which is exactly 5 characters wide, is right justified, and contains the number n.

    • C. 

      Create a string which is at least 5 characters wide, is left justified, and contains the number n.

    • D. 

      Create a string which is at least 5 characters wide, is right justified, and contains the string n.

    • E. 

      None of the above

  • 40. 
    What is the output of the following code?alist = [1,2,3,4]blist = alistalist[0] = 5print blist [0] 
    • A. 

      [1, 2, 3, 4]

    • B. 

      [5, 2, 3, 4]

    • C. 

      [5, 1, 2, 3, 4]

    • D. 

      Traceback (most recent call last): File "", line 2, in TypeError: 'list' object does not support item assignment

    • E. 

      None of the above

  • 41. 
    What is the output of the following code?fruit = [ 'b', 'a', 'n' , 'a', 'n' , 'a' ]fruit[0] = 'B'print "_".join(fruit)
    • A. 

      ['B', 'a', 'n', 'a', 'n', 'a']

    • B. 

      Traceback (most recent call last): File "", line 2, in TypeError: 'list' object does not support items assignment

    • C. 

      B_a_n_a_n_a

    • D. 

      Banana

    • E. 

      None of the above

  • 42. 
    What is the output of the following code?for number in range(20):if number %2 == 0:print number,print 
    • A. 

      2 4 6 8 10 12 14 15 18 20

    • B. 

      0 2 4 6 8 10 12 14 16 18 20

    • C. 

      0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

    • D. 

      1 3 5 7 9 11 13 15 17 19

    • E. 

      None of the above

    • F. 

      None of the above

  • 43. 
    What is the output of the following code?numbers = [1, 2, 3, 4, 5]for index, value in enumerate(numbers):numbers[index] = value**2print numbers 
    • A. 

      [1, 2, 3, 4, 5]

    • B. 

      [2, 4, 6, 8, 10]

    • C. 

      [0, 1, 4, 9, 16]

    • D. 

      [1, 4, 9, 16, 25]

    • E. 

      None of the above

  • 44. 
    What is the output of the following code?def change_list(param_list):param_list[0] = "Hello" alist = [1, 2, 3, 4, 5]change_list(alist)print alist 
    • A. 

      [1, 2, 3, 4, 5]

    • B. 

      ['H', 'e', 'l', 'l', 'o']

    • C. 

      ['Hello', 2, 3, 4, 5]

    • D. 

      Traceback (most recent call last): File "", line 2, in TypeError: 'list' object does not support multiple component types

    • E. 

      None of the above

  • 45. 
    Which of the following statements are true? 
    • x -  Persistent changes to a function's parameters are called side effects.
    • y -  Pure functions do not produce side effects.
    • z -  It is good programming practice to use pure functions as much as possible.
    • A. 

      X any y

    • B. 

      X and z

    • C. 

      Y and z

    • D. 

      X and y and z

    • E. 

      None of the above

  • 46. 
    What is the output of the following code?matrix= [[1,2,3],[4,5,6],[7,8,9]]print matrix[1] 
    • A. 

      1

    • B. 

      2

    • C. 

      4

    • D. 

      [1, 2, 3]

    • E. 

      None of the above

  • 47. 
     What is the output of the following code?def sum_elements(numbers):sum = 0for item in numbers:if type(item)==list:for item2 in item:sum += item2return sum print sum_elements([[1,2], 3, [4,5]]) 
    • A. 

      15

    • B. 

      12

    • C. 

      3

    • D. 

      9

    • E. 

      None of the above

  • 48. 
    What is the output of the following code?items = (1, 3, 5, 7, 9)items[0] = -1print items[0] 
    • A. 

      1

    • B. 

      -1

    • C. 

      Traceback (most recent call object): File "tuples.py", line 2, in items[0] = -1 TypeError: 'tuple' object does not support item assignment

    • D. 

      9

    • E. 

      None of the above

  • 49. 
    What is the output of the following code?def swap (x, y) :x, y = y, x x = 3y = 4z = 5swap(x,y)swap(y,z)print x, y, z  
    • A. 

      5 4 3

    • B. 

      4 5 3

    • C. 

      3 4 5

    • D. 

      3 5 4

    • E. 

      None of the above

  • 50. 
    • A. 

      X and y

    • B. 

      X and z

    • C. 

      Y and z

    • D. 

      X and y and z

    • E. 

      None of the above

  • 51. 
    What is the output of the following code?a = set("banana")b = set ("apple")print a & b
    • A. 

      Bananaapple

    • B. 

      Banana & apple

    • C. 

      Set 9['s', 'p', 'b', 'e', 'l', 'n'])

    • D. 

      Set('bananaapple')

    • E. 

      None of the above

  • 52. 
    What is the output of the following code?def matrix_to_sparse(in_matrix):sparse= {}for row_index,row in enumerate(in_matrix):for col_index,val in enumerate(row):if val != 0: sparse[(row_index,col_index)] = valreturn sparse matrix= [[0,0,1], [0,2,0], [3,0,0]] sparse = matrix_to_sparse(matrix)print sparse[(2,0)], sparse[(1,1)], sparse[(0,2)] 
    • A. 

      1 2 3

    • B. 

      1 3 2

    • C. 

      3 1 2

    • D. 

      2 1 3

    • E. 

      None of the above

  • 53. 
    What is the output of the following code?def count(strng):counts= {}for ch in strng:counts[ch] =  counts.get(ch,0)+1return counts counts= count("Hello, Hello")for ch in "Hello":print counts[ch],print 
    • A. 

      2 2 4 4 2

    • B. 

      1 1 1 1 1

    • C. 

      1 1 1 2 1

    • D. 

      1 1 2 1

    • E. 

      None of the above

  • 54. 
    Given the following code in file argv.py:import sysstrings= sys.argv[1:]for index, value in enumerate(strings):strings[index] = float(value) print sum(strings)What is the output of the following program invocation?$ python argv.py 3 4 5 
    • A. 

      Traceback (most recent call object): File "argv.py", line 7, in print sum(strings) TypeError: unsupported operand type(s) for +: 'int' and 'str

    • B. 

      12.0

    • C. 

      3.0

    • D. 

      9.0

    • E. 

      None of the above

  • 55. 
    What will the variable programs contain in the following code fragment?import globprograms = glob.glob("*.py") 
    • A. 

      A list of names of all files ending in '.py' in the current working directory.

    • B. 

      A list of names of all files ending in '.py' on the whole computer.

    • C. 

      A reference to the file *. py.

    • D. 

      The string' glob. glob ( "*. py") '.

    • E. 

      None of the above

  • 56. 
    Which of the following statements are true? ‚Äč
    • x - In procedural programming, the focus is on writing functions which operate on data.
    • y - In object-oriented programming, the focus is on the creation of objects which contain both data and functionality together.
    • z - A functional programming style involves writing programs where functions do not cause side-effects. 
    • A. 

      X and y

    • B. 

      X and z

    • C. 

      Y and z

    • D. 

      X and y and z

    • E. 

      None of the above

  • 57. 
    Which of the following statements are true?
    • x - Functions defined within a class are called methods of that class.
    • y - Variables of the class type are called globals.
    • z - The power of object-oriented programming is that we need to know the details of their implementation to use variables of a given class. 
    • A. 

      X and y

    • B. 

      X and z

    • C. 

      Y and z

    • D. 

      X and y and z

    • E. 

      None of the above

  • 58. 
    What is the output of the following code?class Point:def __ init __(self) :self.x = 0self.y = 0 def move(self, dx, dy):self.x += dxself.y += dy def equals(self, p2):return self.x==p2.x and self.y==p2.y p1 = Point()p1.move(2,3)p2 = Point()p2.move(2,3)print p1 == p2, p1.equals(p2) 
    • A. 

      False False

    • B. 

      True False

    • C. 

      True True

    • D. 

      False True

    • E. 

      None of the above

  • 59. 
    What is the output of the following code?import copyclass Point:def __ init __ (self):self.x = 0self.y = 0def move(self, dx, dy):self.x += dxself.y += dyclass Rectangle:def __ init __ (self):self.corner = Point()self.width = 100self.height = 100def move(self, dx, dy):self.corner.move(dx,dy) box1 = Rectangle()box2 = copy.copy(box1)box1.move(20,50)print box1.corner.x, box1.corner.y, box2.corner.x, box2.corner.y 
    • A. 

      20 50 0 0

    • B. 

      0 0 20 50

    • C. 

      20 50 20 50

    • D. 

      50 20 0 0

    • E. 

      None of the above

  • 60. 
    What is the output of the following code? def encrypt (char, rotation):    return chr((ord(char)+ord(rotation))%127) def decrypt (char, rotation):    return chr((ord(char) -ord(rotation))%127) print decrypt (encrypt ('A', 'B'), 'B'),print encrypt (decrypt ('A', 'B'), 'B')
    • A. 

      A A

    • B. 

      A B

    • C. 

      B A

    • D. 

      B B

    • E. 

      None of the above