Python
If you want a function to return two values, contained in variables x and y, which of the following methods will work?
- Include the statement "return [x, y]" - Include the statement "return (x, y)" - Include the statement "return x, y"
What does the following code print? x = -10 if x < 0: print("The negative number ", x, " is not valid here.") print("This is always printed")
- The negative number -10 is not valid here - This is always printed
What is the output when the script below executes? def sub(x, y): return x - y print(sub(5, 8))
-3
What will the output be from this code? def sub(x, y): return x - y print(sub(5, 8))
-3
What will print with the statement: print(sub(5,9))?
-4 will print because 5-9 is -4
What does the following code output? def f(x = 0, y = 1): return x * y print(f())
0
What will the following statement print? print(6 % 6)
0 The % sign is division
What value is printed when the following code executes? print(13 % 4)
1
def f(x = 0, y = 1): return x * y print(f(1))
1 Since one parameter value is specified, it is bound to x; y gets the default value of 1.
What is printed by the following statement? alist = [1,3,5] print(alist * 3)
1,3,5,1,3,5,1,3,5
What is printed by the following statement? alist = [1,3,5] blist = [2,4,6] print(alist + blist)
1,3,5,2,4,6
How many times will the word HELLO be printed by the following statement? s = "python rocks" for ch in s: print("HELLO")
12
What is the value of the following expression: 16 - 2 * 5 // 3 + 1
14 Using parentheses, the expression is evaluated as (2*5) first, then (10 // 3), then (16-3), and then (13+1).
What will the following statement print? d = {1:5, 2:6, 3:3, 4:1} x = 0 for n in d: x = x + d[n] print(x)
15
What is printed by the following statements? mydict = {"cat":12, "dog":6, "elephant":23} mydict["mouse"] = mydict["cat"] + mydict["dog"] print(mydict["mouse"])
18
mydict = {"cat":12, "dog":6, "elephant":23} mydict["mouse"] = mydict["cat"] + mydict["dog"] What will display when the following is executed? print(mydict['mouse']
18
Given: s = 'get started' What is the value of the following expression? len(s.split())
2
What is printed by the following statements? mydict = {"cat":12, "dog":6, "elephant":23, "bear":20} answer = mydict.get("cat")//mydict.get("dog") print(answer)
2
What value is printed when the following code executes? print(8.4 // 4)
2.0
What value is printed when the following code executes? print(8.4 / 4)
2.1
How many times is the letter p printed by the following statement? s = "python" for idx in range(len(s)): print(s[idx % 2])
3
What value will be printed for y? initial = 7 def f(x, y = 3, z = initial): print("x, y, z are:", x, y, z) f(2, z = 10)
3
d1 = {'a':1, 'b':2, 'c':3} d2= d1 The following statement is executed: d2['a'] = 3 What is displayed when the following statement is executed? print(d1['a'])
3
What is printed when the following code is executed? s = "python rocks" print(s.count("o") + s.count("p"))
3-- There are 2 o's and one p
How many lines will print out when the following code is executed? try: for i in range(5): print(1.0 / (3-i)) except Exception, error_inst: print("Got an error", error_inst)
4
What is printed by the following statements? total = 0 mydict = {"cat":12, "dog":6, "elephant":23, "bear":20} for akey in mydict: if len(akey) > 3: total = total + mydict[akey] print(total)
43
How many times will the word HELLO be printed by the following statement? s = "python rocks" for ch in s[3:8]: print("HELLO")
5
What will print with the statement: print(sub(square(3)), square(1+1)))?
5 will print: square 3 is 9 and square (1+1) is 4, 9-4 is 5
What value is printed when the following statement executes? print(int(53.785))
53 The int function truncates all values after the decimal and prints the integer value.
What will be printed by the following statement? nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] accum = 0 for w in nums: accum = accum + w print(accum)
55, the accumulation pattern will add every number in nums together.
What is printed by the following statement? mydict = {"cat":12, "dog":6, "elephant":23} print(mydict["dog"])
6
What value will be printed for z? initial = 7 def f(x, y = 3, z = initial): print "x, y, z are:", x, y, z initial = 0 f(2)
7
What value will be printed for z? initial = 7 def f(x, y = 3, z = initial): print("x, y, z are:", x, y, z) f(2, 5)
7
What will the following code output? def square(x): return x*x def g(y): return y + 3 def h(y): return square(y) + 3 print(h(2))
7..default for x squared is 2
In Python a module is:
A file containing Python definitions and statements intended for use in other Python programs.
Why would you define a function in order to make a request to a REST API for data?
A. Because that means you have to write less repeated code if you want to make a request to the same API more than once in the same program. B. Because writing functions to complete a complex process in your code makes it easier to read and easier to fix later. C. Because a lot of things stay the same among different requests to the same API.
Consider the following alternative way to swap the values of variables x and y. What's wrong with it? y = x x = y
At the end, x still has it's original value instead of y's original value.
One reason that lotteries don't use computers to generate random numbers is:
Because computers don't really generate random numbers, they generate pseudo-random numbers.
Why is it important to use a function like the params_unique_combination function in this caching pattern?
Because when requests.get encodes URL parameters, the params might be in any order, which would make it hard to compare one URL to another later on, and you could cache the same data multiple times.
What is printed when the following code is executed? s = "Ball" s[0] = "C" print(s)
Error -- strings are immutabe
What is printed by the following statement? alist = [4,2,8,6,5] alist = alist + 999 print(alist)
Error, you cannot concatenate a list with an integer
What will be printed by the following statement? nums = [3, 5, 8] accum = [] for w in nums: x = w**2 accum.append(x) print(accum)
Every item in the list will be multiplied by itself [ 9, 25, 64 ]
A list can only contain integer items
False
After a run-time exception is handled by an except clause, the rest of the code in the try clause will be executed.
False
What does the following code print? if (4 + 5 == 10): print("TRUE") else: print("FALSE")
False
What is printed by the following statements? mydict = {"cat":12, "dog":6, "elephant":23, "bear":20} print(23 in mydict)
False
What will the following statement print? mydict = {"cat":12, "dog":6, "elephant":23} print(12 in mydict)
False
True / False: All standard Python modules will work in activecode.
False Only turtle, math, and random have been ported to work in activecode at this time.
What is printed by the following statement? alist = [3, 67, "cat", 3.14, False] print("at" in alist)
False, at is not in the list alist
To find out information on the standard modules available with Python you should:
Go to the Python Documentation site.
What does the in operator test?
If one string is a substring of another
Given: lst = ['5', '6', True] str='234' What is the type of the following expression? str[:2] + lst[1]
Integer
What would happen if we indented the print statement? nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] for w in nums: accum = 0 accum = accum + w print(accum)
It would print out 10 instead of 55, as accum would be set to 0 each time through the loop.
Given the following script: s = "ball" r = "" for item in s: r = item.upper() + r print(r) What is displayed on the when the script executes?
LLAB
What will the following statement print: '3' + "1"
Nothing, there is no print statement
What will the output be from this code? def sub(x, y): return x - y sub(5, 8)
Nothing, there is no print statement
How many lines of code can appear in the indented code block below the if and else lines in a conditional?
One or more
What is the data type of 'this is what kind of data'?
String
What is printed when the following code is executed? s = "python rocks" print(len(s))
There are 12 characters in the string
What is printed by the following statement? L = [0.34, '6', 'SI106', 'Python', -2] print(len(L[1:-1]))
There are 3 items in this list
What is printed by the following statement? alist = [3, 67, "cat", 3.14, False] print(len(alist))
There are 5 items in the list alist
What value will be printed for x? initial = 7 def f(x, y = 3, z = initial): print("x, y, z are:", x, y, z) f(2, x=5)
There will be a runtime error, as two different values are provided for x
What is displayed when the following code executes? x =10 if x < 10: print('one') elif x < 20: if x > 15: print('two') else: print('three') elif x < 30: print('four') else: print("five")
Three
A dictionary is an unordered collection of key-value pairs.
True
True or False: You can rewrite any for-loop as a while-loop.
True
What is printed by the following statements? mydict = {"cat":12, "dog":6, "elephant":23, "bear":20} print("dog" in mydict)
True
When a run-time exception of type ZeroDivisionError occurs, and you have a statement except IndexError, the program will stop executing completely.
True
idx % 2 is 0 whenever idx is even
True
What is printed by the following statement? alist = [3, 67, "cat", 3.14, False] print("cat" in alist)
True, cat is in the list alist
What will the following code print if x = 3, y = 5, and z = 2? if x < y and x < z: print("a") elif y < x and y < z: print("b") else: print("c")
Will print c. The first two boolean statements are false, then the else will be executed.
Will the following code cause an error? x = -10 if x < 0: print("The negative number ", x, " is not valid here.") else: print(x, " is a positive number") else: print("This is always printed")
Yes, because the second else is not connected to an if statement
What is printed by the following statement? alist = [4,2,8,6,5] alist.append(True) alist.append(False) print(alist)
[ 4, 2, 8, 6, 5, True, False ]
What is printed by the following statement? alist = [4,2,8,6,5] blist = alist * 2 blist[3] = 999 print(alist)
[ 4, 2, 8, 6, 5] alist was unchanged in the statement blist was a copy of the references in alist
What is printed by the following statement? alist = [4,2,8,6,5] blist = alist blist[3] = 999 print(alist)
[ 4, 2, 8, 999, 5 ]
What is printed by the following statement? alist = [4,2,8,6,5] alist[2] = True print(alist)
[ 4, 2, True, 6, 5 ]
What is printed by the following statement? alist = [4,2,8,6,5] blist = [ ] for item in alist: blist.append(item+5) print(blist)
[ 9, 7, 13, 11, 10 ] every item adds 5
What is printed by the following statement? alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False] print(alist[4:])
[ [ ], 3.14, False ]
Given the function below: def f(x, y=2, z=['hi']): y += x z.append(y) return z What is displayed the the code below is executed: print(f(2,z=['bye']))
['bye', 4]
Given the function below: def f(x, y=2, z=['hi']): y += x z.append(y) return z What is displayed the the code below is executed: print(f(1,2))
['hi', 3]
What will be printed by the following statement? numbers = [10, 20, 30, 40, 50] for i in range(len(numbers)): numbers[i] = numbers[i]**2 print(numbers)
[100, 400, 900, 1600, 2500 ]
Given the function below: def f(x, y=2, z=['hi']): y += x z.append(y) return z What is displayed the the code below is executed: print(f(1,z=[ ]))
[3]
What is printed by the following statement? list= [3,0,9,4,1,7] new_list=[] for i in range(len(list)): new_list.append(list[i]+5) print(new_list)
[8,5,14,9,6,12] Yes, the for loop processes each list[i] of the list. 5 is added before list[i] is appended to blist.
Which type of loop can be used to perform the following iteration: You choose a positive integer at random and then print the numbers from 1 up to and including the selected integer
a for-loop or a while-loop
Describe the type of order this will be. L1 = [1, 7, 4, -2, 3] print(sorted(L1, key = lambda x: -x, reverse = True))
ascending order, from -2 up to 7
Which of the following is a legal assignment statement, after the following code executes? d = {'key1': {'a': 5, 'c': 90, 5: 50}, 'key2':{'b': 3, 'c': "yes"}}
d[5] = {1: 2, 3: 4} d['key1']['d'] = d['key2']
Describe the type of order this will be. L1 = [1, 7, 4, -2, 3] print(sorted(L1, key = lambda x: -x))
descending order, from 7 down to -2
What is printed when the following code is executed? s = "python rocks" print(s[3])
h
What is printed when the following code is executed? s = "python rocks" print(s[3:8])
hon r
The following code contains an infinite loop. Which is the best explanation for why the loop does not terminate? n = 10 answer = 1 while ( n > 0 ): answer = answer + n n = n + 1 print(answer)
n starts at 10 and is incremented by 1 each time through the loop, so it will always be positive
The correct code to generate a random number between 1 and 100 (inclusive) is:
prob = random.randrange(1, 101)
What is printed when the following code executes? s = "python" t = "rocks" print(s+t)
pythonrocks
How would you request the URL http://bar.com/goodstuff?greet=hi+there&frosted=no using the requests module?
requests.get("http://bar.com/goodstuff", params = {'greet': 'hi there', 'frosted':'no'})
What is printed when the following code is executed? s = "python rocks" print(s[7:11]*3)
rockrockrock
Which type of error can be noticed and handled using try/except?
run-time
Which of the following will sort the keys of d in ascending order of their values (i.e., from lowest to highest)? L = [4, 5, 1, 0, 3, 8, 8, 2, 1, 0, 3, 3, 4, 3] d = {} for x in L: if x in d: d[x] = d[x] + 1 else: d[x] = 1 def g(k, d): return d[k] ks = d.keys()
sorted(ks, key=lambda x: g(x, d)) C. sorted(ks, key=lambda x: d[x])
What will the output be from this code? def sub(x, y): return x - y print("sub(5, 8)")
sub(5,8)
What is printed when the following code executes? s = "python rocks" print(s[2] + s[-4])
to
x != y
x is not equal to y
What is printed when the following code is executed? s = "python rocks" print(s[1]*s.index("n"))
yyyyy s[1] is y and the index of n is 5, so 5 y characters. It is important to realize that the index method has precedence over the repetition operator. Repetition is done last.