1.
What is the output of the following code? x = 'Hello'y = 'there'print x+y
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".
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
Correct Answer
B. W and z
3.
Which of the following is not a Python basic type?
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.
4.
What is the output of the following code?max(3, 1, abs(-11), 7, 16-10)
Correct Answer
E. None of the above
Explanation
>>> print max(3, 1, abs(-11), 7, 16-10)
11
>>>
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')
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
>>>
6.
Correct Answer
B. Hello Goodbye
Explanation
>>> Hello Goodbye >>>
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'
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
>>>
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)
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
>>>
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))
Correct Answer
E. None of the above
Explanation
>>>
>>> var1 = 10
>>> var2 = 5
>>> var3 = 7
>>> (var1> (var2> (var2> ((var1>
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'
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>
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
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
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
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).
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
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.
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
Correct Answer
A. W and x
15.
A namespace is:
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.
16.
What is the output of the following program?a = 5b = aa = 3print a, b
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".
17.
What is the best explanation for the following code (assume x is an int)?x = x + 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.
18.
What is the output of the following code? def countdown(n):while n>=0:print n,n = n-1print ncountdown(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".
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)
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.
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)
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.
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.
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.
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> ')
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.
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.
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").
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
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.
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 ()
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).
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.
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.
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.
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.
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.
29.
In the context of programming, refactoring is:
Correct Answer
A. Converting a number into a product of prime factors.
30.
In the context of programming, abstraction is:
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.
31.
What is the output of the following code? fruit = 'banana'print fruit [2]
Correct Answer
C. N
Explanation
>>> fruit = 'banana'
>>> print fruit [2]
n
>>>
32.
What is the output of the following code?fruit = 'banana'print fruit[-2]
Correct Answer
E. None of the above
Explanation
>>> fruit = 'banana'
>>> print fruit[-2]
n
>>>
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
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
>>>
34.
What is the output of the following code?fruit = 'banana'print fruit[1:5]
Correct Answer
E. None of the above
Explanation
fruit = 'banana'
print fruit[1:5]
>>>
anan
>>>
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
Correct Answer
C. Zebra comes before aardvark
Explanation
>>>
Zebra comes before aardvark
>>>
36.
What is the output of the following code?fruit = 'banana'fruit[0] = 'B'print fruit
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
>>>
37.
What is the output of the following code?fruit = 'banana'fruit = 'orange'print fruit
Correct Answer
B. Orange
Explanation
>>>
orange
>>>
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)
Correct Answer
A. I am Alice and I am 10 years old.
Explanation
>>>
I am Alice and I am 10 years old.
>>>
39.
What is the meaning of the string format conversion specification' %-5d' % n?
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.
40.
What is the output of the following code?alist = [1,2,3,4]blist = alistalist[0] = 5print blist [0]
Correct Answer
E. None of the above
Explanation
>>>
5
>>>
41.
What is the output of the following code?fruit = [ 'b', 'a', 'n' , 'a', 'n' , 'a' ]fruit[0] = 'B'print "_".join(fruit)
Correct Answer
A. ['B', 'a', 'n', 'a', 'n', 'a']
Explanation
>>>
B_a_n_a_n_a
>>>
42.
What is the output of the following code?for number in range(20):if number %2 == 0:print number,print
Correct Answer(s)
E. None of the above
F. None of the above
Explanation
>>>
0 2 4 6 8 10 12 14 16 18
>>>
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
Correct Answer
D. [1, 4, 9, 16, 25]
Explanation
>>>
[1, 4, 9, 16, 25]
>>>
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
Correct Answer
C. ['Hello', 2, 3, 4, 5]
Explanation
>>>
['Hello', 2, 3, 4, 5]
>>>
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.
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.
46.
What is the output of the following code?matrix= [[1,2,3],[4,5,6],[7,8,9]]print matrix[1]
Correct Answer
E. None of the above
Explanation
>>>
[4, 5, 6]
>>>
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]])
Correct Answer
B. 12
Explanation
>>>
12
>>>
48.
What is the output of the following code?items = (1, 3, 5, 7, 9)items[0] = -1print items[0]
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
>>>
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
Correct Answer
C. 3 4 5
Explanation
>>>
3 4 5
>>>
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.
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.