EXAM
Questions from all the chapters!
These questions are not sorted by chapter, so it will give you a more realistic experience for the test.
- What does the
readlines()
method in file handling return?- A. The entire file content as a single string.
- B. The number of lines in the file.
- C. A list of strings, each representing a line in the file.
- D. The first line of the file only.
Click to see the answer
Answer: C. A list of strings, each representing a line in the file.
Explanation: Thereadlines()
method reads all the lines in a file and returns them as a list of strings.
- How can you reverse the order of elements in a list
myList
?- A.
myList.sort(reverse=True)
- B.
myList.reverse()
- C.
reverse(myList)
- D.
myList.reversed()
- A.
Click to see the answer
Answer: B. myList.reverse()
reverse()
method reverses the elements of the list myList
in place.
- What will
dict.get('key')
return if ‘key’ does not exist in the dictionarydict
?- A. KeyError
- B. None
- C. 0
- D. An empty string
Click to see the answer
Answer: B. None
Explanation: Theget()
method returns None
if the specified key is not found in the dictionary.
- Which Pandas function is used to select data based on row and column labels?
- A.
select()
- B.
iloc[]
- C.
loc[]
- D.
filter()
- A.
Click to see the answer
Answer: C. loc[]
loc[]
function in Pandas is used to select data based on row and column labels.
- In the context of the Requests library, what does
raise_for_status()
do?- A. Increases the priority of the request.
- B. Raises an exception for unsuccessful status codes.
- C. Checks if the server status is active.
- D. Updates the status of the request.
Click to see the answer
Answer: B. Raises an exception for unsuccessful status codes.
Explanation: Theraise_for_status()
method in the Requests library raises an HTTPError exception for responses with HTTP error status codes (like 4xx or 5xx).
- What type of request does
requests.post(url)
send?- A. GET request
- B. POST request
- C. PUT request
- D. DELETE request
Click to see the answer
Answer: B. POST request
Explanation: Therequests.post(url)
function sends an HTTP POST request to the specified URL.
- What does
response.status_code
return in therequests
library?- A. The response headers
- B. The URL of the request
- C. The status code of the HTTP response
- D. The body of the response
Click to see the answer
Answer: C. The status code of the HTTP response
Explanation:response.status_code
returns the integer status code of the HTTP response (e.g., 200 for success, 404 for not found).
- What is the purpose of the
params
parameter inrequests.get(url, params=parameters)
?- A. To set the request headers
- B. To pass data in the request body
- C. To add query parameters to the URL
- D. To specify the HTTP method
Click to see the answer
Answer: C. To add query parameters to the URL
Explanation: Theparams
parameter in requests.get()
is used to append query parameters to the URL.
- How do you send a JSON payload with a POST request using the
requests
library?- A.
requests.post(url, data=json_data)
- B.
requests.post(url, json=json_data)
- C.
requests.post(url, params=json_data)
- D.
requests.post(url, headers=json_data)
- A.
Click to see the answer
Answer: B. requests.post(url, json=json_data)
json
parameter in the requests.post()
function.
- How do you include custom headers in a request using the
requests
library?
- A.
requests.get(url, data=headers)
- B.
requests.get(url, json=headers)
- C.
requests.get(url, headers=headers)
- D.
requests.get(url, params=headers)
Click to see the answer
Answer: C. requests.get(url, headers=headers)
headers
parameter in the requests.get()
or other request methods.
Sure, here are five additional practice questions about API usage, formatted with interactive answers and explanations. The questions start at number 11 and end at 15.
- How do you send a JSON payload with a POST request using the
requests
library?
- A.
requests.post(url, data=json_data)
- B.
requests.post(url, json=json_data)
- C.
requests.post(url, params=json_data)
- D.
requests.post(url, headers=json_data)
Click to see the answer
Answer: B. requests.post(url, json=json_data)
json
parameter in the requests.post()
function.
- Which HTTP method is typically used to delete a resource on the server?
- A. GET
- B. POST
- C. PUT
- D. DELETE
Click to see the answer
Answer: D. DELETE
Explanation: The HTTP DELETE method is used to request the deletion of a resource identified by a URI.- What is the typical use of the
params
argument inrequests.get()
?
- A. To send data in the body of the request
- B. To set custom headers for the request
- C. To include query string parameters in the URL
- D. To specify the HTTP response format
Click to see the answer
Answer: C. To include query string parameters in the URL
Explanation: Theparams
argument in requests.get()
is used to append query parameters to the URL of the HTTP GET request.
- What will
response.raise_for_status()
do if the HTTP response status code is 404?
- A. Return
None
- B. Raise a
HTTPError
exception - C. Print an error message
- D. Automatically retry the request
Click to see the answer
Answer: B. Raise a HTTPError
exception
response.raise_for_status()
will raise an HTTPError
exception for HTTP responses with error status codes like 404.
- Which function in the
requests
module is used to send an HTTP PUT request?
- A.
requests.put()
- B.
requests.get()
- C.
requests.post()
- D.
requests.delete()
Click to see the answer
Answer: A. requests.put()
requests.put()
function is used to send an HTTP PUT request to the specified URL.
Certainly! Here are practice questions for the specified functions, each followed by an interactive answer section:
- What is the result of the following code using
open()
andread()
?
with open('example.txt', 'r') as file:
= file.read() content
- A. Reads the entire content of ‘example.txt’.
- B. Reads the first line of ‘example.txt’.
- C. Opens ‘example.txt’ in write mode.
- D. Returns an error because the mode is not specified.
Click to see the answer
Answer: A. Reads the entire content of ‘example.txt’.
Explanation: Theread()
method reads the entire content of the file when used with open()
in ‘r’ (read) mode.
- What will the following code using
list.append()
output?
= [1, 2, 3]
myList 4)
myList.append(print(myList)
- A.
[1, 2, 3, 4]
- B.
[4, 1, 2, 3]
- C.
[1, 2, 3]
- D. An error message
Click to see the answer
Answer: A. [1, 2, 3, 4]
append()
method adds an element to the end of the list.
- What does the following code using
dict.get()
return?
= {'a': 1, 'b': 2}
myDict print(myDict.get('c', 'Not Found'))
- A.
1
- B.
2
- C.
None
- D.
'Not Found'
Click to see the answer
Answer: D. 'Not Found'
get()
method returns the value for the key if it exists, otherwise it returns the default value provided, in this case, 'Not Found'
.
- What is the result of the following code using
requests.json()
?
import requests
= requests.get('https://httpbin.org/json')
response = response.json() data
- A. The entire HTTP response as a string.
- B. The status code of the response.
- C. A Python dictionary from the JSON response.
- D. The headers of the response.
Click to see the answer
Answer: C. A Python dictionary from the JSON response.
Explanation: Thejson()
method of the response
object parses the JSON response content and returns it as a Python dictionary.
- What will the following Pandas
DataFrame()
code create?
import pandas as pd
= {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
data = pd.DataFrame(data) df
- A. A DataFrame with two rows and two columns.
- B. A DataFrame with one row and two columns.
- C. A Series object.
- D. An empty DataFrame.
Click to see the answer
Answer: A. A DataFrame with two rows and two columns.
Explanation: TheDataFrame()
function creates a DataFrame object from the dictionary, with each key-value pair becoming a column in the DataFrame, resulting in two rows and two columns.
Certainly! Here are additional practice questions for the specified functions, starting from number 20, along with interactive answers:
- What does the following code using
readlines()
output?
with open('example.txt', 'r') as file:
= file.readlines()
lines print(len(lines))
Assuming ‘example.txt’ has 3 lines of text.
- A. The entire content of ‘example.txt’ as a single string.
- B.
3
- C. A list containing the lines in ‘example.txt’.
- D. The first line of ‘example.txt’ only.
Click to see the answer
Answer: B. 3
readlines()
method returns a list where each element is a line in the file, so len(lines)
will be the number of lines in the file, which is 3 in this case.
- What will the following code using
list.count()
output?
= [1, 2, 2, 3, 3, 3]
myList print(myList.count(3))
- A.
1
- B.
2
- C.
3
- D.
6
Click to see the answer
Answer: C. 3
count()
method returns the number of occurrences of the specified element in the list. There are 3 occurrences of 3
in myList
.
- What does the following code using
dict.values()
return?
= {'a': 1, 'b': 2, 'c': 3}
myDict = myDict.values()
values print(values)
- A.
['a', 'b', 'c']
- B.
[1, 2, 3]
- C.
{'a': 1, 'b': 2, 'c': 3}
- D.
None
Click to see the answer
Answer: B. [1, 2, 3]
values()
method returns a view object that displays a list of all the values in the dictionary.
- What happens when
response.raise_for_status()
is executed if the HTTP response status code is 500?
- A. Returns
None
- B. Raises a
HTTPError
exception - C. Prints the status code
- D. Nothing happens
Click to see the answer
Answer: B. Raises a HTTPError
exception
response.raise_for_status()
will raise an HTTPError
exception for HTTP responses with error status codes like 500.
- What will the following Pandas
to_records()
code produce?
import pandas as pd
= {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
data = pd.DataFrame(data)
df = df.to_records(index=False)
records print(records)
- A. A DataFrame with two rows and two columns.
- B. A list of tuples, each representing a row in the DataFrame.
- C. A NumPy record array representing the DataFrame.
- D. An error, as
to_records
is not a valid method.
Click to see the answer
Answer: C. A NumPy record array representing the DataFrame.
Explanation: Theto_records()
method converts the DataFrame into a NumPy record array. The index=False
parameter excludes the index from the record array.