NEW
Font size
WorksheetsPython final questions part 1
Total questions: 62
Worksheet time: 31mins
What is the primary purpose of a variable in Python?
a) To store data values.
b) To execute Python code.
c) To display output on the screen.
d) To import external libraries.
What is the correct syntax to display "Hello, World!" in Python?
print("Hello, World!")
echo "Hello, World!"
printf("Hello, World!")
System.out.print("Hello, World!")
Which data type is used to store a sequence of characters in Python?
str
int
list
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)
while x < 10
repeat 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():
func function_name():
create function function_name:
Which Python method can be used to add an item to the end of a list?
append()
insert()
add()
extend()
What is the output of print(2 ** 3)?
8
6
23
32
Which of the following is the correct way to declare a variable that holds a floating-point number in Python?
x = 5.2
float x = 5.2
x = float(5.2)
float x(5.2)
What is the correct way to start a Python program?
print("Hello, World!")
def main()
start program
begin()
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?
List
String
Integer
Float
What will be the output of print(type(5)) in Python?
<class 'int'>
<class 'float'>
int
float
Which Python function is used to return the length of a string or list?
len()
size()
count()
length()
Which of these is an immutable data type in Python?
Tuple
List
Dictionary
Set
Which method would you use to remove an item from a list in Python?
remove()
pop()
delete()
discard()
What does the break statement do in Python?
Exits the current loop.
Stops the program entirely.
Skips the current iteration.
Moves to the next loop.
Which of the following is used to import a Python module?
import module_name
require module_name
include module_name
use 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?
else:
else if:
elseif:
else do:
Which method in Python is used to convert a string to uppercase?
upper()
toUpperCase()
capitalize()
uppercase()
Which statement is used to handle exceptions in Python?
try-except
catch
error
throw
Which of the following is the correct syntax for defining a class in Python?
class MyClass:
MyClass class:
define MyClass:
class MyClass():
What does the continue statement do in a loop in Python?
Skips the rest of the current loop iteration.
Exits the loop completely.
Restarts the loop from the beginning.
Breaks the loop and goes to the next statement.
Which of these is a valid way to create a dictionary in Python?
my_dict = {'key1': 'value1', 'key2': 'value2'}
my_dict = ('key1', 'value1')
my_dict = ['key1', 'value1']
my_dict = ('key1': 'value1')
What will be the output of print(5 // 2)?
2
2,5
3
5
Which function is used to read input from the user in Python?
input()
read()
scan()
get()
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:
Which of these is not a valid Python data type?
char
float
complex
int
What will the following Python code output: print("Hello" * 3)?
HelloHelloHello
Hello 3
Hello
Hello Hello Hello
Which method is used to remove whitespace from the beginning and end of a string in Python?
strip()
trim()
remove()
cut()
How do you write a single-line comment in Python?
# comment
// comment
/* comment */
comment
What is the result of 10 % 3 in Python?
1
3
30
0
How do you handle exceptions in Python?
try-except
catch-throw
catch-finally
throw-except
Which of the following is the correct way to define a class in Python?
class MyClass:
define MyClass:
MyClass class:
object MyClass:
Which statement is used to exit a loop in Python?
break
exit
end
return
Which method is used to add an element to the end of a list?
append()
add()
insert()
extend()
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.
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
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):
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?
int()
float()
str()
number()
Which keyword is used to define a function in Python?
def
function
func
define
What will print(2 + 3 * 4) output?
14
20
18
12
What is the output of print(2 == 2)?
True
False
None
Error
Which of the following is used to define a dictionary in Python?
{}
[]
()
<>
Which function is used to sort a list permanently in Python?
sort()
sorted()
order()
arrange()
What is the output of print(bool(0))?
False
True
Error
None
Which of the following is NOT a valid Python loop?
do-while
while
for
nested for
What is the result of the following code? print(5 in [1, 2, 3, 4, 5])
True
False
None
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()
