AlgoMaster Logo

Advanced Topics - Quiz

Last Updated: December 6, 2025

Advanced Topics Exercises

29 quizzes

1
Code Completion

Complete the code to check how many references currently point to the object stored in data

python
1
count = sys.getrefcount(data

Click an option to fill the blank:

2
Code Completion

Complete the code so that the weak reference returns the current target object when accessed

python
1
target = cache_ref

Click an option to fill the blank:

3
Code Completion

Complete the code to obtain the size in bytes of the object stored in payload

python
1
size_in_bytes = sys.getsizeof(payload

Click an option to fill the blank:

4
Multiple Choice

Which part of memory does Python primarily use to store objects like lists and dictionaries?

5
Multiple Choice

What is the main purpose of Python's garbage collector beyond reference counting?

6
Multiple Choice

Which function lets you manually trigger Python's cyclic garbage collection?

7
Multiple Choice

What happens to an object when its reference count reaches zero?

8
Multiple Choice

Which Python module is specifically used to create weak references?

9
Multiple Choice

Why might you use weak references in an in-memory cache?

10
Multiple Choice

What is a key benefit of writing a C extension for a CPU-bound Python function?

11
Multiple Choice

Which Python standard library module provides an FFI for calling functions in shared libraries?

12
Multiple Choice

What file extension is commonly used for Cython source files?

13
Multiple Choice

Which statement about reference counting in CPython is correct?

14
Multiple Choice

When calling a C function using ctypes, why should you set argtypes and restype?

15
Sequencing

Order the steps to create and use a simple C extension function in Python.

Drag and drop to reorder, or use the arrows.

16
Sequencing

Order the steps to call a function from a shared C library using ctypes.

Drag and drop to reorder, or use the arrows.

17
Output Prediction

What is the output of this code involving reference counts?

1import sys
2items = []
3count1 = sys.getrefcount(items)
4alias = items
5count2 = sys.getrefcount(items)
6print(count2 - count1)
18
Output Prediction

What is the output of this code using weak references?

1import weakref
2class Cache:
3    pass
4obj = Cache()
5ref = weakref.ref(obj)
6obj = None
7print(ref() is None)
19
Output Prediction

What is the output of this code using gc to demonstrate a cycle?

1import gc
2class Node:
3    def __init__(self):
4        self.ref = None
5a = Node()
6b = Node()
7a.ref = b
8b.ref = a
9del a, b
10unreachable_before = len(gc.garbage)
11gc.collect()
12unreachable_after = len(gc.garbage)
13print(unreachable_after >= unreachable_before)
20
Output Prediction

What is the output when inspecting sizes of two integers?

1import sys
2x = 10
3y = 10
4print(sys.getsizeof(x) == sys.getsizeof(y))
21
Output Prediction

What is the output of this Cython-like performance test written in pure Python?

1def sum_of_squares(n):
2    total = 0
3    for i in range(n):
4        total += i * i
5    return total
6print(sum_of_squares(3))
22
Bug Spotting

Find the bug in this code that tries to track reference counts.

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

python
1
import sys
2
 
3
def track(obj):
4
    before = sys.getrefcount(obj)
5
    temp = obj
6
    after = sys.getrefcount(obj)
7
    return before, after
23
Bug Spotting

Find the bug in this ctypes code that calls a C function returning an int.

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

python
1
import ctypes
2
 
3
lib = ctypes.CDLL('mylib.so')
4
 
5
result = lib.compute(5)
6
print(result + 1)
24
Matching

Match each concept with its most accurate description.

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

25
Matching

Match each tool with how it integrates C/C++ code with Python.

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

26
Fill in the Blanks

Complete the code to store objects in a cache that does not keep them alive unnecessarily.

python
1
import weakref
2
3
class Image:
4
def __init__(self, name):
5
self.name = name
6
7
thumbnails = (Image)
8
9
preview = Image('logo.png')
10
thumbnails['logo'] = preview
11
12
preview = None
13
print(thumbnails['logo']() is )

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the Cython build script to compile a .pyx module.

python
1
from setuptools import setup
2
from Cython.Build import
3
4
setup(
5
ext_modules=()
6
)

Click an option to fill blank 1:

28
Hotspot Selection

Click the line that creates a circular reference that the garbage collector must handle.

Click on the line to select.

python
1
class Node:
2
    def __init__(self):
3
        self.ref = None
4
 
5
first = Node()
6
second = Node()
7
first.ref = second
8
second.ref = first
29
Hotspot Selection

Click the line that creates a strong reference which prevents the object from being garbage collected while it exists.

Click on the line to select.

python
1
import weakref
2
 
3
class Data:
4
    pass
5
 
6
obj = Data()
7
ref = weakref.ref(obj)
8
keep = obj

Premium Content

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