Thursday, October 8, 2026

Python Requests Package quiz


Python Requests Package — Complete Quiz with Answers


Basic + Intermediate


Q1. Which Python package is commonly used to send HTTP requests easily?

A) urllib

B) requests

C) httpclient

D) httplib

Answer: B) requests


Q2. Which attribute returns the HTTP status code of a response object r?

A) r.status

B) r.code

C) r.status_code

D) r.http_code

Answer: C) r.status_code


Q3. response.json() converts a JSON response into a Python dictionary or list.

A) True

B) False

Answer: A) True


Q4. The requests package is included in Python's standard library and does not require installation.

A) True

B) False

Answer: B) False


Q5. Which methods are valid HTTP methods supported directly by Requests?

A) delete()

B) get()

C) post()

D) put()

Answer: B, C, D

Note: delete() is also supported by Requests; this question's original answer set was incomplete.


Q6. Which parameter is used to send query-string values in requests.get()?

A) data

B) params

C) query

D) args

Answer: B) params



---


Intermediate Code Quiz


Q7. What is printed?


import requests

r = requests.get('https://example.com')

print(r.status_code)


A) 404

B) 200

C) 500

D) 301

Answer: B) 200


Q8. What does this send to the server?


requests.get(

    'https://api.example.com/users',

    params={'page': 2, 'limit': 10}

)


A) JSON body

B) Form data

C) URL query parameters

D) HTTP headers

Answer: C) URL query parameters


Q9. What Python object is most likely returned?


response = requests.get(url)

data = response.json()


If the API returns a JSON object: A) tuple

B) list

C) dict

D) bytes

Answer: C) dict


Q10. response.text returns the response body decoded as text.

A) True

B) False

Answer: A) True


Q11. What is the purpose of response.raise_for_status()?

A) Converts response to JSON

B) Raises an exception for unsuccessful HTTP status codes

C) Retries the request

D) Closes the connection

Answer: B


Q12. What does this request do?


requests.post(

    url,

    json={'name': 'John'},

    headers={'Authorization': 'Bearer TOKEN'}

)


A) Sends data as URL parameters

B) Sends JSON data in the request body

C) Sends a POST request

D) Sends an Authorization header

Answer: B, C, D


Q13. What does timeout=5 do?


requests.get(url, timeout=5)


A) Retries 5 times

B) Sets a 5-second timeout

C) Sends 5 requests

D) Waits 5 minutes

Answer: B) Sets a 5-second timeout


Q14. Which exception can be caught for timeout or other Requests-related network errors?


try:

    requests.get(url, timeout=3)

except ______ as e:

    print(e)


A) ValueError

B) KeyError

C) requests.exceptions.RequestException

D) IndexError

Answer: C


Q15. What does this return?


r.headers.get('Content-Type')


A) HTTP status code

B) Content-Type header value

C) Request body

D) URL

Answer: B) Content-Type header value


Q16. What does this send?


requests.post(url, data={'name': 'John'})


A) JSON data

B) Form data

C) Query parameters

D) Cookies

Answer: B) Form data


Fresh Intermediate Code Quiz


Q17. What does r.ok indicate?


r = requests.get(url)

print(r.ok)


A) True only for status 200

B) True only for status 201

C) True only for status 300

D) True when status is below 400

Answer: D


Q18. What does r.content provide?


r = requests.get(url)

content = r.content


A) JSON dictionary

B) Raw response bytes

C) HTTP status

D) Headers

Answer: B) Raw response bytes


Q19. What happens for HTTP 404?

r = requests.get(url)

r.raise_for_status()

print("Done")

A) Prints Done

B) Returns 200

C) Retries automatically

D) Raises HTTPError

Answer: D


Q20. Which arguments can be supplied to requests.get()?

A) cookies

B) headers

C) verify

D) All of the above

Answer: D


Q21. A requests.Session can persist settings such as cookies across multiple requests.

A) True

B) False

Answer: A) True


Q22. What is the purpose of this code?

session = requests.Session()

session.headers.update({'User-Agent': 'MyApp'})

A) Sends one request

B) Sets a default header for session requests

C) Deletes headers

D) Disables cookies

Answer: B


Q23. What does this do?

requests.get(url, allow_redirects=False)

A) Prevents automatic redirects

B) Forces a redirect

C) Disables HTTPS

D) Retries redirects

Answer: A


Q24. What does this do?


requests.get(

    url,

    cookies={'session_id': 'abc123'}

)


A) Sends cookies with the request

B) Creates a new session

C) Deletes cookies

D) Reads response headers

Answer: A


Q25. Which code correctly saves an image response?

r = requests.get(url)

A)open('image.jpg', 'w').write(r.text)

B)open('image.jpg', 'r').write(r.content)

C) open('image.jpg', 'wb').write(r.content)

D) open('image.jpg', 'a').write(r.json())

Answer: C


Q26. What is the purpose of requests.head(url)?

A) Downloads the complete response body

B) Sends a POST request

C) Deletes a resource

D) Fetches headers without normally downloading the response body

Answer: D


Quick Revision


requests.get() → GET request

requests.post() → POST request

requests.put() → PUT request

requests.delete() → DELETE request


params={} → URL query parameters

data={} → Form/request data

json={} → JSON request body

headers={} → HTTP headers

cookies={} → Cookies


r.status_code → HTTP status code

r.ok → True for status < 400

r.text → Response as text

r.content → Response as bytes

r.json() → Parse JSON response

r.headers → Response headers


raise_for_status() → Raise HTTP error

timeout=5 → Request timeout

Session() → Persist settings/cookies

allow_redirects=False→ Don't follow redirects

head() → Fetch headers

Tuesday, October 6, 2026

Python try-except Quiz

 Python try-except Quiz


1. What is the output? try: print(10 / 0) except ZeroDivisionError: print("Error")

   A. Error

   B. 10

   C. No output

   D. 0


2. What happens if an exception occurs inside try and there is no matching except block?

   A. The finally block is skipped automatically

   B. The exception becomes None

   C. The program raises the exception

   D. Python ignores the exception


3. A finally block normally executes whether or not an exception occurs in the associated try block.

   True

   False


4. What is the output? try: x = int("abc") except ValueError: print("Invalid") else: print("Valid")

   A. No output

   B. Valid

   C. Invalid

   D. abc


5. Which of the following can be used to explicitly raise an exception in Python? Select all that apply.

   A. throw ValueError("Invalid")

   B. raise RuntimeError("Failed")

   C. raise ValueError("Invalid")

   D. raise Exception("Problem")


6. The except block must always specify an exception type.

   True

   False


7. What is the output? try: print(5 / 1) except ZeroDivisionError: print("Error") else: print("Success")

   A. Success only

   B. 5 then Error

   C. 5.0 then Success

   D. Error


8. Which exception is most appropriate when accessing a dictionary key that does not exist?

   A. TypeError

   B. NameError

   C. IndexError

   D. KeyError


9. What does the else block in try-except-else execute when the try block completes successfully?

   A. It executes only when an exception occurs

   B. It executes after the try block

   C. It always executes before try

   D. It replaces finally


10. What is the output? try: numbers = [1, 2, 3] print(numbers[5]) except IndexError: print("Index problem") finally: print("Done")

   A. Index problem then Done

   B. 5 then Done

   C. Index problem only

   D. Done only