Comp150 Python Exam 2009

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Zilch
Z
Zilch
Community Contributor
Quizzes Created: 1 | Total Attempts: 124
Questions: 60 | Attempts: 124

SettingsSettingsSettings
Comp150 Python Exam 2009 - Quiz


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.

    Correct Answer
    C. Hellothere
    Explanation
    The output of the code is "Hellothere". This is because the code is concatenating the strings stored in the variables x and y using the + operator. The result is the combination of the two strings, resulting in "Hellothere".

    Rate this question:

  • 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

    Correct Answer
    B. W 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

    Correct Answer
    E. They are all basic types
    Explanation
    The given answer is incorrect. The correct answer is "variable". In Python, "variable" is not a basic type. Instead, it is a placeholder used to store values of different types such as bool, float, and list. The other options mentioned (bool, float, and list) are indeed basic types in Python.

    Rate this question:

  • 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

    Correct Answer
    E. None of the above
    Explanation
    >>> print max(3, 1, abs(-11), 7, 16-10)
    11
    >>>

    Rate this question:

  • 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

    Correct Answer
    E. None of the above
    Explanation
    def print_twice(phrase):
    print phrase, phrase

    def print_four_times(phrase):
    print_twice(phrase +phrase)

    print_twice('there')




    >>>
    there there
    >>>

    Rate this question:

  • 6. 

     

    • A.

      Hello Hello

    • B.

      Hello Goodbye

    • C.

      Goodbye Goodbye

    • D.

      Goodbye Hello

    • E.

      None of the above

    Correct Answer
    B. Hello Goodbye
    Explanation
    >>> Hello Goodbye >>>

    Rate this question:

  • 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

    Correct Answer
    C. X and y
    Explanation
    >>>
    >>> 5 == 5 and 5 == 6
    False
    >>> 5 == 6 or 5 == 5
    True
    >>> 25%2 == 10%3
    True
    >>> 'compl50' == 'easy'
    False
    >>>

    Rate this question:

  • 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

    Correct Answer
    D. Y
    Explanation
    >>>
    >>> varl = True
    >>> var2 = False
    >>> var3 = True
    >>> varl and (var2 or var3) and (not var2)
    True
    >>> varl or var2
    True
    >>> not (varl or var2)
    False
    >>> varl and (var2 or var3)
    True
    >>>

    Rate this question:

  • 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

    Correct Answer
    E. None of the above
    Explanation
    >>>
    >>> var1 = 10
    >>> var2 = 5
    >>> var3 = 7
    >>> (var1> (var2> (var2> ((var1>

    Rate this question:

  • 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

    Correct Answer
    E. They all produce different Output
    Explanation
    >>>
    >>> n = 99
    >>> if 0>> if 0> if 0 < n < 10:
    print 'n is a positive single digit'


    >>> n = -99
    >>> if 0>> if 0> if 0>

    Rate this question:

  • 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

    Correct Answer
    B. W, y and z
    Explanation
    5.6. Programming with style
    Readability is very important to programmers, since in practice programs are read and modified far more often than they are written. All the code examples in this book will be consistent with the Python Enhancement Proposal 8 (PEP 8), a style guide developed by the Python community.

    We’ll have more to say about style as our programs become more complex, but a few pointers will be helpful already:

    use 4 spaces for indentation
    imports should go at the top of the file
    separate function definitions with two blank lines
    keep function definitions together
    keep top level statements, including function calls, together at the bottom of the program

    Rate this question:

  • 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

    Correct Answer
    A. W and x
    Explanation
    The correct answer is w and x. This means that in regards to unit testing, it is true that you can't test a function if you don't know what the output should be (w), and unit testing guarantees the correctness of a function (x).

    Rate this question:

  • 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

    Correct Answer
    A. X, y and z
    Explanation
    Random access memory (RAM) is volatile, meaning that its contents are lost when power is turned off or interrupted. A hard drive is an example of non-volatile memory, as the data stored on it remains even when power is removed. Data on non-volatile storage, such as a hard drive, is stored in named locations called files. Therefore, the statements x, y, and z are all true.

    Rate this question:

  • 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

    Correct Answer
    A. W and x
  • 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.

    Correct Answer
    A. A module.
    Explanation
    A namespace is a syntactic container which permits the same name to be used in different modules or functions. It helps avoid naming conflicts and allows for better organization of code.

    Rate this question:

  • 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

    Correct Answer
    A. 3 5
    Explanation
    The output of the program is "3 5" because the value of "a" is initially set to 5. Then, the value of "b" is assigned the value of "a", which is 5. Finally, the value of "a" is reassigned to 3. Therefore, when we print the values of "a" and "b", we get "3 5".

    Rate this question:

  • 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.

    Correct Answer
    A. This code always returns False.
    Explanation
    The given code `x = x + 1` does not involve any conditional statements or comparisons. Therefore, it does not return a boolean value like True or False. Instead, it increments the value of the variable `x` by 1. Hence, the answer "This code always returns False" is incorrect. The correct explanation is that the code increments the value of `x` by 1.

    Rate this question:

  • 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

    Correct Answer
    A. 1 2 3 4 5
    Explanation
    The code defines a function called countdown that takes an argument n. Inside the function, there is a while loop that continues as long as n is greater than or equal to 0. Inside the loop, it prints the value of n and then subtracts 1 from it. After the loop, it prints the final value of n.

    When the countdown function is called with an argument of 5, it will print the numbers 5, 4, 3, 2, 1, and then the final value of n which is 0. Therefore, the output of the code is "1 2 3 4 5 0".

    Rate this question:

  • 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

    Correct Answer
    A. 5 16 8 4 2
    Explanation
    The code defines a function called collatz_sequence that takes a number n as input. It then enters a while loop that continues until n is equal to 1. Inside the loop, it prints the current value of n. If n is divisible by 2, it updates n to be n divided by 2. Otherwise, it updates n to be n multiplied by 3 and then added by 1.

    When collatz_sequence(5) is called, the output will be 5 16 8 4 2. This is because 5 is not divisible by 2, so it is updated to be 5 multiplied by 3 and then added by 1, resulting in 16. 16 is then divided by 2 to give 8, and so on until n becomes 1.

    Rate this question:

  • 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

    Correct Answer
    A. False False
    Explanation
    The code defines a function called "test_while" that takes an argument "n". Inside the function, there is a while loop that iterates as long as "i" (initialized as "n") is greater than 0. In each iteration, the remainder of dividing "i" by 10 is calculated and stored in the variable "remainder". If "remainder" is equal to 0, the variable "num_zeros" is incremented by 1. If "num_zeros" becomes equal to 3, the function returns True. After each iteration, "i" is divided by 10. Finally, the code prints the result of calling the "test_while" function with the arguments 123456 and 1002. Since there are no zeros in either number, the output is False False.

    Rate this question:

  • 21. 

    Which of the following statements are true in the context of Graphical User Interface (GUI) programming?x -  An event loop typically gets events from the user, processes events and causes windows to be redrawn.y - Callbacks are functions that are called when a GUI event occurs.z - In Tkinter, widgets must be created and placed in the root window to appear on the screen. 

    • A.

      X

    • B.

      X and y

    • C.

      X and z

    • D.

      Y and z

    • E.

      X and y and z

    Correct Answer
    A. X
    Explanation
    In the context of Graphical User Interface (GUI) programming, statement x is true. An event loop is responsible for receiving events from the user, processing those events, and triggering the redrawing of windows.

    Rate this question:

  • 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

    Correct Answer
    A. X
    Explanation
    The function definition allows for two optional parameters: prompt and convert. In the given answer, only the first call to read_value_type, "x", is a valid call because it does not provide any arguments and uses the default values for both prompt and convert. The other calls, "y" and "z", provide arguments for the prompt parameter but do not provide an argument for the convert parameter, which would result in an error.

    Rate this question:

  • 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

    Correct Answer
    A. X
    Explanation
    The statement "x" is true because the function say_hi is the callback that gets called when the hello_button is pressed. However, the statements "y" and "z" are false. The message "hi there" is not output to the console when the hello_button is pressed (statement "y"), and a dialog box with the message "hi there" is not popped up when the hello_button is pressed (statement "z").

    Rate this question:

  • 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

    Correct Answer
    A. Gvar has not changed 1var has not changed
    Explanation
    The output of the code is "gvar has not changed" and "1var has not changed" because the global_variable_func() function does not modify the values of the variables gvar and 1var. The function only changes the value of the local variable var. Therefore, when the print statements are executed, the values of gvar and 1var remain the same as before the function call.

    Rate this question:

  • 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.

    Correct Answer
    A. The ball appears on the gameCanvas at the position (109, 109).
    Explanation
    The code provided creates a ball on a canvas and uses a while loop to continuously update the position of the ball. The initial position of the ball is set to (10, 10), and it moves across and down the canvas with a step size of 1 in both the x and y directions. The loop runs for 100 iterations, and each time it updates the position of the ball by adding the step size to the current position. Therefore, the ball slowly moves across and down the canvas until it comes to rest at the position (109, 109).

    Rate this question:

  • 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

    Correct Answer
    A. X
  • 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

    Correct Answer
    A. X
    Explanation
    Using symbolic constants makes code easier to read. Symbolic constants are named values that are assigned to a constant variable. By using symbolic constants, it becomes easier to understand the purpose and meaning of the values used in the code. It also improves code readability by providing descriptive names rather than using literal values.

    Rate this question:

  • 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

    Correct Answer
    A. X
    Explanation
    The correct answer is x. This is because the code fragment is binding the function do_something to the event caused by the user pressing the k key. This means that whenever the user presses the k key, the function do_something will be executed.

    Rate this question:

  • 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.

    Correct Answer
    A. Converting a number into a product of prime factors.
  • 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

    Correct Answer
    A. A Python function that converts a value into a variable.
    Explanation
    Abstraction in programming refers to the process of simplifying complex systems by breaking them down into smaller, more manageable parts. It involves hiding unnecessary details and only exposing the essential features. The given answer, "A Python function that converts a value into a variable," aligns with this concept of abstraction. By using a function in Python, one can encapsulate a set of instructions or operations and convert a value into a variable, which can then be used in the program. This abstraction helps in organizing and structuring the code, making it easier to understand and maintain.

    Rate this question:

  • 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

    Correct Answer
    C. N
    Explanation
    >>> fruit = 'banana'
    >>> print fruit [2]
    n
    >>>

    Rate this question:

  • 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

    Correct Answer
    E. None of the above
    Explanation
    >>> fruit = 'banana'
    >>> print fruit[-2]
    n
    >>>

    Rate this question:

  • 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

    Correct Answer
    E. None of the above
    Explanation
    fruit = 'banana'
    n = 0
    for char in fruit:
    if char==' a':
    n += 1
    else:
    n -= 1
    print n


    >>>
    -6
    >>>

    Rate this question:

  • 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

    Correct Answer
    E. None of the above
    Explanation
    fruit = 'banana'
    print fruit[1:5]

    >>>
    anan
    >>>

    Rate this question:

  • 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

    Correct Answer
    C. Zebra comes before aardvark
    Explanation
    >>>
    Zebra comes before aardvark
    >>>

    Rate this question:

  • 36. 

    What is the output of the following code?fruit = 'banana'fruit[0] = 'B'print fruit 

    • 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

    Correct Answer
    C. Traceback (most recent call last): File "", line 2, in TypeError: 'str' object does not support item assignment
    Explanation
    >>>

    Traceback (most recent call last):
    File "/Users/imac/OneDrive/OneDriveBusiness/COMP150/Previous Exams/Untitled.py", line 2, in
    fruit[0] = 'B'
    TypeError: 'str' object does not support item assignment
    >>>

    Rate this question:

  • 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

    Correct Answer
    B. Orange
    Explanation
    >>>
    orange
    >>>

    Rate this question:

  • 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

    Correct Answer
    A. I am Alice and I am 10 years old.
    Explanation
    >>>
    I am Alice and I am 10 years old.
    >>>

    Rate this question:

  • 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

    Correct Answer
    A. Create a string which is at most 5 characters wide, is left justified, and contains the number n.
    Explanation
    The string format conversion specification '%-5d' % n creates a string that is at most 5 characters wide, is left justified, and contains the number n. The '-' sign indicates left justification, and the '5' specifies the minimum width of the string. Since it is at most 5 characters wide, the string can be less than 5 characters if the number n is smaller.

    Rate this question:

  • 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

    Correct Answer
    E. None of the above
    Explanation
    >>>
    5
    >>>

    Rate this question:

  • 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

    Correct Answer
    A. ['B', 'a', 'n', 'a', 'n', 'a']
    Explanation
    >>>
    B_a_n_a_n_a
    >>>

    Rate this question:

  • 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

    Correct Answer(s)
    E. None of the above
    F. None of the above
    Explanation
    >>>
    0 2 4 6 8 10 12 14 16 18
    >>>

    Rate this question:

  • 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

    Correct Answer
    D. [1, 4, 9, 16, 25]
    Explanation
    >>>
    [1, 4, 9, 16, 25]
    >>>

    Rate this question:

  • 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

    Correct Answer
    C. ['Hello', 2, 3, 4, 5]
    Explanation
    >>>
    ['Hello', 2, 3, 4, 5]
    >>>

    Rate this question:

  • 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

    Correct Answer
    A. X any y
    Explanation
    The given correct answer is "x and y". This means that both statements x and y are true. Statement x states that persistent changes to a function's parameters are called side effects, which is true. Statement y states that pure functions do not produce side effects, which is also true. Therefore, the correct answer is x and y.

    Rate this question:

  • 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

    Correct Answer
    E. None of the above
    Explanation
    >>>
    [4, 5, 6]
    >>>

    Rate this question:

  • 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

    Correct Answer
    B. 12
    Explanation
    >>>
    12
    >>>

    Rate this question:

  • 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

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

    Traceback (most recent call last):
    File "/Users/imac/OneDrive/OneDriveBusiness/COMP150/Previous Exams/Untitled.py", line 2, in
    items[0] = -1
    TypeError: 'tuple' object does not support item assignment
    >>>

    Rate this question:

  • 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

    Correct Answer
    C. 3 4 5
    Explanation
    >>>
    3 4 5
    >>>

    Rate this question:

  • 50. 

    Which of the following statements are true? 
    • x -  Tuples are more efficient to use than lists.
    • y -  Tuple assignment is an example of poor programming style.
    • z -  Tuples can be used as dictionary keys. 

    • A.

      X and y

    • B.

      X and z

    • C.

      Y and z

    • D.

      X and y and z

    • E.

      None of the above

    Correct Answer
    A. X and y
    Explanation
    Tuples are more efficient to use than lists because tuples are immutable, meaning they cannot be changed once created. This immutability allows tuples to be accessed and processed faster than lists. Tuple assignment is an example of poor programming style because it can make the code less readable and harder to understand. Tuples can be used as dictionary keys because they are hashable, unlike lists which are mutable and cannot be used as keys in a dictionary. Therefore, the correct answer is x and y.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Apr 07, 2024
    Quiz Edited by
    ProProfs Editorial Team
  • May 25, 2015
    Quiz Created by
    Zilch

Related Topics

Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.