Font size
WorksheetsPython Basics Quiz
Total questions: 125
Worksheet time: 1hrs 3mins
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?
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?
{}
[]
()
<>
How do you access the first element of a list my_list = [10, 20, 30]?
my_list[0]
my_list[1]
my_list.first()
my_list[1, 2]
How do you check if a string is empty in Python?
if string == ""
if string.empty()
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?
range(1, 11)
list(1, 10)
range(1, 10)
list(1 to 10)
Which method is used to remove an item from a dictionary in Python?
pop()
remove()
delete()
discard()
What does the continue statement do in a loop?
Skips to the next iteration.
Exits the loop completely.
Repeats the current iteration.
Stops the program.
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(""))
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?
[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?
Checks if two variables point to the same object.
Compares the values of two variables.
Assigns a value to a variable.
Tests for membership in a sequence.
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?
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
What will print(type([])) output?
<class 'list'>
<class 'tuple'>
<class 'set'>
<class 'dict'>
Which function is used to convert a number to a string in Python?
(a)
Which function is used to convert a number to a string in Python?
str()
int()
string()
float()
What is the default starting index for Python lists?
0
1
-1
None
How do you remove an element by its index from a list?
pop(index)
remove(index)
delete(index)
discard(index)
What does the max() function do?
Returns the largest item in an iterable.
Returns the total count of elements.
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?
while
for
continue
if
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?
Splits a string into a list of substrings.
Reverses the string.
Combines multiple strings into one.
Removes spaces from the string.
How do you check the number of elements in a list?
len(list)
size(list)
count(list)
length(list)
Which of the following is a valid Python variable name?
my_var
2ndVar
my-var
my var
Which Python method converts a list into a string with a delimiter?
join()
split()
combine()
merge()
What is the correct way to get the last element of a list?
my_list[-1]
my_list[0]
my_list[last]
my_list[1:]
What does the startswith() method check in a string?
If the string starts with a specified substring.
If the string is uppercase.
If the string contains whitespace.
If the string is alphanumeric.
What will print("a" in "apple") output?
True
False
Error
None
How do you access the value associated with a key in a dictionary?
dict[key]
dict.getKey()
dict.value(key)
dict(key)
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.
Which Python method removes an item by its index from a list?
pop()
remove()
del
clear()
What will the following code output? print(type(10.5))
<class 'float'>
<class 'int'>
<class 'complex'>
<class 'decimal'>
