AlgoMaster Logo

Debugging & Profiling - Quiz

Last Updated: December 6, 2025

Debugging & Profiling Exercises

30 quizzes

1
Code Completion

Complete the code to record an informational message about a user login using the logging module

python
1
logging.info()

Click an option to fill the blank:

2
Code Completion

Complete the code to measure how long it takes to run the callable fetch_data once using timeit

python
1
duration = timeit.timeit(fetch_data, number=)

Click an option to fill the blank:

3
Code Completion

Complete the code so that pdb will pause execution at this point for interactive debugging

python
1
pdb.()

Click an option to fill the blank:

4
Multiple Choice

Which type of error occurs when code is syntactically valid but produces an incorrect result?

5
Multiple Choice

When starting to debug a reported issue, what is the first step in an effective debugging process?

6
Multiple Choice

Which command starts a Python script under the pdb debugger from the command line?

7
Multiple Choice

Inside pdb, which command executes the current line and stops at the next line in the same function?

8
Multiple Choice

Which logging level is most appropriate for detailed information used only during debugging?

9
Multiple Choice

What is one main advantage of using logging instead of print statements?

10
Multiple Choice

Which built-in module is commonly used to profile CPU time of Python code?

11
Multiple Choice

Why is timeit preferred over manually using time.time() for small snippets?

12
Multiple Choice

Which tool lets you annotate Python functions with @profile to see line-by-line memory usage?

13
Multiple Choice

What is a key purpose of using tracemalloc in a Python application?

14
Sequencing

Order the steps to use pdb.set_trace() to debug a function inside a script.

Drag and drop to reorder, or use the arrows.

15
Sequencing

Order the steps to profile a slow_function using cProfile within a script.

Drag and drop to reorder, or use the arrows.

16
Output Prediction

What is the output of this code related to runtime vs logical errors?

1def divide(a, b):
2    try:
3        return a / b
4    except ZeroDivisionError:
5        return "cannot divide"
6
7result = divide(6, 3)
8print(result)
17
Output Prediction

What does this code print when using logging with different levels?

1import logging
2logging.basicConfig(level=logging.WARNING)
3logging.debug("debug message")
4logging.error("error message")
18
Output Prediction

What is the output when using timeit to measure a tiny operation?

1import timeit
2value = timeit.timeit("1+1", number=1)
3print(int(value == 0))
19
Output Prediction

Consider this simplified profiling-like counter. What is printed?

1def counted(n):
2    calls = 0
3    for _ in range(n):
4        calls += 1
5    return calls
6
7print(counted(5))
20
Output Prediction

What does this memory-related example print?

1sizes = []
2for i in range(3):
3    data = [0] * (10 ** i)
4    sizes.append(len(data))
5
6print(sum(sizes))
21
Bug Spotting

This function is meant to log an error when list access fails. Find the bug.

Click on the line(s) that contain the bug.

python
1
import logging
2
 
3
logging.basicConfig(level=logging.ERROR)
4
 
5
def get_item(items, index):
6
    try:
7
        return items[index]
8
    except IndexError as exc:
9
        logging.info("Index out of range: %s", exc)
10
        return None
22
Bug Spotting

This code is intended to profile a function using cProfile. Find the bug.

Click on the line(s) that contain the bug.

python
1
import cProfile
2
 
3
 
4
def fetch_all():
5
    total = 0
6
    for i in range(1000):
7
        total += i
8
    return total
9
 
10
 
11
if __name__ == "__main__":
12
    cProfile.run(fetch_all())
23
Matching

Match each pdb command with what it does.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

24
Matching

Match each profiling tool to what it primarily measures.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

25
Fill in the Blanks

Complete the code to configure logging to write DEBUG and higher messages to a file named app.log.

python
1
import logging
2
3
logging.basicConfig(filename=, level=)
4
5
logging.debug("debug details")

Click an option to fill blank 1:

26
Fill in the Blanks

Complete the code to measure the average time of calling compute() 100 times using timeit.

python
1
import timeit
2
3
average = timeit.timeit(compute, number=) /
4
print(average)

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the code to start and report basic tracemalloc memory statistics.

python
1
import tracemalloc
2
3
tracemalloc.()
4
# ... run some code here ...
5
current, peak = tracemalloc.()
6
print(peak)

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the code to decorate a function so that memory_profiler can measure its memory usage.

python
1
from memory_profiler import
2
3
@
4
def load_data():
5
buffer = [b"x" * 1024] * 1000
6
return len(buffer)

Click an option to fill blank 1:

29
Hotspot Selection

Click the line where a potential runtime error can occur due to list indexing.

Click on the line to select.

python
1
values = [1, 2, 3]
2
position = 5
3
item = values[position]
4
print(item)
30
Hotspot Selection

Click the line most appropriate for setting a breakpoint when debugging an incorrect average calculation.

Click on the line to select.

python
1
def calculate_average(numbers):
2
    total = sum(numbers)
3
    count = len(numbers)
4
    average = total / count
5
    return average
6
 
7
result = calculate_average([10, 20, 30])
8
print(result)

Premium Content

Subscribe to unlock full access to this content and more premium articles.