D. Watch the video: Thinking Like a Computer -- Control Flow, doing the next set of questions while you do so.
15. What gets printed when main
is called in the program shown below? (Pay close attention to the order in which the statements are executed.)
def main():M. Line 1 ->
hello()
goodbye()
hello()
def hello():
print('Hello!')
return 'Hello!'
def goodbye():
print('Ciao!')
Hello!M. Line 2 ->
Ciao!M. Line 3 ->
Hello!15. What gets printed when
main
is called in the program shown below? (Hint: this code is very similar to the code in the previous problem, and this problem is a bit of a "trick" question.) def main():M. Line 1 ->
hello()
goodbye()
hello()
def hello():
print('Hello!')
return 'Hello!'
print('Hello Again!')
def goodbye():
print('Ciao!')
Hello!M. Line 2 ->
Ciao!M. Line 3 ->
Hello!M. ->
Hello Again!17. What gets printed when
main
is called in the program shown below? (You will probably want to use post-it cards (as in the video) or some written notes to help you solve this problem.) M. Line 1 -> basketball M. Line 2 -> truck M. Line 3 -> basketball M. Line 4 -> basketball 17. What gets printed when
def main():
big()
bigger()
big()
def big():
print('basketball')
def bigger():
print('truck')
big()
main
is called in the program shown below? (You will probably want to use post-it cards (as in the video) or some written notes to help you solve this problem.) M. Line 1 -> basketball M. Line 2 -> truck M. Line 3 -> basketball M. Line 4 -> house M. Line 5 -> truck M. Line 6 -> basketball M. Line 7 -> basketball M. Line 8 -> basketball D. Do this online reading: Functions with Parameters and Returned Values, doing the next set of questions while you do so. 5. Consider the function call
def main():
big()
bigger()
biggest()
big()
def big():
print('basketball')
def bigger():
print('truck')
big()
def biggest():
print('house')
bigger()
big()
round(3.14159, 2)which rounds 3.14159 to 2 decimal places. What are the arguments in that function call? a.
3.14159
b. 2
c. 3.14
d. 3.14159
and 2
~
e. round
6. Consider the function call round(3.14159, 2)which rounds 3.14159 to 2 decimal places. What is the returned value from that function call? a.
3.14159
b. 2
c. 3.14
~
d. 3.14159
and 2
e. round
6. Consider the function call round(3.14159, 2)which rounds 3.14159 to 2 decimal places. What is the name of the function being called? a.
3.14159
b. 2
c. 3.14
d. 3.14159
and 2
e. round
~
6. Does the following define or call the function blah
? blah(x, 34, foo(100))a. define b. call~ 6. Does the following define or call the function
blah
def blah(a, b, c) print(10 * a, b + 4, c)a. define~ b. call 7. As a user of a function (that is, as someone who will call the function), you don't need to know how the function is implemented; you just need to know the specification of the function. a. True~ b. False D. For the next several questions, consider the
cube_volume
function defined as follows: def cube_volume(side_length):8. What is the value of
volume = side_length ** 3
return volume
cube_volume(3)
?
a. 9
b. 27~
c. 81
d. 256
e. 512
9. What is the value of cube_volume(2)
?
a. 4
b. 8~
c. 9
d. 16
e. 27
10. What is the value of cube_volume(cube_volume(2))
?
a. 4
b. 27
c. 81
d. 256
e. 512~
f. 729
11. Provide an alternate implementation of the body of the cube_volume
that does not use the exponent operator.
M. Line 1 -> volume = side_length * side_length * side_length
M. Line 2 -> return volume
M. -> volume = side_length * 3
M. -> volume = side_length ^ 3
D. For the next several questions, consider the mystery
function defined as follows: def mystery(x, y):12. What is the value of
result = (x + y) / (y - x)
return result
mystery(2, 3)
?
ANS. 5
13. What is the value of mystery(3, 2)
?
ANS. -5
14. What is the value of mystery(-1, 3)
?
ANS. 0.5
ANS. .5
D. For the next several questions, consider the box_string
function shown below. This function takes a string as its argument and correctly displays that string “in a box”: For example, calling
def box_string(contents):
n = len(contents)
print('-' * (n + 2))
print('!' + contents + '!')
print('-' * (n + 2))
box_string
with 'Hello Moon'
as its argument yields the following: ------------27. Consider the following (silly!) statement:
!Hello Moon!
------------
print(box_string('Hello'))What, exactly, does the above statement cause to appear on the Console? M. Line 1 -> ------- M. Line 2 -> !Hello! M. Line 3 -> ------- M. Line 4 -> None M. -> !Hello Moon! M. -> ------------ M. -> ------------ M. -> (nothing appears on this line) 28. How should the above statement been written, to be sensible? a. box_string('Hello')~ b. print(box_string('Hello')) c. print(box_string('Hello')) - None d. print(box_string('Hello') - None) e. box_string('!Hello!') 29. Write statements that would use
box_string
to produce on the Console the output shown below.M. Line 1 -> box_string('Hello') M. Line 2 -> box_string('Moon') M. -> print(box_string('Hello')) M. -> print(box_string('Moon')) D. First, do this online reading: Namespaces and Variables' Scope. Then, watch the video: Thinking Like a Computer -- Namespaces, Parameters, and Returns, doing the next set of questions while you do so. (So don't start answering the questions until you are watching the video, since it clarifies ideas in the reading.) D. For the next several questions, you will probably want to use post-it cards (as in the video) and/or some written notes to help you solve them, and perhaps a calculator. 16. What gets printed when
-------
!Hello!
-------
------
!Moon!
------
main
is called in the program shown below?ANS. 25 18. What gets printed when
def main():
a = 4
answer = mystery(a + 1)
print(answer)
def mystery(x):
y = x * x
return y
main
is called in the program shown below?def main():ANS. 25 19. What gets printed when
a = 4
print(mystery(a + 1))
def mystery(x):
return x * x
main
is called in the program shown below?def main():ANS. 25 20. What gets printed when
a = 4
print(mystery(a + 1))
def mystery(x):
a = 9
return x * x
main
is called in the program shown below?def main():ANS. 4 21. What gets printed when
a = 4
mystery(a + 1)
print(a)
def mystery(x):
a = 9
return x * x
main
is called in the program shown below?def main():ANS. 29 22. What gets printed when
a = 4
b = mystery(a + 1)
print(a + b)
def mystery(x):
a = 9
return x * x
main
is called in the program shown below?def main():ANS. 49 23. What gets printed when
a = 4
b = mystery(a + 1)
print(a + b)
def mystery(x):
a = 9
return a * x
main
is called in the program shown below?def main():ANS. 50 24. What gets printed when
a = 4
print(mystery(a + 1) + mystery(a + 1))
def mystery(x):
a = 9
return x * x
main
is called in the program shown below?def main():ANS. 81 D. For each of the following code snippets:
a = 2
print(mystery(mystery(a + 1)))
def mystery(x):
return x * x
main
runs.def main():a. Correct, prints the value of
x = foo()
print(x)
def foo(m):
return m ** 3
x**3
b. Correct, prints the value of m**3
c. Incorrect because the call to foo
is missing a parameter~
d. Incorrect because m
in foo
is undefined
e. Incorrect because x
is undefined
f. Incorrect because the value of m
can't be cubed
31. def main():a. Correct, prints the value of
x = foo(m)
print(x)
def foo(m):
return m ** 3
x**3
b. Correct, prints the value of m**3
c. Incorrect because the call to foo
is missing an argument
d. Incorrect because m
in foo
is undefined
e. Incorrect because m
in main
is undefined~
f. Incorrect because x
is undefined
g. Incorrect because the value of m
can't be cubed
32. def main():a. Correct, prints
x = foo('help')
print(x)
def foo(m):
return m ** 3
helphelphelp
a. Correct, prints m**3
c. Incorrect because the call to foo
is missing an argument
d. Incorrect because m
in foo
is undefined
e. Incorrect because x
is undefined
f. Incorrect because the value of m
can't be cubed~
33. The code in the box below has syntax errors: it causes big red X error message(s). Check all lines that will have red X error message(s) beside them. def main():a.
foo()
print(n)
print(m)
def foo():
n = 3
m = 1
return m
def main():
b. foo()
c. print(n)
~
d. print(m)
~
e. def foo():
f. n = 3
g. m = 1
h. return m
37. What gets printed when main
is called in the program shown below? def main():M. Line 1 -> 2 3 M. Line 2 -> 2 3 M. Line 3 -> 2 3 M. -> 88 99 M. -> 400 500 M. -> 44 55 36. What gets printed when
a = 2
b = 3
foo1()
print(a, b)
foo2(a, b)
print(a, b)
foo3(a, b)
print(a, b)
def foo1():
a = 88
b = 99
def foo2(a, b):
a = 400
b = 500
def foo3(x, y):
x = 44
y = 55
main
is called in the program shown below?def main():M. Line 1 -> 8 M. Line 2 -> 9 M. Line 3 -> 4 M. Line 4 -> 27 M. Line 5 -> 81 M. Line 6 -> 81 M. ->
a = 2
b = 3
m = do_it(a, b)
print(m)
m = do_it(b, a)
print(m)
m = do_it(a, a)
print(m)
m = do_it(b, b)
print(m)
c = do_it(b, a)
m = do_it(c, a)
print(m)
b = do_it(b, a)
m = do_it(b, a)
print(m)
def do_it(x, y):
return x ** y
6561
D. Do this online reading: Counted Loops, doing the next set of questions while you do so.
3. Trace the snippet of code shown below by hand (no fair typing it into a program, but you may want to use paper and pencil to help you keep track of things), and show what gets printed: for k in range(4):
print(k)
a. 1 2
(all on separate lines)
b. 1 2 3
(all on separate lines)
c. 1 2 3 4
(all on separate lines)
d. 0 1 2
(all on separate lines)
e. 0 1 2 3
(all on separate lines)~
f. 0 1 2 3 4
(all on separate lines)
3. Trace the snippet of code shown below by hand (no fair typing it into a program, but you may want to use paper and pencil to help you keep track of things), and show what gets printed: for k in range(3):
print(k + 10)
a. 1 2
(all on separate lines)
b. 1 2 3
(all on separate lines)
c. 1 2 3 4
(all on separate lines)
d. 0 1
(all on separate lines)
e. 0 1 2
(all on separate lines)
f. 0 1 2 3
(all on separate lines)
g. 10 11
(all on separate lines)
h. 10 11 12
(all on separate lines)~
i. 10 11 12 13
(all on separate lines)
j. 11 12
(all on separate lines)
k. 11 12 13
(all on separate lines)
l. 11 12 13 14
(all on separate lines)
1. Choose the correct lines to make a loop that prints the string 'funny'
40,000 times.
M. Line 1-> for k in range(40000):
M. -> for k in range(40001):
M. -> for k in range(n):
M. Line 2-> print('funny')
M. -> return 'funny'
M. -> print 'funny'
M. -> print 'funny'
2. Choose the correct lines to make a loop that prints the cubes of the numbers from 35 to m
, inclusive (where m
is some integer bigger than 35). For example, if m
were 37, then this loop should print: 42875
46656
50653
(The above numbers are 35 cubed, 36 cubed, and 37 cubed, respectively.)
M. Line 1-> for k in range(m - 34):
M. -> for k in range(0):
M. -> for k in range(37):
M. -> for k in range(m):
M. -> for k in range(m - 33):
M. -> for k in range(m - 35):
M. -> for k in range(m - 36):
M. Line 2-> print((k + 35) ** 3)
M. -> print(34 ** 3)
M. -> print(35 ** 3)
M. -> print((k + 34) ** 3)
M. -> print(m ** 3)
M. -> print(k ** 3)
M. -> print((m + 34) ** 3)
M. -> print((m + 35) ** 3)
D. Watch Video: The Accumulator Pattern - Summing, doing the next set of questions while you do so.
3. Trace the snippet of code shown below by hand (no fair typing it into a program, but you will probably want to use paper and pencil to help you keep track of things), and show what gets printed: total = 0
for k in range(4):
total = total + k
print(k, total)
print('The sum is:')
print(total)
M. Line 1 ->0 0
M. Line 2 ->1 1
M. Line 3 ->2 3
M. Line 4 ->3 6
M. Line 5 ->The sum is:
M. Line 6 ->6
M. ->1 0
M. ->0 1
M. ->1 2
M. ->2 2
M. ->3 3
M. ->4 4
M. ->4 6
M. ->0
M. ->1
M. ->2
M. ->3
M. ->>4
M. ->5
M. ->7
M. ->8
M. ->9
M. ->10
M. ->11
M. ->12
3. Trace the snippet of code shown below by hand (no fair typing it into a program, but you will definitely want to use paper and pencil to help you keep track of things), and show what gets printed: total = 0
for k in range(5):
total = total + (k + 10)
print(k, total)
print('The sum is:')
print(total)
M. Line 1 ->0 10
M. Line 2 ->1 21
M. Line 3 ->2 33
M. Line 4 ->3 46
M. Line 5 ->4 60
M. Line 6 ->The sum is:
M. Line 7 ->60
M. ->1 20
M. ->1 11
M. ->2 12
M. ->3 13
M. ->4 14
M. ->5 15
M. ->5 75
M. ->10
M. ->46
M. ->75
3. Trace the snippet of code shown below by hand (no fair typing it into a program, but you will probably want to use paper and pencil to help you keep track of things), and show what gets printed: total = 5
for k in range(3):
total = total * k
print(total)
a. 0~
b. 1
c. 3
d. 6
e. 24
4. Write a snippet of code that calculates: math.sin(3) + math.sin(4) + math.sin(5) + ... + math.sin(500)
Assume that there is already an import math
that executed previously in the code.
M. Line 1 -> total = 0
M. -> total = 500
M. -> k = 0
M. -> k = 500
M. Line 2 -> for k in range(498):
M. -> for k in range(497):
M. -> for k in range(499):
M. -> for k in range(500):
M. -> for k in range(3):
M. Line 3 -> total = total + math.sin(k + 3)
M. -> total = total + math.sin(k + 3)
M. -> total = math.sin(k + 3)
M. -> total = math.sin(k + 3)
M. -> total = total + math.sin(k + 2)
M. -> total = total + math.sin(k + 2)