Comp150 Python Exam 2009

60 Questions | Total Attempts: 66

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.