WorksheetsLECTURE 04 Quizs (24.11.2025)
Total questions: 20
Worksheet time: 10mins
Programming Problem: Write a function called `describe_person` that takes a name (word) and an age (number), and gives back a sentence like "Sam is 20 years old." Add type hints and a docstring. Which answer is correct?
```python def describe_person(name: str, age: int) -> str: """Returns a description of the person.""" return f"{name} is {age} years old." ```
```python def describe_person(name, age): return f"{name} is {age} years old." ```
```python def describe_person(name: str, age: int): return f"{name} is {age} years old." ```
```python def describe_person(name: int, age: str) -> str: return f"{name} is {age} years old." ```
True or False: Type hinting in Python helps you know what kind of data a function should use.
True
False
Which function is written the best for a real project?
def f(x): return x+1
def f(x: int) -> int: """Adds one to x.""" return x+1
def f(x: int) -> int: return x+1
def f(x): """Adds one to x.""" return x+1
If you are making a function for a big project, what should you include?
Only the code
Code, type hints, and a docstring
Only type hints
Only a docstring
How do you show that a function gives back a whole number?
-> str
-> float
-> int
-> bool
True or False: NumPy broadcasting lets you add a number to every item in an array easily.
True
False
If you see `def add(x: int, y: int) -> int:`, what does it mean?
The function uses words and gives back a word
The function uses numbers and gives back a number
The function uses decimals and gives back a decimal
The function uses True/False and gives back True/False
Which tool checks your code and uses type hints to find mistakes?
Sphinx
mypy
Flask
pip
What happens when you do np.array([1, 2, 3]) + 5 in NumPy?
You get an error
Each number in the array gets 5 added to it
Each number is multiplied by 5
The number 5 is ignored
If you write `def greet(name: str) -> str:`, what should the function give back?
A number
A word or sentence
A list
True or False
Which is a good docstring for a function that adds two numbers?
# Adds two numbers
"""Adds two numbers and returns the result."""
'Adds two numbers'
Adds two numbers
Which is NOT a reason to use type hints?
Makes code easier to read
Helps find mistakes before running code
Makes code run faster
Helps fix bugs
True or False: NumPy will give an error if you try to add np.array([1, 2, 3]) + 5.
True
False
True or False: It is a good idea to use type hints and docstrings when writing functions for real projects.
True
False
Programming Problem: Write a Python function called `calculate_area` that takes two whole numbers, `length` and `width`, and gives back their product. Add type hints and a docstring. Which answer is correct?
```python def calculate_area(length, width): return length * width ```
```python def calculate_area(length: int, width: int) -> int: """Finds the area by multiplying length and width.""" return length * width ```
```python def calculate_area(length: float, width: float) -> float: return length * width ```
```python def calculate_area(length: int, width: int): return length * width ```
True or False: Writing type hints for both the inputs and outputs of a function makes your code easier to understand.
True
False
Which type hint shows that a function argument should be a word or sentence?
name: int
name: str
name: float
name: bool
True or False: A docstring is a special note under a function that explains what the function does.
True
False
Which tool uses docstrings to help make web pages for your code?
Git
Flask
Sphinx
pip
Programming Problem: Make a function called `add_numbers` that takes two whole numbers and gives back their sum. Add type hints and a docstring. Which answer is correct?
```python def add_numbers(a: int, b: int) -> int: """Returns the sum of a and b.""" return a + b ```
```python def add_numbers(a, b): return a + b ```
```python def add_numbers(a: float, b: float) -> float: return a + b ```
```python def add_numbers(a: int, b: int): return a + b ```
