Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

LECTURE 04 Quizs (24.11.2025)

Total questions: 20

Worksheet time: 10mins

Name
Class
Date
1.

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?

a)

```python def describe_person(name: str, age: int) -> str: """Returns a description of the person.""" return f"{name} is {age} years old." ```

b)

```python def describe_person(name, age): return f"{name} is {age} years old." ```

c)

```python def describe_person(name: str, age: int): return f"{name} is {age} years old." ```

d)

```python def describe_person(name: int, age: str) -> str: return f"{name} is {age} years old." ```

2.

True or False: Type hinting in Python helps you know what kind of data a function should use.

a)

True

b)

False

3.

Which function is written the best for a real project?

a)

def f(x): return x+1

b)

def f(x: int) -> int: """Adds one to x.""" return x+1

c)

def f(x: int) -> int: return x+1

d)

def f(x): """Adds one to x.""" return x+1

4.

If you are making a function for a big project, what should you include?

a)

Only the code

b)

Code, type hints, and a docstring

c)

Only type hints

d)

Only a docstring

5.

How do you show that a function gives back a whole number?

a)

-> str

b)

-> float

c)

-> int

d)

-> bool

6.

True or False: NumPy broadcasting lets you add a number to every item in an array easily.

a)

True

b)

False

7.

If you see `def add(x: int, y: int) -> int:`, what does it mean?

a)

The function uses words and gives back a word

b)

The function uses numbers and gives back a number

c)

The function uses decimals and gives back a decimal

d)

The function uses True/False and gives back True/False

8.

Which tool checks your code and uses type hints to find mistakes?

a)

Sphinx

b)

mypy

c)

Flask

d)

pip

9.

What happens when you do np.array([1, 2, 3]) + 5 in NumPy?

a)

You get an error

b)

Each number in the array gets 5 added to it

c)

Each number is multiplied by 5

d)

The number 5 is ignored

10.

If you write `def greet(name: str) -> str:`, what should the function give back?

a)

A number

b)

A word or sentence

c)

A list

d)

True or False

11.

Which is a good docstring for a function that adds two numbers?

a)

# Adds two numbers

b)

"""Adds two numbers and returns the result."""

c)

'Adds two numbers'

d)

Adds two numbers

12.

Which is NOT a reason to use type hints?

a)

Makes code easier to read

b)

Helps find mistakes before running code

c)

Makes code run faster

d)

Helps fix bugs

13.

True or False: NumPy will give an error if you try to add np.array([1, 2, 3]) + 5.

a)

True

b)

False

14.

True or False: It is a good idea to use type hints and docstrings when writing functions for real projects.

a)

True

b)

False

15.

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?

a)

```python def calculate_area(length, width): return length * width ```

b)

```python def calculate_area(length: int, width: int) -> int: """Finds the area by multiplying length and width.""" return length * width ```

c)

```python def calculate_area(length: float, width: float) -> float: return length * width ```

d)

```python def calculate_area(length: int, width: int): return length * width ```

16.

True or False: Writing type hints for both the inputs and outputs of a function makes your code easier to understand.

a)

True

b)

False

17.

Which type hint shows that a function argument should be a word or sentence?

a)

name: int

b)

name: str

c)

name: float

d)

name: bool

18.

True or False: A docstring is a special note under a function that explains what the function does.

a)

True

b)

False

19.

Which tool uses docstrings to help make web pages for your code?

a)

Git

b)

Flask

c)

Sphinx

d)

pip

20.

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?

a)

```python def add_numbers(a: int, b: int) -> int: """Returns the sum of a and b.""" return a + b ```

b)

```python def add_numbers(a, b): return a + b ```

c)

```python def add_numbers(a: float, b: float) -> float: return a + b ```

d)

```python def add_numbers(a: int, b: int): return a + b ```