Font size
WorksheetsPython Programming Quiz TrWLH
Total questions: 100
Worksheet time: 1hrs 7mins
What is the correct data type for a whole number in Python?
Integer
Float
String
Boolean
How do you declare a string variable in Python?
var_name = 42
var_name = 3.14
var_name = "Hello"
var_name = True
Which function is used to take user input in Python?
input()
print()
read()
get()
What does the print() function do in Python?
Takes user input
Displays output on the console
Performs mathematical operations
Defines a function
What does the // operator do in Python?
Floor division
Exponentiation
Modulus
Addition
result = 5 + 3 * 2 print(result) What is the output?
16
11
13
26
What is the value of the expression True and False?
True
False
Error
None
Which of the following represents a Boolean value in Python?
"Hello"
42
3.14
True
How is an if statement written in Python?
if condition:
if then:
if (condition)
condition if
What is the purpose of the else statement in Python?
To define a function
To handle exceptions
To execute code when the if condition is False
To perform mathematical operations
What does the break statement do in a loop?
Skips the current iteration and continues with the next one
Immediately terminates the loop and continues with the next statement after the loop
Restarts the loop from the beginning
Pauses the loop execution temporarily
How do you add an element to the end of a list in Python?
list.append()
list.insert()
list.extend()
list.add()
In Python, what does a for loop iterate over?
Indices of a list
Characters in a string
Elements of a sequence
All of the above
What is the main difference between list and tuple?
Lists are mutable; tuples are immutable
Lists are of fixed size; tuples can grow or shrink
Lists can only store integers; tuples can store any data type
Lists can have duplicate elements; tuples cannot
What is the purpose of the return statement in a function?
To print the output of a function
To terminate the function
To send a value back to the caller
To define a variable
How do you call a function in Python?
By using the function's name followed by parentheses ()
By using the function keyword
By using the call() method
By using the run() function
Can you modify the elements of a tuple after it is created?
Yes
No
Only if it has a single element
Only if it is empty
How do you create an empty tuple in Python?
empty_tuple = []
empty_tuple = {}
empty_tuple = ()
empty_tuple = set()
What is the purpose of keys in a dictionary?
To define the data type
To represent the index of a value
To uniquely identify and access values
To sort the dictionary
How do you remove a key-value pair from a dictionary in Python?
remove()
delete()
pop()
discard()
What is the purpose of the try statement in Python?
To raise an exception
To handle exceptions
To define a function
To terminate the program
Which of the following is NOT a built-in exception in Python?
ValueError
TypeError
MyException
IndexError
What is the purpose of code testing and debugging in Python?
To write code faster
To catch and fix errors in your code
To improve code readability
To optimize performance
Which tool is commonly used for code testing and debugging in Python?
IDLE
A code editor
Pytest
All of the above
What is the purpose of a variable in Python?
To perform complex mathematical operations
To store and manage data
To create loops
To display messages to the user
What data type is used to store whole numbers in Python?
Float
String
Integer
Boolean
Which of the following is not a basic arithmetic operator in Python?
-
*
$
%
What are the two Boolean values in Python?
Yes and No
True and False
On and Off
Positive and Negative
In a conditional statement, what keyword is used to execute a block of code if a condition is met?
while
for
then
if
Which loop in Python is used for iterating over a sequence (e.g., a list)?
while loop
do-while loop
for loop
if loop
What is the primary purpose of a Python list?
To store key-value pairs
To group related data
To perform mathematical calculations
To handle exceptions
Which logical operator returns True if both operands are True in Python?
and
or
not
xor
What is a primary use of functions in Python?
To create conditional statements
To store user input
To encapsulate and reuse code
To access dictionary elements
What are exceptions used for in Python?
To enhance program performance
To handle errors and exceptional situations
To create dictionaries
To create loops
Which statement is used to catch and handle exceptions in Python?
if-else
try-except
while
for
How can you read data from a file in Python?
Using the read() function
Using the write() function
Using the open() function
Using the input()
How can you read data from a file in Python?
Using the read() function
Using the write() function
Using the open() function
Using the input() function
What is the correct way to declare a variable in Python?
variable x = 5
x = 5
set x = 5
5 = x
What function is used to display output to the console in Python?
print()
display()
show()
output()
Which of the following is a comparison operator in Python?
&
==
!
In a Python if statement, what is executed if the condition is not met?
The elif block
The else block
The for loop
The try block
Which loop in Python repeats a block of code until a condition is met?
for loop
while loop
if loop
try loop
What logical operator returns True if at least one operand is True in Python?
and
or
not
xor
What is the correct syntax for defining a Python function?
function my_function():
def my_function():
my_function(): def
define my_function():
What is the primary purpose of a Python dictionary?
To store a sequence of items
To calculate mathematical expressions
To store key-value pairs
To create conditional statements
What is the key difference between a tuple and a list in Python?
Tuples are immutable, lists are not
Lists are ordered, tuples are not
Lists can store multiple data types, tuples cannot
Tuples are immutable, lists are not
Which of the following is a bitwise operator in Python?
&
or
not
and
What does the try block in Python exception handling do?
Executes the code that may raise an exception
Skips the execution of the code
Terminates the program
Displays an error message
Which operator is used to access a specific value in a dictionary in Python?
[]
()
{}
<>
How can you write data to a file in Python?
Using the write() function
Using the read() function
Using the input() function
Using the open() function
What is the result of the expression 10 / 2 in Python?
5.0
5
10
0.5
Which of the following is a valid string in Python?
"Hello World"
Hello World
Hello_World
HelloWorld
What is the purpose of the len() function in Python?
To calculate logarithms
To find the length of a string or sequence
To perform mathematical operations
To round floating-point numbers
Which data type is used to store decimal numbers in Python?
String
Float
Integer
List
What is the output of print("Hello" + " " + "World") in Python?
Hello World
HelloWorld
Hello_World
Hello+World
What is the purpose of the int() function in Python?
To round floating-point numbers
To convert a string to an integer
To calculate logarithms
To find the absolute value
Which of the following is a valid Python variable name?
2var
my_variable
variable-name
#var
What does the % operator do in Python?
Exponentiation
Floor division
Modulo (remainder)
Multiplication
Which of the following is a boolean operator in Python?
and
or
not
All of the above
What is the purpose of the print() function in Python?
To get user input
To perform mathematical operations
To round floating-point numbers
To display output to the console
Which of the following is a valid Python data type for whole numbers?
Float
String
Integer
Boolean
How can you add an element to the end of a list in Python?
list.append(element)
list.insert(0, element)
list.add(element)
list.extend(element)
What is the correct way to access the third element in a list named my_list?
my_list(2)
my_list[2]
my_list{2}
my_list.third()
Which of the following is a valid way to remove an element from a list in Python?
list.remove(element)
list.delete(element)
list.pop(element)
list.discard(element)
What is the purpose of the range() function in a for loop?
To generate a sequence of numbers
To perform mathematical calculations
To create conditional statements
To handle exceptions
What is the purpose of the “in” operator in Python?
To perform mathematical calculations
To check membership in a list or sequence
To create conditional statements
To handle exceptions
How can you iterate over both the index and element of a list in a for loop?
for index in my_list:
for element in my_list:
for index, element in enumerate(my_list):
for element, index in my_list:
How can you create a shallow copy of a list in Python?
new_list = list.copy()
new_list = list.clone()
new_list = list[:]
new_list = copy(list)
What is the purpose of the break statement in a loop?
To exit the loop prematurely
To skip the current iteration and continue with the next
To create an infinite loop
What is the purpose of the break statement in a loop?
To exit the loop prematurely
To skip the current iteration and continue with the next
To create an infinite loop
To restart the loop from the beginning
a = 2 b = 1 a = a + b print(a) What is the output?
3
1
2
4
What does super() do in a subclass?
Calls parent class methods
Creates superclasses
Superimposes data
None of the above
What is polymorphism in Python?
Ability of objects to take multiple forms
Multiple classes
Multiple functions
Multiple variables
What is the purpose of init() in a class?
To destroy objects
To initialize object attributes
To define class methods
To inherit from parent
self in a class method refers to:
The class itself
The instance of the class
A global variable
None of the above
Which method sorts a list in ascending order?
order()
sort()
arrange()
reverse()
What is the output of print({1: "one", 2: "two"}[1])?
one
two
1
Error
What is the output of print(max([1, 5, 3]))?
1
3
5
Error
What is the output of print("abc"[::-1])?
abc
cba
bca
Error
Which method removes a key-value pair from a dictionary?
clear()
delete()
remove()
pop()
If `x = 5` and `y = 2`, what is the result of `x % y`?
2.5
2
1
0
What are the three main programming structures?
Structured, object-oriented, procedural
Java, Python, Visual Basic
Sequence, selection, iteration
Machine, assembly, high-level
What is the output of the above program ?
Am I here?
Or here ?
Am I here ? Or here ?
Or here ? Or over here ?
What is the output if the user types "Python"?
Welcome
Try again!
Error
Enter your password
numbers = [5, 14, 9, 17]
for number in numbers:
if number % 3 == 0:
print(number)
The output will be?
17
9
14
9
17
14
17
for i in range(1, 5):
print(str(i) * 5)
for i in range(1, 5):
print("i" * i)
for i in range(1, 6):
print("i" * i)
for i in range(1, 6):
print(str(i) * i)
How many times does the following program print the word ‘computer’:
word= "computer"
for num in range(1,6):
print(word)
5
6
1
0
Which of the following programs prints ten lines?
for i in range(10):
print("hi")
for i = 1 to 10:
print("hi")
for i in 1 - 10:
print("hi")
for i from 0 to 9:
print("hi")
Which of the following for loops would print the following numbers?
3
5
7
9
for i in range(3,9):
print(i)
for i in range(9):
print(i)
for i in range(3, 9, 2):
print(i)
for i in range(3, 10, 2):
print(i)
What is the value of num when this loop completes?
num = 0
for i in range(2, 8, 2):
num += i
2
8
12
20
drink_choices = ["coffee", "tea", "water", "juice", "soda"]
for drink in drink_choices:
print(drink)
"coffee"
["coffee", "tea", "water", "juice", "soda"]
drink
"coffee tea water juice soda"
What would be the output of the following code:
for i in range(3):
print(5)
0 1 2
3 3 3 3 3
5 5 5
0 1 2 3 4
numbers = [2, 4, 6, 8]
for number in numbers:
print("hello!")
2 hello! 4 hello! 6 hello! 8 hello!
hello!
2 4 6 8
hello! hello! hello! hello!
How can you concatenate two lists in Python?
list1.add(list2)
list1.extend(list2)
list1 + list2
concat(list1, list2)
a=[2,4,6,8] a[1]=3 print(a) What is the output?
[3,4,6,8]
[2,3,6,8]
[3,2,4,6,8]
3,4,6,8
Which of the following create an empty dictionary (D)?
D = {}
D = []
D = ()
D=" "
d = {'foo': 100, 'bar': 200, 'baz': 300}
What method call will delete the entry whose value is 200?
delete d('bar')
d.pop("bar")
d.remove("bar")
None of the above
Please select all correct ways to empty the following dictionary
student = {
"name": "Emma",
"class": 9,
"marks": 75
del student
del student[0:2]
student.clear()
student[0:2]
Which of the following will give error?
Suppose dict1={"a":1,"b":2,"c":3}
print(len(dict1))
print(dict1.get("b"))
dict1["a"]=5
None of these.
Python is a general purpose programming language created by
Dennis M. Ritchie
Guido Van Rossum
James Gosling
Bjarne Stroustrup
