REST & API Questions with Answers

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

Sample questions

Question 1Easy

Which DRF permission class lets anonymous users read but allows only signed-in users to write?

  1. IsAuthenticated
  2. AllowAny
  3. IsAuthenticatedOrReadOnly
  4. IsAdminUser

Answer: C. IsAuthenticatedOrReadOnly

Explanation

IsAuthenticatedOrReadOnly lets everyone use the safe methods GET, HEAD and OPTIONS, and requires authentication for POST, PUT, PATCH and DELETE.

Step-by-step solution

  1. GET, HEAD and OPTIONS are the safe methods.
  2. Safe methods are allowed for everyone.
  3. Other methods require an authenticated user.

Why the other options are wrong

  • A. It also blocks anonymous reads.
  • B. It lets everyone write, not only signed-in users.
  • D. It limits access to staff users.

Tip: OrReadOnly means reads are open.

Question 2Easy

Which DRF class provides full CRUD with one class and a router?

  1. ModelViewSet
  2. APIView only
  3. GenericForm
  4. CrudView

Answer: A. ModelViewSet

Explanation

Register it with a DefaultRouter to generate list/detail URLs.

Step-by-step solution

  1. Define queryset and serializer.
  2. Router adds URLs.

Why the other options are wrong

  • B. APIView needs manual methods.
  • C. GenericForm is not a Django class; the correct choice is ModelViewSet.
  • D. CrudView is not a Django class; the correct choice is ModelViewSet.

Question 3Easy

Which package is the common toolkit for building REST APIs on Django?

  1. Flask-RESTful
  2. Express
  3. Django REST Framework
  4. FastAPI

Answer: C. Django REST Framework

Explanation

DRF adds serializers, viewsets, authentication and routers.

Step-by-step solution

  1. Install DRF.
  2. Add to INSTALLED_APPS.

Why the other options are wrong

  • A. Different framework.
  • B. Node.
  • D. Different framework.

Question 4Easy

A DRF view returns Response(data) without a status argument. Which HTTP status does the client get on success?

  1. 201 Created
  2. 204 No Content
  3. 302 Found
  4. 200 OK

Answer: D. 200 OK

Explanation

Response uses status 200 when none is given. Pass status=201 explicitly when a view creates a new object.

Step-by-step solution

  1. No status argument was passed.
  2. Response uses its default, which is 200.
  3. Pass status=201 for creates.

Why the other options are wrong

  • A. 201 is sent only when you pass it explicitly.
  • B. 204 is for success with no body.
  • C. 302 is a redirect, and nothing here redirects.

Tip: Default is 200; ask for 201 on creates.

Question 5Easy

Which decorator turns a function into a Django REST Framework view that accepts only the HTTP methods you list?

  1. @require_GET
  2. @route(method='GET')
  3. @api_view(['GET'])
  4. @rest_view('GET')

Answer: C. @api_view(['GET'])

Explanation

DRF's api_view wraps a function view, takes the allowed methods as a list, and gives the function DRF's Request and Response objects. Django's require_GET only restricts the method and does not give DRF's request handling.

Step-by-step solution

  1. api_view takes the list of allowed HTTP methods.
  2. It wraps the function so it receives DRF's Request object.
  3. A method not in the list gets a 405 response.

Why the other options are wrong

  • A. A Django decorator that does not provide DRF's Request and Response objects.
  • B. There is no such DRF decorator.
  • D. There is no such DRF decorator.

Tip: Function views in DRF start with @api_view.

Question 6Medium

What does a DRF serializer do?

  1. Renders HTML templates from model data and sends the finished page to the browser
  2. Runs migrations
  3. Manages sessions
  4. Converts model instances to JSON-friendly data and validates incoming data

Answer: D. Converts model instances to JSON-friendly data and validates incoming data

Explanation

ModelSerializer builds fields from a model.

Step-by-step solution

  1. Instance in.
  2. Data out.

Why the other options are wrong

  • A. Templates do that.
  • B. Migrations are run by manage.py migrate, not by serializers.
  • C. Sessions are handled by the session middleware, not by DRF serializers.

Question 7Medium

A serializer is created with an existing model instance and new data, and then .save() is called. Which method does DRF call?

  1. create(validated_data)
  2. update(instance, validated_data)
  3. to_representation(instance)
  4. run_validation(data)

Answer: B. update(instance, validated_data)

Explanation

save() calls update() when the serializer was given an instance, and create() when it was not. update() changes the instance's fields from validated_data and saves it.

Step-by-step solution

  1. save() checks whether an instance was passed in.
  2. An instance was passed, so it calls update(instance, validated_data).
  3. create() is used only when there is no instance.

Why the other options are wrong

  • A. create() is used only when no instance is passed.
  • C. That turns an object into output data; it does not save anything.
  • D. That validates the input; it does not write to the database.

Tip: instance given means update; no instance means create.

Question 8Medium

A client calls /api/items/?limit=10&offset=20. Which DRF pagination class reads those parameters?

  1. LimitOffsetPagination
  2. PageNumberPagination
  3. CursorPagination
  4. Paginator

Answer: A. LimitOffsetPagination

Explanation

LimitOffsetPagination reads limit (how many items) and offset (how many to skip). PageNumberPagination uses page and page_size, and CursorPagination uses an opaque cursor.

Step-by-step solution

  1. limit sets how many items to return.
  2. offset says how many items to skip.
  3. That pair is LimitOffsetPagination.

Why the other options are wrong

  • B. It reads page and page_size, not limit and offset.
  • C. It uses an opaque cursor parameter, not offset.
  • D. Django's core Paginator is not a DRF pagination class that reads query parameters.

Tip: limit and offset go together.

Question 9Hard

What does is_valid(raise_exception=True) do in a DRF serializer?

  1. Saves the object automatically and then raises an error when no field is changed
  2. Raises a ValidationError that DRF turns into a 400 response
  3. Returns a 500
  4. Skips validation

Answer: B. Raises a ValidationError that DRF turns into a 400 response

Explanation

It keeps view code short.

Step-by-step solution

  1. Validate.
  2. 400 on failure.

Why the other options are wrong

  • A. Save is separate.
  • C. It becomes 400.
  • D. It validates.

Practise all 9 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