Python Programming - Standard Libraries

Exercise : Standard Libraries - General Questions
  • Standard Libraries - General Questions
71.
What is the purpose of the platform module?
Sorting elements in a list
Providing access to platform-specific APIs
Handling exceptions and errors
Working with cryptographic hash functions
Answer: Option
Explanation:
import platform

# Example usage of platform module
platform_name = platform.system()
print(f"Platform: {platform_name}")

72.
How can you use the argparse module in Python to parse command-line arguments?
argparse.parse_args()
argparse.get_args()
argparse.parse()
argparse.ArgumentParser().parse_args()
Answer: Option
Explanation:
import argparse

# Example usage of argparse module for parsing command-line arguments
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator')
args = parser.parse_args()
print(args.integers)

73.
How can you use the shutil module in Python to copy a file from one location to another?
shutil.copy_file(src, dst)
shutil.move_file(src, dst)
shutil.copy(src, dst)
shutil.move(src, dst)
Answer: Option
Explanation:
import shutil

# Example usage of shutil module for copying a file
source_path = 'file.txt'
destination_path = 'copy_of_file.txt'
shutil.copy(source_path, destination_path)

74.
What is the purpose of the email module?
Sorting elements in a list
Handling email messages and MIME attachments
Parsing JSON data
Working with cryptographic hash functions
Answer: Option
Explanation:
import email.message

# Example usage of email module for creating an email message
msg = email.message.EmailMessage()
msg.set_content("Hello, this is a test email.")
msg["Subject"] = "Test Email"
msg["From"] = "sender@example.com"
msg["To"] = "recipient@example.com"

75.
How can you use the sqlite3 module in Python to connect to an SQLite database and execute a query?
sqlite3.connect(database)
sqlite3.query(database, sql)
sqlite3.execute(database, sql)
sqlite3.query(sql)
Answer: Option
Explanation:
import sqlite3

# Example usage of sqlite3 module for connecting to an SQLite database and executing a query
connection = sqlite3.connect('example.db')
cursor = connection.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)')
connection.commit()
connection.close()