Miscellaneous
Expressions
Functions
Strings
Lists
100
A sequence of commands to change to the child directory named labfiles and show the names of the files in labfiles.
What is cd labfiles ls
100
Output of the code: a = 1.5 print type(a) a = 2 print type(a)
What is
100
3 good tests for a function Capitalize that takes a string argument and returns the string in capitalized form.
What is assertEqual(Capitalize('abc'),'Abc'), assertEqual(Capitalize(''),''), assertEqual(Capitalize('ABC'),'ABC')
100
A string expression to extract the last four digits of someone's social security number from the variable soc
What is soc[-4:] or soc[6:]
100
The output of the code segment: nums = [11,3,2,4,5] nums[2]= 9 print nums[:3]
What is [11,3,9]
200
Order the different storage devices by fastest to slowest: flash drive, main memory, cache, hard drive
What is cache, main memory, hard drive, flash drive
200
The result of calling a recursive function that has no IF statements with a base case branch.
What is infinite execution until the stack grows as big as memory allows.
200
Output from: def hmm(x): return x + 1 print hmm(2) y = 0 print y print hmm(y) print y
What is 3 0 1 0
200
The result of the code: phrase = 'hey' phrase[1] = 'a' print phrase
What is phrase[1] = 'a' TypeError: 'str' object does not support item assignment
200
The output of the following code: def doit(alist): alist[2]=999 return alist mylist = [1,2,3,4,5] print doit(mylist)
What is [1,2,999,4,5]
300
Explain the difference between an interpreter and compiler.
What is a compiler translates to machine code which is then executed, while an interpreter translates and executes one instruction at a time
300
The result of executing print 10 / 3 print 10.0 / 3 print 10 % 3
What is 3 3.33333333333 1
300
At the end, what is the value of w? def f(x): x = x + 1 return x >>> w = 7 >>> z=f(w)
What is 7
300
Code to check if the string name contains the substring 'hey' and print YES otherwise print NO
What is if 'hey' in name: print 'YES' else: print 'NO'
300
Code that makes and prints a list that contains 3 pairs of zeros.
What is print[[0,0]] * 3
400
A code segment that demonstrates the difference between a local and a global variable.
What is def do(x): print 'local is', x x = 0 print 'global is', x do(3) print 'global is', x
400
The results of executing: a = 9 b = a + a / 3 - 2 print a,b
What is 9 10
400
At the end, what is the value of z? def f(x): x = x + 1 return x >>> w = 7 >>> z=f(w)
What is 8
400
A test of whether the function capitalize(name) works correctly in capitalizing its arguments.
What is assertEqual(capitalize('me'),'Me') assertEqual(capitalize(''),'')
400
The value extracted by the expression print list[1][1][1] on the list list = [1,[2,[3,4],5]]
What is 4
500
Good Tests for a function absolute value abs(num) that returns the absolute value of num
What is assertEqual(abs(-1),1) assertEqual(abs(4),4) assertEqual(abs(0),0)
500
A recursive function to sum the numbers from 1 to x and a test for x as 6
What is www.google.com def sum(x):\n if x == 0: return x else: return x + sum(x-1) print sum(6)
500
def f(x): return x + g(x) def g(y): return y * 2 print f(1) What does this program print?
What is 3
500
The result of calling the function below as magic(name) where name is 'Barney' def magic(name): if name=='': return else: magic(name[1:]) print name[0],
What is y e n r a B
500
A function to print the elements of a list alist.
What is def printlist(alist): if alist ==[]: return else: print alist[0] printlist(alist[1:])
M
e
n
u