NEW
Font size
WorksheetsPython
Total questions: 57
Worksheet time: 29mins
Which of the following is used to define a dictionary in Python?
{}
[]
()
<>
How do you access the first element of a list my_list = [10, 20, 30]?
my_list[1, 2]
my_list.first()
my_list[1]
my_list[0]
How do you check if a string is empty in Python?
if string.empty()
if string == ""
if string is ""
if len(string) == 0
What will print("Python" == "python") output?
False
True
None
Error
Which of the following will create a list of numbers from 1 to 10 in Python?
list(1, 10)
range(1, 10)
range(1, 11)
list(1 to 10)
Which method is used to remove an item from a dictionary in Python?
remove()
pop()
delete()
discard()
What does the continue statement do in a loop?
Exits the loop completely.
Repeats the current iteration.
Stops the program.
Skips to the next iteration.
Which function is used to determine the type of an object in Python?
typeof()
object_type()
type()
classof()
What will the following code output? print(bool(""))
False
True
None
Error
How do you start a Python script file?
By saving it with a .py extension.
By saving it with a .python extension.
By using #start as the first line.
By running python init in the terminal.
What will print([1, 2, 3] + [4, 5]) output?
12345
[1, 2, 3, 4, 5]
[5, 4, 3, 2, 1]
[1, 2, 3][4, 5]
Which operator is used to repeat a string multiple times in Python?
!
&
+
*
What does the is operator do in Python?
Tests for membership in a sequence.
Assigns a value to a variable.
Compares the values of two variables.
Checks if two variables point to the same object.
Which function is used to round a number in Python?
ceil()
round()
floor()
approx()
How do you create an empty set in Python?
()
[]
set()
{}
What is the output of print(4 // 3)?
1.3333
1
0.75
Error
What will print("Python".find("P")) output?
None
-1
0
Error
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?
generate
generate
return
yield
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?
of
in
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?
[1, 2, 3, 4, 5]
[5, 4, 3, 2, 1]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5]
Which method removes all items from a list?
empty()
remove_all()
delete()
clear()
How do you add an item to a set in Python?
insert()
add()
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?
Ends a loop.
Compares two values.
Assigns a value to a variable.
Reverses the result of a boolean expression.
What is the output of print(type([1, 2, 3]))?
<class 'tuple'>
<class 'list'>
<class 'set'>
<class 'dict'>
Which function can you use to iterate over both keys and values of a dictionary?
keys()
items()
values()
iterate()
What will the following code output? print(10 / 4)
Error
3
2
2.5
Which of the following represents a valid Python module?
A .txt file containing text.
A .exe file containing executable code.
A .py file containing Python code.
A .csv file containing comma-separated values.
How do you handle multiple exceptions in a single try block?
Use the finally clause.
Use multiple except clauses.
Use a try block for each exception.
Write all exceptions on one line separated by semicolons.
Which of the following will check if a string starts with a specific prefix?
startswith()
endswith()
checkprefix()
beginwith()
What will print("5" + "5") output?
Error
5+5
10
55
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?
Sorts a list.
Reverses a list.
Filters elements in a list.
Returns pairs of indices and elements from a sequence.
Which keyword is used to define an anonymous function in Python?
def
anonymous
lambda
function
What is the output of print(4 ** 0.5)?
2
2.0
4
16
Which method is used to get all the keys of a dictionary?
get_keys()
keys()
values()
items()
Which data type is unordered in Python?
set
list
tuple
string
What is the default value of a boolean in Python?
0
True
None
False
What will print(bool(0)) output?
0
Error
True
False
What is the correct way to open a file for writing in Python?
open("file.txt", "r")
open("file.txt", "w")
open("file.txt", "a")
write("file.txt")
Which Python keyword is used to exit a function?
break
exit
return
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
What will print(type([])) output?
<class 'list'>
<class 'tuple'>
<class 'dict'>
<class 'set'>
Which function is used to convert a number to a string in Python?
int()
str()
string()
float()
What is the default starting index for Python lists?
1
0
-1
None
How do you remove an element by its index from a list?
remove(index)
delete(index)
discard(index)
pop(index)
What does the max() function do?
Returns the total count of elements.
Returns the largest item in an iterable.
Converts numbers to the largest integer.
Removes the largest value.
Which keyword is used to create a loop that continues as long as a condition is true?
for
if
while
continue
What will print("hello".capitalize()) output?
HELLO
hello
Hello
None
What is the result of print(4 < 2 or 3 > 1)?
True
False
None
Error
What does the split() method do?
Reverses the string.
Removes spaces from the string.
Combines multiple strings into one.
Splits a string into a list of substrings.
How do you check the number of elements in a list?
len(list)
size(list)
count(list)
length(list)
