WorksheetsPython 71-105
Total questions: 35
Worksheet time: 18mins
What is the correct way to define a dictionary in Python?
{'key': 'value'}
{key = value}
['key', 'value']
(key, value)
Which Python keyword is used to create a generator?
yield
return
generate
create
What does list.pop() do?
Removes and returns the last item in a list.
Adds an item to the end of a list.
Removes an item by index.
Clears the list.
Which keyword is used to check for membership in a sequence?
in
of
has
contains
What is the output of print("PYTHON".lower())?
python
PYTHON
Python
None
Which Python method is used to replace parts of a string?
replace()
change()
update()
switch()
What will print(list(range(5))) output?
[0, 1, 2, 3, 4]
[1, 2, 3, 4, 5]
[5, 4, 3, 2, 1]
[0, 1, 2, 3, 4, 5]
Which method removes all items from a list?
clear()
delete()
remove_all()
empty()
How do you add an item to a set in Python?
add()
insert()
append()
extend()
Which function can convert a list into a set?
set()
convert_set()
make_set()
list_to_set()
What is the purpose of the not keyword in Python?
Reverses the result of a boolean expression.
Compares two values.
Assigns a value to a variable.
Ends a loop.
What is the output of print(type([1, 2, 3]))?
<class 'list'>
<class 'tuple'>
<class 'set'>
<class 'dict'>
Which function can you use to iterate over both keys and values of a dictionary?
items()
keys()
values()
iterate()
What will the following code output? print(10 / 4)
2.5
2
Error
3
Which of the following represents a valid Python module?
A .py file containing Python code.
A .txt file containing text.
A .exe file containing executable code.
A .csv file containing comma-separated values.
How do you handle multiple exceptions in a single try block?
Use multiple except clauses.
Use a try block for each exception.
Write all exceptions on one line separated by semicolons.
Use the finally clause.
Which of the following will check if a string starts with a specific prefix?
startswith()
endswith()
checkprefix()
beginwith()
What will print("5" + "5") output?
55
10
Error
5 + 5
What is the correct way to define a multi-line string in Python?
"""This is a multi-line string"""
// This is a multi-line string //
## This is a multi-line string ##
/** This is a multi-line string */
What does the enumerate() function do in Python?
Returns pairs of indices and elements from a sequence.
Sorts a list.
Reverses a list.
Filters elements in a list.
Which keyword is used to define an anonymous function in Python?
lambda
function
def
anonymous
What is the output of print(4 ** 0.5)?
2.0
2
4
16
Which method is used to get all the keys of a dictionary?
keys()
values()
items()
get_keys()
Which data type is unordered in Python?
set
list
tuple
string
What is the default value of a boolean in Python?
False
None
True
0
What will print(bool(0)) output?
False
True
Error
0
What is the correct way to open a file for writing in Python?
open("file.txt", "w")
open("file.txt", "r")
open("file.txt", "a")
write("file.txt")
Which Python keyword is used to exit a function?
return
break
exit
stop
How do you create a new line in a string in Python?
\n
\t
\\
//
What is the output of print(10 // 3)?
3
3.33
10.0
Error
Which Python function can be used to convert an integer to a string?
str()
int()
convert()
string()
Which method is used to count the number of occurrences of a substring in a string?
count()
find()
index()
repeat()
What does the pass keyword do in Python?
Acts as a placeholder and does nothing.
Skips the next iteration of a loop.
Ends the execution of a function.
Raises an exception.
What will the following code output? print(type(10.5))
<class 'float'>
<class 'int'>
<class 'complex'>
<class 'decimal'>
Which Python method removes an item by its index from a list?
pop()
remove()
del
clear()
