0
0
Djangoframework~20 mins

Low-level cache API in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cache Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Django cache code?
Consider this Django code snippet using the low-level cache API:

from django.core.cache import cache

cache.set('key1', 'value1', timeout=5)
value = cache.get('key1')
print(value)

What will be printed immediately after running this code?
Django
from django.core.cache import cache

cache.set('key1', 'value1', timeout=5)
value = cache.get('key1')
print(value)
Avalue1
BNone
CKeyError
DTimeoutError
Attempts:
2 left
💡 Hint
Think about what cache.set and cache.get do immediately after setting a value.
state_output
intermediate
2:00remaining
What is the value of 'result' after this cache operation?
Given this code snippet:

from django.core.cache import cache

cache.set('counter', 10)
result = cache.incr('counter', 5)

What is the value of result after running this?
Django
from django.core.cache import cache

cache.set('counter', 10)
result = cache.incr('counter', 5)
ANone
B15
C10
DValueError
Attempts:
2 left
💡 Hint
What does cache.incr do to the stored value?
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in Django cache usage?
Identify which code snippet will cause a syntax error when using Django's low-level cache API:
Acache.set('key', 'value', timeout=60)
Bcache.get('key', default=None)
Ccache.set('key', 'value' timeout=60)
Dcache.delete('key')
Attempts:
2 left
💡 Hint
Look carefully at the commas separating arguments.
🔧 Debug
advanced
2:00remaining
Why does this cache increment raise an error?
Given this code:

from django.core.cache import cache

cache.set('visits', 'ten')
cache.incr('visits')

What error will be raised and why?
Django
from django.core.cache import cache

cache.set('visits', 'ten')
cache.incr('visits')
AValueError because 'ten' is not an integer
BKeyError because 'visits' does not exist
CTypeError because incr expects a string
DNo error, increments to 'ten1'
Attempts:
2 left
💡 Hint
cache.incr expects the stored value to be an integer.
🧠 Conceptual
expert
2:00remaining
Which statement about Django's low-level cache API is TRUE?
Select the correct statement about Django's low-level cache API behavior:
Acache.delete() raises an error if the key does not exist
Bcache.set() automatically serializes complex Python objects without configuration
Ccache.incr() can increment keys that do not exist by creating them with value 1
Dcache.get() returns None if the key does not exist and no default is provided
Attempts:
2 left
💡 Hint
Think about default return values and error handling in cache methods.