Signals & Caching Questions with Answers

7 practice questions on Signals & Caching (Django) with answers and step-by-step solutions. Try 7 samples below, then practise all 7 free in the AptiNest app.

Sample questions

Question 1Easy

Which command runs the project's tests?

  1. python manage.py check
  2. python manage.py runtests
  3. python manage.py pytest
  4. python manage.py test

Answer: D. python manage.py test

Explanation

It discovers test*.py files.

Step-by-step solution

  1. Run command.
  2. Results printed.

Why the other options are wrong

  • A. check validates configuration.
  • B. python manage.py runtests is not a Django management command; the correct choice is python manage.py test.
  • C. Needs a plugin.

Question 2Easy

Which signal fires right after a model's save() completes?

  1. after_save
  2. saved
  3. post_save
  4. on_commit

Answer: C. post_save

Explanation

Connect receivers with the @receiver decorator.

Step-by-step solution

  1. Define receiver.
  2. Connect to post_save.

Why the other options are wrong

  • A. after_save is not a Django signal; the correct choice is post_save.
  • B. saved is not a Django signal; the correct choice is post_save.
  • D. Different mechanism.

Question 3Medium

Which cache API stores a value for 60 seconds?

  1. cache.set('k', v, 60)
  2. cache.put('k', v, ttl=60)
  3. cache.save('k', v)
  4. cache.add('k', 60)

Answer: A. cache.set('k', v, 60)

Explanation

cache.get returns None on a miss unless a default is given.

Step-by-step solution

  1. Set key.
  2. Timeout seconds.

Why the other options are wrong

  • B. cache.put('k', v, ttl=60) is not the cache API; the correct choice is cache.set('k', v, 60).
  • C. cache.save('k', v) is not the cache API; the correct choice is cache.set('k', v, 60).
  • D. add has different semantics.

Question 4Medium

What does @transaction.atomic guarantee for a function?

  1. Faster queries, because Django batches the statements in one round trip to the DB
  2. All database changes inside commit together or roll back together
  3. Automatic retries
  4. Read-only mode

Answer: B. All database changes inside commit together or roll back together

Explanation

An exception rolls back the whole block.

Step-by-step solution

  1. Open transaction.
  2. Commit or rollback.

Why the other options are wrong

  • A. No speed effect.
  • C. No retry.
  • D. Not read-only.

Question 5Medium

How do you send a test request in a Django TestCase?

  1. self.client.get('/url/')
  2. requests.get('/url/')
  3. urllib.get('/url/')
  4. self.fetch('/url/')

Answer: A. self.client.get('/url/')

Explanation

The test client simulates requests without a server.

Step-by-step solution

  1. Use client.
  2. Assert response.

Why the other options are wrong

  • B. Real HTTP library, not the test client.
  • C. urllib has no get() function, and it would not use the Django test client anyway.
  • D. Tornado style.

Question 6Hard

What does the TestCase class in django.test do for each test?

  1. Drops and recreates the whole database before every single test method runs
  2. Shares data between tests
  3. Wraps it in a transaction that is rolled back, giving a clean database
  4. Uses the production DB

Answer: C. Wraps it in a transaction that is rolled back, giving a clean database

Explanation

Django creates a test database automatically.

Step-by-step solution

  1. Test starts.
  2. Rollback at end.

Why the other options are wrong

  • A. That is slower TransactionTestCase style.
  • B. Isolation is the aim.
  • D. A separate test DB is used.

Question 7Hard

Why use transaction.on_commit() to send an email after saving?

  1. It speeds up SQL by batching all statements into one round trip to the server
  2. It disables rollbacks
  3. It encrypts data
  4. It runs only if the transaction commits successfully

Answer: D. It runs only if the transaction commits successfully

Explanation

Avoids sending messages for data that rolls back.

Step-by-step solution

  1. Schedule callback.
  2. Run after commit.

Why the other options are wrong

  • A. No speed effect.
  • B. Opposite.
  • C. on_commit runs a callback after the commit; it does not encrypt anything.

Practise all 7 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Django topics · All topics