Comp150 Python Exam 2009

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Zilch
Z
Zilch
Community Contributor
Quizzes Created: 1 | Total Attempts: 129
| Attempts: 129
SettingsSettings
Please wait...
  • 1/60 Questions

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

    • W
    • W and z
    • X and y
    • X, y and z
    • All of w, x, y and z
Please wait...
About This Quiz

This Comp150 Python Exam 2009 tests knowledge of Python programming. It covers syntax, basic types, and function calls, assessing the ability to interpret code outputs and understand Python's structural rules. Suitable for learners seeking to evaluate their Python skills.

Comp150 Python Exam 2009 - Quiz

Quiz Preview

  • 2. 

    Which of the following is not a Python basic type? 

    • Bool

    • Float

    • List

    • Variable

    • They are all basic types

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

  • 3. 

    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 

    • X, y and z

    • W, x and y

    • W, y and z

    • W, y and z

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

  • 4. 

    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 

    • W and x

    • W and y

    • W and z

    • X and y

    • X and z

    Correct Answer
    A. W and x
  • 5. 

    A namespace is: 

    • A module.

    • A special Python file where all the keywords are defined.

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

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

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

  • 6. 

    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> ') 

    • X

    • X and y

    • X and z

    • Y and z

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

  • 7. 

    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. 

    • X

    • X and y

    • X and z

    • Y and z

    • X and y and z

    Correct Answer
    A. X
  • 8. 

    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. 

    • X

    • X and y

    • X and z

    • Y and z

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

  • 9. 

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

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

    • A

    • B

    • Bana

    • None of the above

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

    Rate this question:

  • 10. 

    What is the output of the following code?fruit = 'banana'fruit = 'orange'print fruit 

    • Banana

    • Orange

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

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

    • None of the above

    Correct Answer
    A. Orange
    Explanation
    >>>
    orange
    >>>

    Rate this question:

  • 11. 

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

    • I am Alice and I am 10 years old.

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

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

    • I am name and I am age years old.

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

  • 12. 

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

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

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

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

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

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

  • 13. 

    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 

    • [1, 2, 3, 4, 5]

    • [2, 4, 6, 8, 10]

    • [0, 1, 4, 9, 16]

    • [1, 4, 9, 16, 25]

    • None of the above

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

    Rate this question:

  • 14. 

    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 

    • [1, 2, 3, 4, 5]

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

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

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

    • None of the above

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

    Rate this question:

  • 15. 

     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]]) 

    • 15

    • 12

    • 3

    • 9

    • None of the above

    Correct Answer
    A. 12
    Explanation
    >>>
    12
    >>>

    Rate this question:

  • 16. 

    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  

    • 5 4 3

    • 4 5 3

    • 3 4 5

    • 3 5 4

    • None of the above

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

    Rate this question:

  • 17. 

    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)) 

    • W and x

    • X

    • Y and z

    • Z

    • None of the above

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

    Rate this question:

  • 18. 

    What is the output of the following code? x =  'Hello'y = 'there'print  x+y

    • 'Hello there'

    • Hello there

    • Hellothere

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

    • None of the above.

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

  • 19. 

     

    • Hello Hello

    • Hello Goodbye

    • Goodbye Goodbye

    • Goodbye Hello

    • None of the above

    Correct Answer
    A. Hello Goodbye
    Explanation
    >>> Hello Goodbye >>>

    Rate this question:

  • 20. 

    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' 

    • W and x

    • W and y

    • X and y

    • X and z

    • All of w,x,y and z

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

  • 21. 

    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) 

    • X and y

    • W

    • Y and z

    • Y

    • None of w, x, y and z

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

  • 22. 

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

    • 16-10

    • 10

    • 7

    • 1

    • None of the above

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

    Rate this question:

  • 23. 

    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') 

    • 'there'

    • 'therethere'

    • 'therethere therethere'

    • 'there there there there'

    • None of the above

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

  • 24. 

    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 

    • W and x

    • X and y

    • W and z

    • Y and z

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

  • 25. 

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

    • This code always returns False.

    • This code always returns True

    • The code has a syntax error.

    • The code has a semantic error.

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

  • 26. 

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

    • 1 2 3 4 5

    • 5 4 3 2 1

    • 5 4 3 2 1 0 -1

    • 5 4 3 2 1 0

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

  • 27. 

    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) 

    • 5 16 8 4 2

    • 5 16 8 4 2 1

    • 5 2 7 3

    • 16 8 4 2

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

  • 28. 

    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 () 

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

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

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

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

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

  • 29. 

    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. 

    • X

    • X and y

    • X and z

    • Y and z

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

  • 30. 

    In the context of programming, refactoring is: 

    • Converting a number into a product of prime factors.

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

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

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

    • None of the above.

    Correct Answer
    A. Converting a number into a product of prime factors.
  • 31. 

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

    • B

    • A

    • N

    • Bananabanana

    • None of the above

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

    Rate this question:

  • 32. 

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

    • -1

    • 0

    • 1

    • 2

    • None of the above

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

  • 33. 

    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

    • Zebra equals aardvark

    • Aardvark comes before Zebra

    • Zebra comes before aardvark

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

    • None of the above

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

    Rate this question:

  • 34. 

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

    • Banana

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

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

    • B

    • None of the above

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

  • 35. 

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

    • [1, 2, 3, 4]

    • [5, 2, 3, 4]

    • [5, 1, 2, 3, 4]

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

    • None of the above

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

    Rate this question:

  • 36. 

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

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

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

    • B_a_n_a_n_a

    • Banana

    • None of the above

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

    Rate this question:

  • 37. 

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

    • 1

    • 2

    • 4

    • [1, 2, 3]

    • None of the above

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

    Rate this question:

  • 38. 

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

    • 1

    • -1

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

    • 9

    • None of the above

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

  • 39. 

    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. 

    • X and y

    • X and z

    • Y and z

    • X and y and z

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

  • 40. 

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

    • Bananaapple

    • Banana & apple

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

    • Set('bananaapple')

    • None of the above

    Correct Answer
    A. None of the above
    Explanation
    >>>
    set(['a'])
    >>>

    Rate this question:

  • 41. 

    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 

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

    • 12.0

    • 3.0

    • 9.0

    • None of the above

    Correct Answer
    A. None of the above
    Explanation
    >>>
    0
    >>> $ python argv.py 3 4 5
    SyntaxError: invalid syntax
    >>>

    Rate this question:

  • 42. 

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

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

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

    • A reference to the file *. py.

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

    • None of the above

    Correct Answer
    A. A list of names of all files ending in '.py' in the current working directory.
    Explanation
    The variable "programs" will contain a list of names of all files ending in '.py' in the current working directory. This is because the "glob.glob" function is used to find all files matching a specific pattern, in this case, '*.py'. The function returns a list of file names that match the pattern, and this list is assigned to the variable "programs".

    Rate this question:

  • 43. 

    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. 

    • X and y

    • X and z

    • Y and z

    • X and y and z

    • None of the above

    Correct Answer
    A. X and y
    Explanation
    The correct answer is "x and y". In procedural programming, the focus is on writing functions that operate on data. In object-oriented programming, the focus is on the creation of objects that contain both data and functionality together. Therefore, both statements x and y are true.

    Rate this question:

  • 44. 

    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) 

    • False False

    • True False

    • True True

    • False True

    • None of the above

    Correct Answer
    A. False True
    Explanation
    >>>
    False True
    >>>

    Rate this question:

  • 45. 

    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 

    • 20 50 0 0

    • 0 0 20 50

    • 20 50 20 50

    • 50 20 0 0

    • None of the above

    Correct Answer
    A. 20 50 20 50
    Explanation
    >>>
    20 50 20 50
    >>>

    Rate this question:

  • 46. 

    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.  

    • X

    • X and y

    • X and z

    • Y and z

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

  • 47. 

    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' 

    • X and y

    • X and z

    • Y and z

    • X, y and z

    • They all produce different Output

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

  • 48. 

    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 

    • W, x and z

    • W, y and z

    • X, y and z

    • All of w, x, y and z

    • None of w, x, y and z

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

  • 49. 

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

    • 3 5

    • 3 3

    • 5 3

    • 5 5

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

Quiz Review Timeline (Updated): Apr 7, 2024 +

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
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.