Quiz and Solution
Quiz and Solution
Quiz and Solution
)
1. What is printed by the Python code?
x = 5
y = x + 3
x = x - 1
z = 10
x = x + z
print('x: {}, y: {}, z: {}'.format(x, y, z))
2. What is printed by the Python code? 10. What is printed by the Python code?
print(14//4, 14%4, 14.0/4) def func(x):
return x - 1
3. What is printed by the Python code?
print(2*'No' + 3*'!') print(func(3) * func(5))
print(2 * ('No' + 3*'!'))
11. What is printed by the Python code?
4. What is printed by the Python code? n = 3 #1
Be careful: Note the backslashes: for x in [2, 5, 8]: #2
print('how\nis it\nnow') n = n + x #3
print(n) #4
5. What is printed by the Python code?
for z in [2, 4, 7, 9]: 12. What is printed by the Python code?
print(z - 1) print(list(range(3)))
6. What is printed by the Python code? 13. What is printed by the Python code?
print('2' + '3') for i in range(3):
print('Hello again!')
7. What is printed by the Python code?
def f1(): 14. What is printed by the Python code?
print('Hi') for i in range(4):
def f2(): print(i)
print('Lo')
15. What is printed by the Python code?
f2() def s(x): #1
f1() return x*x #2
f1()
for n in [1, 2, 10]: #3
print(s(n)) #4
8. What is printed by the Python code?
def func():
print('Yes') 16. What is printed by the Python code?
def s(x): #1
print('No') return x*x #2
func()
tot = 0 #3
for n in [1, 3, 5]: #4
9. What is printed by the Python code?
def func(x): tot = tot + s(n) #5
print(2*x) print(tot) #6
func(5)
func(4)
17. What is printed by the Python code?
x = 2.5679
y = 9.0
print('Answers {:.3f} and {:.3f}'.format(x, y))
19. Write a Python program that prompts the user for two numbers, reads them in, and prints out the
product, labeled.
20. Given a string s, write a short expression for a string that includes s repeated five times.
21. Suppose you know x is an integer and ys is a string representing an integer. For instance, x is 3 and
ys is '24'. Write code to print out the arithmetic sum of the two. In the example case, 27 would be
printed.
22. Suppose you are given a list of words, wordList. Write Python code that will write one line for each
word, repeating that word twice. For example if wordList is ['Jose', 'Sue', 'Ivan'], then
your code would print
Jose Jose
Sue Sue
Ivan Ivan
23. Write code to create a Python dictionary (the dict type). Add two entries to the dictionary: Associate
the key ‘name’ with the value ‘Juan’, and associate the key ‘phone’ with ‘508-1234’
24. Complete the code for the following function so it matches its documentation:
def doubleList(numberList):
''' For each of the numbers in the list numberList, print a line
containing twice the original number. For example,
doubleList([3, 1, 5]) would print
6
2
10
'''
25. Assuming a function process is already 26. Complete the function definition so it returns the
defined, write two lines of code, using a for- square of the product of the parameters, so
loop, that is equivalent to the following: sqrProd(2, 5) returns (2*5)*(2*5) = 100.
6. 23 14. 0
1
7. Lo 2
Hi 3
Hi
range(4) contains 0, 1, 2, 3
First the functions are remembered. Afterward
they are called in the order given. 15. 1
4
8. No 100
Yes
First the function is remembered. It is only Evaluates s, the squaring function, for each element in the
called after 'No' is printed. list, and prints the results.
16. 35 # 0 + 1*1 + 3*3 + 5*5
details:
line tot n x comment
1-2 definition
3 0
4 1 first value in list
5 evaluate s(1), returns 1 = 1*1
1 so tot = tot+1 = 0+1 = 1
4 3 next value in list
5 evaluate s(3), returns 9 = 3*3
10 so tot = tot+9 = 1+9 = 10
4 5 last value in list
5 evaluate s(5), returns 25 = 5*5
35 so tot = tot+25 = 10+25 = 35
4 done with list and loop
6 prints 35
19.
x = int(input('Enter a number: ')) # or some such prompt
y = int(input(”Enter another number: ”)) # or some such prompt
print('The product is ', x*y) # or some such label
20. s*5 # or: s+s+s+s+s
21. print(x + int(ys))
22. for word in wordlist: # variable word is arbitrary
print(word, word) # but must match here!
23. d = dict() # name d is arbitrary, but match it in the next lines
d['name'] = 'Juan'
d['phone'] = '508-1234'
24. def doubleList(numberList):
''' skip repeating docs... '''
for n in numberList:
print(2*n)
25. for name in ['Joe', 'Sue', 'Ann', 'Yan']:
process(name)
26. return x*x*y*y # or: return (x*y)**2