Quiz 00 Practice Problems
The following questions will help solidify concepts learned in class, as well as prepare you for the quizzes and final!
If you feel overwhelmed by the number of questions here, start with just a couple from each section that you think would help fill in any knowledge gaps you have. Then, work through the remaining questions as needed for additional practice.
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 and challenge questions on Gradescope.
If you find yourself feeling lost, please ask for help in office hours or tutoring.
Multiple Choice and True/False
What is a
booldata type in Python?Data type for storing text
Data type for storing whole numbers such as
-10,3, or100Data type for storing True/False values
Data type for storing any type of information
Data type for storing numbers with a decimal point such as
1.0,3.14, or-0.1
SHOW SOLUTION
The correct answer is C.
What is an
intdata type in Python?Data type for storing text
Data type for storing whole numbers such as
-10,3, or100Data type for storing True/False values
Data type for storing any type of information
Data type for storing numbers with a decimal point such as
1.0,3.14, or-0.1
SHOW SOLUTION
The correct answer is B.
What is a
strdata type in Python?Data type for storing text
Data type for storing whole numbers such as
-10,3, or100Data type for storing True/False values
Data type for storing any type of information
Data type for storing numbers with a decimal point such as
1.0,3.14, or-0.1
SHOW SOLUTION
The correct answer is A.
An
intliteral can begin with any number of zeroes.True
False
SHOW SOLUTION
The correct answer is B.
Which of the following literals are a
float?4"4"4.0"4.0"
SHOW SOLUTION
The correct answer is C.
What function can you use to determine the type of an object in Python?
print()str()len()type()
SHOW SOLUTION
The correct answer is D.
What is the type of the following expression?
1.5 + 2intfloatstrboolTypeError
SHOW SOLUTION
The correct answer is B.
What is the type of the following expression?
len("cottage")intfloatstrboolTypeError
SHOW SOLUTION
The correct answer is A.
What is the result of the following expression?
"110" + "110"220"110110"TypeError"220""110""110"
SHOW SOLUTION
The correct answer is B.
What is the result of the following expression?
102 % 5220.4""20"TypeError2.0
SHOW SOLUTION
The correct answer is A.
What is the type of this value in Python?
"True"boolstrTypeErrorintfloat
SHOW SOLUTION
The correct answer is B. While this may look like a bool, the quotes make it a str!
What value will the following expression evaluate to?
"map"[1]"ap""m"str"a"TypeError
SHOW SOLUTION
The correct answer is D. Don’t forget, Python indexing begins at 0!
What will the following expression evaluate to?
int(3.8 + 1)4.853.814TypeError
SHOW SOLUTION
The correct answer is D. 3.8 + 1 evaluates to the float 4.8, but when converted to an int by int(), the precision is removed/truncated.
Expressions
What are the types of the following expressions and what values do they evaluate to? If a
TypeErroroccurs, just writeTypeError.1.1.
1.5 + 21.2.
"hehe" * 21.3.
len("110") ** 21.4.
str(110) * 2.11.5.
float("100.0") / 201.6.
21 // 2 ** 2 + 31.7.
float("220") >= float("100" + "100")1.8.
int("COMP 110"[5]) + 99.01.9.
(42 % 4) == (79 % 11)1.10.
int(4.99)
SHOW SOLUTION
The correct answers are:
1.1. Type: float Value: 3.5
1.2. Type: str Value: "hehehehe"
1.3. Type: int Value: 9
1.4. TypeError
1.5. Type: float Value: 5.0
1.6. Type: int Value: 8
1.7. Type: bool Value: False
1.8. Type: float Value: 100.0
1.9. Type: bool Value: True
1.10. Type: int Value: 4
Which of the following expressions correctly concatenates two strings together?
"Michael" * "Jordan""Michael" + "Jordan""Michael" , "Jordan""Michael" : "Jordan"
SHOW SOLUTION
The correct answer is B.
When using subscription syntax, what index does Python start with?
-101""True
SHOW SOLUTION
The correct answer is B.
- What value would you substitute for
xto make the following expression True? Note: There is only one correct value here.
(3 + x) == ((55 // 11) ** 2)SHOW SOLUTION
The correct answer is 22.
- Use subscription notation, string concatenation, and the string
"nevermind"to write an expression that evaluates to"nvm". Hint: Break this problem down into smaller pieces by first thinking of how you would write an expression using"nevermind"and subscription notation that evaluates to"n".
SHOW SOLUTION
The correct answer is "nevermind"[0] + "nevermind"[2] + "nevermind"[5].
Functions
- What is a function call, and how does it differ from a function definition?
SHOW SOLUTION
A function definition is where you create the function and specify what it does. It includes the function signature and everything indented below it. The signature consists of the function name, parameters, the types of the parameters, and the return type. The function body contains the code block that executes when the function is called, and generally ends with a return statement. A function call occurs when you invoke or execute the function by writing the function name followed by parentheses. If the function accepts inputs, the parentheses will contain the values assigned to the parameters, also known as arguments. When a function definition is encountered, the function body is not executed; it is bypassed until the function is called. To call a function, you must first define it.
- What is the difference between parameters and arguments in the context of functions?
SHOW SOLUTION
Parameters are the variables listed in the function signature that define the expected inputs. Your parameters will hold the values passed into the function when the function is called. These values that are assigned to the parameters are called arguments. Arguments are the actual values passed to the function when it is called.
- When you call a function, once the function is executed, where do you return back to?
SHOW SOLUTION
After the function is executed, you will return back to the point in the code where the function was called. In your memory diagrams, this is called the “Return Address” or just “RA”.
- If you never encounter a return statement in your function, what is your return value?
SHOW SOLUTION
If there is no return statement in a function, it returns None by default.
What does the
len()function do in Python?Converts a value to a string
Rounds a number to the nearest whole number
Returns the length of a sequence, such as a
strConverts a string to a number
Counts the digits in an int
SHOW SOLUTION
The correct answer is C.
Given the following function definition, answer the following questions.
def evaluate_length(name: str) -> int: """This function returns the length of the name.""" return len(name)6.1. What is the result of the following function call?
evaluate_length("airplane")6.2. What is the type of the parameter
name? What is the return type of this function?
SHOW SOLUTION
The correct answers are:
6.1. 8
6.2. The type of name is str and the return type of the function is int.
Given the following function definition, answer the following questions.
def tablespoons_to_teaspoons(tablespoons: int) -> str: """This functions tells you how many teaspoons are in the given number of tablespoons.""" return str(tablespoons * 3) + " teaspoons"7.1. Write a function call to
tablespoons_to_teaspoonsthat returns the string"9 teaspoons".7.2. What is the type of the parameter
tablespoons? What is the return type of this function?
SHOW SOLUTION
The correct answers are:
7.1. tablespoons_to_teaspoons(tablespoons=3)
7.2. The type of tablespoons is int and the return type of the function is str.
Function Signatures
- What is a function signature, and why is it significant?
SHOW SOLUTION
A function signature refers to the first line of a function definition, where the def keyword is used. Following the def keyword is the function name, and after the function name is a set of parentheses that enclose the parameter list. After the parameter list comes the return type. The function signature is important because it defines how the function can be called and what kinds of inputs and outputs are expected. Without a signature, you wouldn’t know what parameters to provide or even how to call the function, since the function name would be missing.
- Write the function signature for a function called
pos_or_negthat takes as input an integer and returns"Positive"if the integer is positive and returns"Negative"if the integer is negative. Usenumberas the name of the parameter. Note: You are not writing the body of this function so do not worry about how it would actually work on the inside for this question.
SHOW SOLUTION
The correct answer is:
def pos_or_neg(number: int) -> str:
- Write the function signature for a function called
gcdthat takes two integers as input and returns the integer that is their greatest common divisor. Usenum_oneandnum_twoas your parameter names. Note: You are not writing the body of this function so do not worry about how it would actually work on the inside for this question.
SHOW SOLUTION
The correct answer is:
def gcd(num_one: int, num_two: int) -> int: