Font size
WorksheetsFinal Python Test
Total questions: 103
Worksheet time: 52mins
Which function is used to determine the type of an object in Python?
type()
typeof()
object_type()
classof()
What will the following code output? print(bool(""))
Error
True
None
False
How do you start a Python script file?
By saving it with a .python extension.
By saving it with a .py extension.
By using #start as the first line.
By running python init in the terminal.
What will print([1, 2, 3] + [4, 5]) output?
[1, 2, 3, 4, 5]
12345
[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?
Assigns a value to a variable.
Tests for membership in a sequence
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?
round()
ceil()
floor()
approx()
How do you create an empty set in Python?
set()
{}
[]
()
What is the output of print(4 // 3)?
1
1.3333
0.75
Error
What will print("Python".find("P")) output?
0
-1
None
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?
return
generate
yield
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
What is the primary purpose of a variable in Python?
To store data values.
To execute Python code.
To display output on the screen.
To import external libraries.
What is the correct syntax to display "Hello, World!" in Python?
echo "Hello, World!"
print("Hello, World!")
printf("Hello, World!")
System.out.print("Hello, World!")
Which data type is used to store a sequence of characters in Python?
int
list
str
dict
What is the purpose of the input() function in Python?
To take user input.
To define a variable.
To iterate over a sequence.
To display output.
How do you create a list in Python?
my_list = [1, 2, 3]
my_list = {1, 2, 3}
my_list = (1, 2, 3)
my_list = <1, 2, 3>
Which Python operator is used for addition?
*
-
/
+
What is the correct syntax for a while loop in Python?
while x < 10
while x > 10:
repeat while x < 10
while(x > 10)
Which of the following is used to comment out a line in Python?
#
/*
//
<!--
How do you create a function in Python?
def function_name():
function function_name():
function function_name():
create function function_name:
Which Python method can be used to add an item to the end of a list?
insert()
add()
extend()
append()
What is the output of print(2 ** 3)?
8
23
32
6
Which of the following is the correct way to declare a variable that holds a floating-point number in Python?
x = 5.2
x = float(5.2)
float x = 5.2
float x(5.2)
What is the correct way to start a Python program?
def main()
print("Hello, World!")
begin()
start program
How do you create a comment that spans multiple lines in Python?
// comment //
/* comment */
''' comment '''
# comment
Which data type is used to store multiple values in Python?
Float
Integer
List
String
31. Which function is used to read input from the user in Python?
input()
read()
scan()
get()
32. What is the correct syntax for a for loop in Python?
for x in range(5):
for x = 1 to 5:
for (x in range(5)):
for x -> 5:
33. Which of these is not a valid Python data type?
char
float
complex
int
34. What will the following Python code output: print("Hello" * 3)?
HelloHelloHello
Hello 3
Hello
Hello Hello Hello
35. Which method is used to remove whitespace from the beginning and end of a string in Python?
strip()
trim()
remove()
cut()
36. How do you write a single-line comment in Python?
# comment
// comment
/* comment */
comment
37. What is the result of 10 % 3 in Python?
1
3
30
0
38. How do you handle exceptions in Python?
try-except
catch-throw
catch-finally
throw-except
39. Which of the following is the correct way to define a class in Python?
class MyClass:
define MyClass
MyClass class:
object MyClass:
40. Which statement is used to exit a loop in Python?
break
exit
end
return
41. Which method is used to add an element to the end of a list?
append()
add()
insert()
extend()
42. What does len() function do in Python?
Returns the length of a string or list.
Converts a string to lowercase.
Adds two strings together.
Returns the number of items in a tuple.
43. Which of the following is the correct way to create a tuple?
my_tuple = (1, 2, 3)
my_tuple = [1, 2, 3]
my_tuple = {1, 2, 3}
my_tuple = 1, 2, 3
44. What is the correct way to check if a value is in a list in Python?
if x in list:
if list.contains(x):
if x in list[]:
if list.includes(x):
45. What does the range() function do in Python?
Generates a list of numbers.
Returns a string from a list.
Reverses a list.
Adds two numbers together.
What is the result of the following code: print("abc" + "def")?
abcdef
abc def
ab cdef
None
Which Python method is used to find the index of an element in a list?
index()
position()
find()
locate()
How do you make a string lowercase in Python?
lower()
to_lower()
downcase()
case_lower()
Which of the following Python functions converts a string to a number?
str()
float()
int()
number()
Which keyword is used to define a function in Python?
define
def
function
func
What will print(2 + 3 * 4) output?
12
18
20
14
What is the output of print(2 == 2)?
Error
None
False
True
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]
my_list.first()
my_list[1, 2]
my_list[0]
How do you check if a string is empty in Python?
if len(string) == 0
if string == ""
if string.empty()
if string is ""
What will print("Python" == "python") output?
None
True
False
Error
Which of the following will create a list of numbers from 1 to 10 in Python?
list(1 to 10)
list(1, 10)
range(1, 11)
range(1, 10)
What does the continue statement do in a loop?
Stops the program.
Exits the loop completely.
Repeats the current iteration.
Skips to the next iteration.
Which function is used to determine the type of an object in Python?
object_type()
type()
typeof()
classof()
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?
delete()
empty()
clear()
empty()
How do you add an item to a set in Python?
insert()
add()
append()
extend()
Which function can convert a list into a set?
convert_set()
set()
make_set()
list_to_set()
What is the purpose of the not keyword in Python?
Compares two values
Assigns a value to a variable
Ends a loop
Reverses the result of a boolean expression
What is the output of print(type([1, 2, 3]))?
<class 'tuple'>
<class 'set'>
<class 'dict'>
<class 'list'>
Which function can you use to iterate over both keys and values of a dictionary?
keys()
values()
items()
iterate()
What will the following code output? print(10 / 4)
2
2.5
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 a try block for each exception.
Write all exceptions on one line separated by semicolons
Use the finally clause
Use multiple except clauses
Which of the following will check if a string starts with a specific prefix?
beginwith()
endswith()
startswith()
checkprefix()
What will print("5" + "5") output?
10
55
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?
anonymous
function
lambda
def
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?
values()
keys()
get_keys()
items()
Which data type is unordered in Python?
list
tuple
set
string
What is the default value of a boolean in Python?
True
None
0
False
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", "r")
open("file.txt", "w")
write("file.txt")
open("file.txt", "a")
Which Python keyword is used to exit a function?
exit
break
return
stop
How do you create a new line in a string in Python?
/t
\n
//
\\
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?
int()
convert()
str()
string()
Which method is used to count the number of occurrences of a substring in a string?
find()
index()
count()
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 'int'>
<class 'complex'>
<class 'float'>
<class 'decimal'>
Which Python method removes an item by its index from a list?
del
remove()
clear()
pop()
What will be the output of print(type(5)) in Python?
int
<class 'float'>
float
<class 'int'>
Which Python function is used to return the length of a string or list?
count()
len()
length()
size()
Which of these is an immutable data type in Python?
Dictionary
List
Set
Tuple
Which method would you use to remove an item from a list in Python?
discard()
remove()
delete()
remove()
What does the break statement do in Python?
Skips the current iteration.
Moves to the next loop.
Stops the program entirely.
Exits the current loop.
Which of the following is used to import a Python module?
include module_name
import module_name
use module_name
require module_name
Which of the following is the correct syntax for an if statement in Python?
if x > 5:
if x > 5 {}
if x > 5 then:
if (x > 5)
Which operator is used for comparison in Python?
==
=
===
><
What is the correct way to write an else statement in Python?
elseif:
else:
else do:
else if:
Which method in Python is used to convert a string to uppercase?
toUpperCase()
upper()
uppercase()
capitalize()
Which of the following is the correct syntax for defining a class in Python?
define MyClass:
MyClass class:
class MyClass:
class MyClass():
What does the continue statement do in a loop in Python?
Exits the loop completely.
Skips the rest of the current loop iteration.
Breaks the loop and goes to the next statement.
Restarts the loop from the beginning.
Which of these is a valid way to create a dictionary in Python?
my_dict = ('key1', 'value1')
my_dict = {'key1': 'value1', 'key2': 'value2'}
my_dict = ('key1': 'value1')
my_dict = ['key1', 'value1']
What will be the output of print(5 // 2)?
2.5
3
5
2
