Exercise: Standard Libraries

Questions for: Standard Libraries

What does the random.choice() function do?
A:
Generates a random integer
B:
Picks a random element from a sequence
C:
Sorts a list in random order
D:
Returns a random floating-point number
Answer: B
The random.choice() function in Python is used to pick a random element from a sequence, such as a list or tuple.
Which function is used to open a file for writing?
A:
open("file.txt", "r")
B:
open("file.txt", "w")
C:
file.write()
D:
file.read()
Answer: B
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")
Which module is commonly used for working with regular expressions?
A:
math
B:
re
C:
os
D:
datetime
Answer: B
The re module in Python is used for working with regular expressions, allowing you to search, match, and manipulate strings based on specified patterns.
What is the purpose of the sys module?
A:
File I/O operations
B:
System-specific parameters and functions
C:
Mathematical operations
D:
Web development
Answer: B
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.
Ad Slot (Above Pagination)
Quiz