Quiz Concepts
Quiz 01 will cover both the concepts that would’ve been on Quiz 00 and Quiz 01. (However, it won’t be any longer than a typical quiz.)
You can find a full list of concepts here.
Questions
The quiz itself will be similar in difficulty to these practice questions. In addition to these questions, you should review all of your lesson responses on Gradescope. The kinds of questions you responded to on Gradescope will also be on the quiz.
Solutions for each problem can be found at the bottom of this page. (But if you’re unsure, before looking up the solution, try figuring it out on your own or testing it out in Visual Studio!)
Data Types
str
literals in Python can be surrounded in either single-quote characters ('like this'
) or double-quote characters ("like this"
), though in COMP110 we prefer the double-quotes. (T/F)TRUE
andFALSE
are valid bool values in Python. (T/F)- An
int
literal can begin with a zero, but cannot end with a zero. (T/F) - What function can we use to identify the type classification of any object in Python?
Expressions
- What is the resulting value and data type of the following expression?
int("20")
- What is the resulting value and data type of the following expression?
str(2023) + str(2023)
- What is the resulting value of the following expression?
type(9 / len( str(110))
- What is the resulting value of the following expression?
"440" + "20"
- What value of x would cause the following expression to evaluate to
True
?(3 + x) == (55 % 2 ** 4)
- What value does the following expression result in, and what is its type?
2 + 2 / 2 ** (2 * 0)
- Using subscription syntax and concatenation, write an expression that evaluates to
"tar"
using the following string:“Saturday"
. - What data type do expressions with relational operators typically evaluate to?
- What does the following expression evaluate to?
int("10" + "40") > 100 * 2
Conditionals
Every
if
statement must be followed by a pairedelse
branch. (T/F)Lines contained in an
else
branch in Python do not have to be indented. (T/F)You can name a variable
else
in your program without Python confusing your variable’s name and theelse
keyword. (If you are unsure, this is a good one to try yourself!) (T/F)All subquestions of this problem will refer to this code snippet:
x: str = int(input("Pick a number: ")) y: int = 10 z: int = 2 x = x - 1 if x < 10: print("A") elif (x % z) == 0: print ("B") if x == (y + z): print("C") else: print("D")
For the following subquestions, answer with a valid input value for x
that would cause the given letter to print. It is OK for your input to cause other letters to print as well. If there is no such value, write “Unreachable”.
4.1 A
4.2 B
4.3 C
4.4 D
Now, answer with a valid input value for x
that would cause the exact given letter(s) to print (no other letters). If there is no such value, write “Unreachable”.
4.5
A
C
4.6
B
C
4.7
C
4.8
D
While Loops Basic
Given the following code snippet, what is the printed output once it completes?
"""Loops Practice!""" x: int = 0 y: int = 3 z: str = "1" while x < y: z = z + str(y) + str(x) x = x + 1 print(x) print(y) print(z)
Given the following code snippet, what is the printed output once it completes?
"""Loops Practice!""" x: int = 10 result: str = "" while x >= 0: if x % 3 > 0: result = result + str(x) else: result = str(x) + result x = x - 1 print(result)
While Loops + Functions
Produce a memory diagram for the following code snippet, being sure to include its stack and output.
WORD: str = "happy" l1_idx: int = 0 l2_idx: int = 0 t1: str = "" t2: str = "" n_appearances: int = 0 while l1_idx < len(WORD): t1 = WORD[l1_idx] n_appearances = 1 l2_idx = 0 while l2_idx < len(WORD): t2 = WORD[l2_idx] if (t1 == t2) and (l1_idx != l2_idx): n_appearances = n_appearances + 1 l2_idx = l2_idx + 1 print(f"{WORD[l1_idx]} appears {n_appearances} times.") l1_idx = l1_idx + 1
1.1 What would happen if while l1_idx < len(WORD)
in line 8 was replaced with while l1_idx < len(WORD) - 1
? Why?
1.2 What would happen if l2_idx=0
was moved to inside the second while loop? In other words, what if lines 11-13 were changed to:
while l2_idx < len(WORD):
l2_idx = 0
1.3 What would happen if, in line 16, if (t1 == t2) and (l1_idx != l2_idx):
was replaced with if (t1 == t2):
? Why?
1.4 What would happen if line 18 (l2_idx = l2_idx + 1
) was removed? Why?
1.5. What would happen if the <
symbol in line 8 was replaced with <=
? (In other words, what if it was changed to while l1_idx <= len(WORD):
)? Why?
Produce a memory diagram for the following code snippet, being sure to include its stack and output.
def main() -> None: """Main Function""" y: int = g(1) f(y) print(g(f(3))) def f(x: int) -> int: """Function 0""" if x % 2 == 0: print(f"{x} is even") else: x += 1 return x def g(x: int) -> int: """Function 1""" while x % 2 == 1: x += 1 return x main()
2.1 Why is it that main()
is defined above f()
and g()
, but we are able to call f()
and g()
inside main()
without errors?
2.2 On line 5, when print(g(f(3)))
is called, is the code block inside of the while
loop ever entered? Why or why not?
2.3 What would happen if a line was added to the end of the snipped that said print(x)
. Why?
Solutions
Data Types Solutions
- T
- F
- F
type()
Expressions Solutions
20
,int
"20232023"
,str
<class 'float'>
(Or justfloat
is sufficient on a quiz.)"44020"
4
4.0
There are two possible answers:
"Saturday"[2] + "Saturday"[1] + "Saturday"[4]
or
"Saturday"[2] + "Saturday"[6] + "Saturday"[4]
bool
True
Conditionals Solutions
- F
- F
- F
- 4.1 Any value < 11 4.2 Any value >= 11 and odd. (Since
x % z == 0
must be True andz
is 2, this meansx - 1
must be even.) 4.3 13 4.4 Any value != 13 4.5 Unreachable 4.6 13 4.7 Unreachable 4.8 Any value >= 11 and even
While Loops Basics Solutions
- 3
3
“1303132” - “036910875421”
While Loops and Functions Solutions
(We do not require that you write out all interim values as long as the initial and final values are correct like in the solution below. However, writing the interim values will help for practice purposes and to avoid mistakes!)
Here’s a link to a video of the solution!
1.1 “y appears 1 times.” would not print. This is because l1_idx
will not enter the while loop for l1_idx = 4
. (For more practice, it’d be good to diagram this instance out to see how it would impact the final values of other variables.)
1.2 An infinite loop would occur because l2_idx
would always equal 1
when returning to the top of the loop, and therefore l2_idx < len(WORD)
will always be True.
1.3 If l1_idx != l2_idx
is no longer required, this means that each letter can count itself twice. For example, WORD[0] == WORD[0]
is true, so n_appearances
for "h"
would increase to 2
.
1.4 There would be an infinite loop because l2_idx
will never increase, and therefore l2_idx < len(WORD)
will always be True.
1.5 There would be an index error because there would be the case where l1_idx = 5
, so on line 9 WORD[5]
would be searching for the element at index 5 in "happy"
, when the indexes only go up to 4.
2.1 Even though main
is defined before f
and g
, it isn’t called until after f
and g
are defined.
2.2 No because x = 4
, so x % 2 == 1
is False, and therefore the code block inside is never run.
2.3 There would be an error because x
is a local variable inside both f
and g
. In other words, x
is NOT a global variable. Therefore, the program does not recognize that x
exists in this context.