Python Programming - Standard Libraries

Exercise : Standard Libraries - General Questions
  • Standard Libraries - General Questions
1.
What is the purpose of the sys module?
File I/O operations
System-specific parameters and functions
Mathematical operations
Web development
Answer: Option
Explanation:
The sys module provides access to some variables used or maintained by the Python interpreter and functions that interact strongly with the interpreter. It is often used for accessing command line arguments, interacting with the Python runtime environment, and more.

2.
Which module is commonly used for working with regular expressions?
math
re
os
datetime
Answer: Option
Explanation:
The re module in Python is used for working with regular expressions, allowing you to search, match, and manipulate strings based on specified patterns.

3.
Which function is used to open a file for writing?
open("file.txt", "r")
open("file.txt", "w")
file.write()
file.read()
Answer: Option
Explanation:
To open a file for writing in Python, you use the open() function with the mode parameter set to "w" (write). For example:
file = open("file.txt", "w")

4.
What does the random.choice() function do?
Generates a random integer
Picks a random element from a sequence
Sorts a list in random order
Returns a random floating-point number
Answer: Option
Explanation:
The random.choice() function in Python is used to pick a random element from a sequence, such as a list or tuple.

5.
What is the purpose of the json.dumps() function?
Reads JSON data from a file
Decodes JSON data
Encodes Python objects into JSON format
Validates JSON syntax
Answer: Option
Explanation:
The json.dumps() function is used to encode Python objects into a JSON-formatted string, allowing you to serialize data for storage or transmission.