Practice Problems

Changelog

Dec 2: Memory diagram 2 subquestions had some small errors/typos for what lines had docstrings and expressions. Before, line 1 was erroneously not included as having a docstring and line 8 was erroneously counted as including an expression.

Quiz 00 Practice Problems

The following questions will help solidify concepts learned in class, as well as prepare you for the quizzes and final!

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

  1. What is a bool data type in Python?

    1. Data type for storing text

    2. Data type for storing whole numbers such as -10, 3, or 100

    3. Data type for storing True/False values

    4. Data type for storing any type of information

    5. 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.

 

  1. What is an int data type in Python?

    1. Data type for storing text

    2. Data type for storing whole numbers such as -10, 3, or 100

    3. Data type for storing True/False values

    4. Data type for storing any type of information

    5. 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.

 

  1. What is a str data type in Python?

    1. Data type for storing text

    2. Data type for storing whole numbers such as -10, 3, or 100

    3. Data type for storing True/False values

    4. Data type for storing any type of information

    5. 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.

 

  1. An int literal can begin with any number of zeroes.

    1. True

    2. False

SHOW SOLUTION

The correct answer is B.

 

  1. Which of the following literals are a float?

    1. 4

    2. "4"

    3. 4.0

    4. "4.0"

SHOW SOLUTION

The correct answer is C.

 

  1. What function can you use to determine the type of an object in Python?

    1. print()

    2. str()

    3. len()

    4. type()

SHOW SOLUTION

The correct answer is D.

 

  1. What is the type of the following expression?

    1.5 + 2
    1. int

    2. float

    3. str

    4. bool

    5. TypeError

SHOW SOLUTION

The correct answer is B.

 

  1. What is the type of the following expression?

    len("cottage")
    1. int

    2. float

    3. str

    4. bool

    5. TypeError

SHOW SOLUTION

The correct answer is A.

 

  1. What is the result of the following expression?

    "110" + "110"
    1. 220

    2. "110110"

    3. TypeError

    4. "220"

    5. "110""110"

SHOW SOLUTION

The correct answer is B.

 

  1. What is the result of the following expression?

    102 // 5
    1. 20

    2. 20.4"

    3. "20"

    4. TypeError

    5. 21

SHOW SOLUTION

The correct answer is A.

 

  1. What is the type of this value in Python?

    "True"
    1. bool

    2. str

    3. TypeError

    4. int

    5. float

SHOW SOLUTION

The correct answer is B.

 

  1. What value will the following expression evaluate to?

    "map"[1]
    1. m

    2. "m"

    3. a

    4. "a"

    5. TypeError

SHOW SOLUTION

The correct answer is D.

 

  1. What will the following expression evaluate to?

    int(3.8 + 1)
    1. 4.8

    2. 5

    3. 3.81

    4. 4

    5. TypeError

SHOW SOLUTION

The correct answer is D.

 

Expressions

  1. What are the types of the following expressions and what values do they evaluate to? If a TypeError occurs, just write TypeError.

    1.1. 1.5 + 2

    1.2. "hehe" * 2

    1.3. len("110") ** 2

    1.4. str(110) * 2.1

    1.5. float("100.0") / 20

    1.6. 21 // 2 ** 2 + 3

    1.7. float("220") >= float("100" + "100")

    1.8. int("COMP 110"[5]) + 99.0

    1.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

 

  1. Which of the following expressions correctly concatenates two strings together?

    1. "clam" * "chowder"

    2. "clam" + "chowder"

    3. "clam" , "chowder"

    4. "clam" : "chowder"

SHOW SOLUTION

The correct answer is B.

 

  1. When using subscription syntax, what index does Python start with?

    1. -1

    2. 0

    3. 1

    4. ""

    5. True

SHOW SOLUTION

The correct answer is B.

 

  1. What value would you substitute for x to make the following expression True?
(3 + x) == ((55 // 11) ** 2)

SHOW SOLUTION

The correct answer is 22.

 

  1. Use subscription notation, string concatenation, and the string "nevermind" to write an expression that evaluates to "nvm".

SHOW SOLUTION

The correct answer is "nevermind"[0] + "nevermind"[2] + "nevermind"[5].

 

Functions

  1. 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. 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.

 

  1. 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.

 

  1. 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.

 

  1. 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.

 

  1. What does the len() function do in Python?

    1. Converts a value to a string

    2. Rounds a number to the nearest whole number

    3. Returns the length of a sequence, such as a str

    4. Converts a string to a number

    5. Counts the digits in an int

SHOW SOLUTION

The correct answer is C.

 

  1. 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.

 

  1. 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_teaspoons that 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

  1. 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.

 

  1. Write the function signature for a function called pos_or_neg that takes as input an integer and returns "Positive" if the integer is positive and returns "Negative" if the integer is negative. Use number as the name of the parameter.

SHOW SOLUTION

The correct answer is:

  def pos_or_neg(number: int) -> str:

 

  1. Write the function signature for a function called gcd that takes two integers as input and returns the integer that is their greatest common divisor. Use num_one and num_two as your parameter names.

SHOW SOLUTION

The correct answer is:

  def gcd(num_one: int, num_two: int) -> int:

 

Memory Diagrams

  1. Trace a memory diagram of the following code listing and answer the following subquestions.

    1  def total_price(calzones: int, strombolis: int) -> int:
    2      """Returns the total price for the order of food, including a service fee of $3."""
    3      return calzones_price(calzones=calzones) + strombolis_price(strombolis=strombolis) + 3
    4
    5  def calzones_price(calzones: int) -> int:
    6      """Returns the price of the given number of calzones."""
    7      return 7 * calzones
    8  
    9  def strombolis_price(strombolis: int) -> int:
    10     """Returns the price of the given number of strombolis."""
    11     return 8 * strombolis
    12
    13 print(total_price(calzones=4, strombolis=2))

    1.1. What line(s) do function definition signatures appear on?

    1.2. What line(s) do docstrings appear on?

    1.3. What line(s) do expressions appear on?

    1.4. What line(s) do function calls appear on?

    1.5. Write a function call to calzones_price that would evaluate to 28.

SHOW SOLUTION

Memory diagram of calzones and strombolis

The correct answers are:

1.1. Lines 1, 5, and 9

1.2. Lines 2, 6, and 10

1.3. Lines 3, 7, 11, 13

1.4. Lines 3 and 13

1.5. calzones_price(calzones=4)

 

  1. Trace a memory diagram of the following code listing and answer the following subquestions.

    1  """Functions of a circle..."""
    2
    3
    4  def main() -> None:
    5      """Entrypoint of Program"""
    6      print(circumference(radius=1.0))
    7      print(area(radius=1.0))
    8      return None
    9
    10
    11 def area(radius: float) -> float:
    12     """Calculate area of a circle"""
    13     return 3.14 * radius ** 2
    14
    15
    16 def circumference(radius: float) -> float:
    17     """Calculate circumference"""
    18     return 2 * 3.14 * radius
    19
    20
    21 main()

    2.1. What line(s) do function definition signatures appear on?

    2.2. What line(s) do docstrings appear on?

    2.3. What line(s) do expressions appear on?

    2.4. What line(s) do function calls appear on?

    2.5. What is the return type of area? What is the return type of main?

SHOW SOLUTION

Memory diagram of circumference and area

The correct answers are:

2.1. Lines 1, 4, 11, and 16

2.2. Lines 5, 12, and 17

2.3. Lines 6, 7, 13, 18, 21

2.4. Lines 6, 7, and 21

2.5. area has a return type of float and main has a return type of None.

 

Solutions

Multiple Choice and True/False

  1. C
  2. B
  3. A
  4. B
  5. C
  6. D
  7. B
  8. A
  9. B
  10. A
  11. B
  12. D
  13. D

Expressions

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

  1. B

  2. B

  3. 22

  4. "nevermind"[0] + "nevermind"[2] + "nevermind"[5]

Functions

  1. 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. 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.

  2. 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.

  3. After the function is executed, you will return back to the point in the code where the function was called.

  4. If there is no return statement in a function, it returns None by default.

  5. C

6.1. 8

6.2. The type of name is str and the return type of the function is int.

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

  1. 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.

def pos_or_neg(number: int) -> str:
def gcd(num_one: int, num_two: int) -> int:

Memory Diagrams

Memory diagram of calzones and strombolis

1.1. Lines 1, 5, and 9

1.2. Lines 2, 6, and 10

1.3. Lines 3, 7, 11, 13

1.4. Lines 3 and 13

1.5. `calzones_price(calzones=4)`

Memory diagram of circumference and area

2.1. Lines 4, 11, and 16

2.2. Lines 5, 12, and 17

2.3. Lines 6, 7, 8, 13, 18, 21

2.4. Lines 21, 6, and 7

2.5. `area` has a return type of `float` and `main` has a return type of `None`.
Contributor(s): Alyssa Lytle, Team 110, Izzi Hinks, Benjamin Eldridge