NEW
Font size
WorksheetsPython Programming Quiz
Total questions: 58
Worksheet time: 1hrs 27mins
Which of the following is the correct syntax for defining a function in Python?
function my_function():
def my_function:
def my_function():
function: my_function()
How do you create a new list with elements from another list, doubled?
[x * 2 for x in my_list]
[2 * x for x in my_list]
[2 + x for x in my_list]
list([2 * x for x in my_list])
Which of the following is the correct syntax to open a file for reading in Python?
open('file.txt', 'r')
file.open('file.txt', 'read')
open.read('file.txt')
open('file.txt')
How would you write a lambda function to return the square of a number x?
lambda x: x * x
lambda x: x^2
square(x) = x * x
def square(x): return x * x
Which of the following is a correct way to catch an exception in Python?
try except Exception as e:
try: except Exception:
try: raise Exception
try: catch:
What is the correct syntax for importing the math module in Python?
import math
from math import *
include math
use math
Which statement is used to end a loop or function in Python?
break
exit
end
return
What is the correct way to define a list in Python?
list = [1, 2, 3]
list(1, 2, 3)
list = (1, 2, 3)
list = {1, 2, 3}
What is the result of the following code? x = [1, 2, 3] x[1] = 10 print(x)
[1, 10, 3]
[1, 2, 10]
10
[1, 2, 3, 10]
Which of the following is the correct syntax for accessing the third item in a list my_list?
my_list[2]
my_list(3)
my_list[3]
my_list[1]
How do you add a new key-value pair to a dictionary my_dict?
my_dict['key'] = value
my_dict.add('key', value)
my_dict.insert('key', value)
my_dict.append('key', value)
Which of the following is the correct syntax to define a generator function in Python?
def my_generator(): yield x
def my_generator(x): return x
def my_generator(): return x
yield def my_generator(): x
Which of the following is correct to read the entire contents of a file in Python?
file.read()
file.get()
file.readline()
file.load()
How can you add an element to the end of a list my_list in Python?
my_list.append(x)
my_list.insert(x)
my_list.add(x)
my_list.push(x)
What does the following list comprehension return? [x for x in range(5) if x % 2 == 0]
[0, 1, 2, 3, 4]
[0, 2, 4]
[1, 3]
[2, 4]
How would you define an empty dictionary in Python?
my_dict = {}
my_dict = []
my_dict = ()
my_dict = set()
Which of the following methods will add an element to the set my_set in Python?
my_set.add()
my_set.insert()
my_set.append()
my_set.put()
Which of the following methods is used to write a list of data to a CSV file?
csv.write()
csv.writer()
csv.writelines()
csv.output()
How do you iterate over a dictionary in Python and get both the key and value?
for key, value in dict:
for key, value in dict.items():
for key in dict:
for value in dict.values():
Which of the following is correct for updating an existing value of a dictionary key?
dict[key] = new_value
dict.update(key, new_value)
dict.insert(key, new_value)
dict.add(key, new_value)
How can you split a string into a list of words in Python?
string.split(' ')
string.split(',')
split(string)
string.splitlines()
Which function can be used to apply a condition on each item of an iterable?
map()
filter()
reduce()
apply()
What is the correct syntax for creating a Python set from a list my_list?
set(my_list)
list(my_list)
set[]
my_list.set()
How do you define a function with a default argument in Python?
def func(a=1):
def func(a, default=1):
def func(default=1, a):
def func(a=1, b):
How can you convert a string s to uppercase in Python?
s.upper()
s.toUpperCase()
upper(s)
s.uppercase()
What does a list comprehension in Python do?
It creates a new list by applying a given expression to each element of an existing list.
It filters out elements from a list.
It sorts a list.
It converts a list to a tuple.
Which of the following is the correct syntax for a lambda function in Python?
lambda x, y: x + y
function lambda x, y: x + y
def lambda(x, y): return x + y
lambda x, y = x + y
What is a decorator in Python?
A way to define default values for function arguments.
A function that adds functionality to an existing function without modifying it.
A special type of class method.
A built-in function for formatting output.
What does the yield keyword do in Python?
It terminates a function.
It creates a generator, which allows for lazy evaluation of data.
It defines a generator expression.
It raises an exception in a function.
Which of the following is used to handle errors in Python?
throw
catch
try-except
error-handler
Which method is used to read a CSV file using Python's built-in CSV module?
csv.read()
csv.load()
csv.reader()
csv.open()
How do you debug Python code?
Use assert statements.
Use a print statement.
Use the pdb module or other debugging tools.
Python doesn't need debugging.
How would you store a list of dictionaries representing CSV data in Python?
In a tuple.
In a list.
In a set.
In an array.
Which of the following is a method for parsing data from a CSV file?
open()
readlines()
csv.reader()
parse()
What is the purpose of strip() when handling CSV data?
It removes empty rows from the CSV file.
It removes leading and trailing whitespace characters from a string.
It splits a string into a list.
It concatenates strings.
How can you avoid duplicated entries when working with CSV data in Python?
By using a dictionary.
By using a list comprehension.
By using a set.
By using a tuple.
Which Python data structure is ideal for storing immutable data from a CSV file?
List
Dictionary
Tuple
Set
How do you read a CSV file into a Pandas DataFrame in Python?
pd.read_csv()
csv.reader()
open('file.csv')
pd.open_csv()
What is the use of re.findall() when working with CSV data?
To find the column headers.
To parse a CSV file.
To perform pattern matching using regular expressions.
To clean empty rows from a CSV file.
What is a benefit of using NumPy arrays over lists for numerical computations?
NumPy arrays can store mixed data types.
NumPy arrays use less memory and provide efficient element-wise operations.
NumPy arrays are mutable.
NumPy arrays are faster for string manipulations.
Which Python library would you use to perform advanced mathematical operations on CSV data?
csv
pandas
NumPy
re
What method in Python would you use to append a row to a CSV file?
csv.append()
csv.writer()
csv.append_row()
csv.writerow()
Which data structure in Python allows you to map keys to values, ideal for indexing CSV data?
List
Set
Dictionary
Tuple
How do you ensure no duplicate values when adding items to a collection of CSV data?
By using a tuple.
By using a list.
By using a set.
By using a dictionary.
What would be the outcome of applying the map() function to a list in Python?
It filters out values from the list.
It applies a function to each element in the list.
It sorts the list.
It reverses the list.
Which Python method is commonly used for cleaning strings in CSV data?
strip()
split()
lower()
join()
What would you use a dictionary for when working with CSV data?
To store data in an immutable format.
To transform data into key-value pairs for easy access.
To perform numerical analysis on the data.
To filter out invalid data.
How would you find the average of numeric data in a list using Python?
sum(list)/len(list)
len(list)/sum(list)
max(list) - min(list)
average(list)
What is the primary purpose of using regular expressions (regex) with CSV data?
To format the CSV file.
To find patterns or extract specific parts of the data.
To transform data into JSON format.
To sort the data based on certain conditions.
How do you create a Python module?
By defining a class with the name module.
By creating a file with a .py extension and writing Python code in it.
By using the import statement.
By calling create_module() function.
Which Python package is best suited for data manipulation and analysis?
NumPy
pandas
matplotlib
re
How do you install an external Python package?
python install package_name
pip install package_name
import package_name
download package_name
What is the purpose of __init__.py in a Python package?
To initialize the module.
To mark the directory as a Python package.
To create an executable script.
To define global variables.
Which function is used to get the list of all modules in a package in Python?
dir()
modules()
list_modules()
import_modules()
What is the advantage of using packages in Python?
Packages reduce code complexity by grouping related modules.
Packages allow you to skip the import process.
Packages increase runtime efficiency.
Packages can store data in CSV format.
What does the zip() function do in Python?
Combines multiple lists into one list.
Compresses data.
Combines multiple iterables element-wise.
Sorts lists.
Which of the following methods can be used to handle missing values in CSV data?
dropna()
isnull()
fillna()
All of the above
How would you check if a value exists in a dictionary when working with CSV data?
key in dict
dict.has_key()
dict.contains()
dict.exists()
