wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Programming Quiz TrWLH

Total questions: 100

Worksheet time: 1hrs 7mins

Name
Class
Date
1.

What is the correct data type for a whole number in Python?

a)

Integer

b)

Float

c)

String

d)

Boolean

2.

How do you declare a string variable in Python?

a)

var_name = 42

b)

var_name = 3.14

c)

var_name = "Hello"

d)

var_name = True

3.

Which function is used to take user input in Python?

a)

input()

b)

print()

c)

read()

d)

get()

4.

What does the print() function do in Python?

a)

Takes user input

b)

Displays output on the console

c)

Performs mathematical operations

d)

Defines a function

5.

What does the // operator do in Python?

a)

Floor division

b)

Exponentiation

c)

Modulus

d)

Addition

6.

result = 5 + 3 * 2 print(result) What is the output?

a)

16

b)

11

c)

13

d)

26

7.

What is the value of the expression True and False?

a)

True

b)

False

c)

Error

d)

None

8.

Which of the following represents a Boolean value in Python?

a)

"Hello"

b)

42

c)

3.14

d)

True

9.

How is an if statement written in Python?

a)

if condition:

b)

if then:

c)

if (condition)

d)

condition if

10.

What is the purpose of the else statement in Python?

a)

To define a function

b)

To handle exceptions

c)

To execute code when the if condition is False

d)

To perform mathematical operations

11.

What does the break statement do in a loop?

a)

Skips the current iteration and continues with the next one

b)

Immediately terminates the loop and continues with the next statement after the loop

c)

Restarts the loop from the beginning

d)

Pauses the loop execution temporarily

12.

How do you add an element to the end of a list in Python?

a)

list.append()

b)

list.insert()

c)

list.extend()

d)

list.add()

13.

In Python, what does a for loop iterate over?

a)

Indices of a list

b)

Characters in a string

c)

Elements of a sequence

d)

All of the above

14.

What is the main difference between list and tuple?

a)

Lists are mutable; tuples are immutable

b)

Lists are of fixed size; tuples can grow or shrink

c)

Lists can only store integers; tuples can store any data type

d)

Lists can have duplicate elements; tuples cannot

15.

What is the purpose of the return statement in a function?

a)

To print the output of a function

b)

To terminate the function

c)

To send a value back to the caller

d)

To define a variable

16.

How do you call a function in Python?

a)

By using the function's name followed by parentheses ()

b)

By using the function keyword

c)

By using the call() method

d)

By using the run() function

17.

Can you modify the elements of a tuple after it is created?

a)

Yes

b)

No

c)

Only if it has a single element

d)

Only if it is empty

18.

How do you create an empty tuple in Python?

a)

empty_tuple = []

b)

empty_tuple = {}

c)

empty_tuple = ()

d)

empty_tuple = set()

19.

What is the purpose of keys in a dictionary?

a)

To define the data type

b)

To represent the index of a value

c)

To uniquely identify and access values

d)

To sort the dictionary

20.

How do you remove a key-value pair from a dictionary in Python?

a)

remove()

b)

delete()

c)

pop()

d)

discard()

21.

What is the purpose of the try statement in Python?

a)

To raise an exception

b)

To handle exceptions

c)

To define a function

d)

To terminate the program

22.

Which of the following is NOT a built-in exception in Python?

a)

ValueError

b)

TypeError

c)

MyException

d)

IndexError

23.

What is the purpose of code testing and debugging in Python?

a)

To write code faster

b)

To catch and fix errors in your code

c)

To improve code readability

d)

To optimize performance

24.

Which tool is commonly used for code testing and debugging in Python?

a)

IDLE

b)

A code editor

c)

Pytest

d)

All of the above

25.

What is the purpose of a variable in Python?

a)

To perform complex mathematical operations

b)

To store and manage data

c)

To create loops

d)

To display messages to the user

26.

What data type is used to store whole numbers in Python?

a)

Float

b)

String

c)

Integer

d)

Boolean

27.

Which of the following is not a basic arithmetic operator in Python?

a)

-

b)

*

c)

$

d)

%

28.

What are the two Boolean values in Python?

a)

Yes and No

b)

True and False

c)

On and Off

d)

Positive and Negative

29.

In a conditional statement, what keyword is used to execute a block of code if a condition is met?

a)

while

b)

for

c)

then

d)

if

30.

Which loop in Python is used for iterating over a sequence (e.g., a list)?

a)

while loop

b)

do-while loop

c)

for loop

d)

if loop

31.

What is the primary purpose of a Python list?

a)

To store key-value pairs

b)

To group related data

c)

To perform mathematical calculations

d)

To handle exceptions

32.

Which logical operator returns True if both operands are True in Python?

a)

and

b)

or

c)

not

d)

xor

33.

What is a primary use of functions in Python?

a)

To create conditional statements

b)

To store user input

c)

To encapsulate and reuse code

d)

To access dictionary elements

34.

What are exceptions used for in Python?

a)

To enhance program performance

b)

To handle errors and exceptional situations

c)

To create dictionaries

d)

To create loops

35.

Which statement is used to catch and handle exceptions in Python?

a)

if-else

b)

try-except

c)

while

d)

for

36.

How can you read data from a file in Python?

a)

Using the read() function

b)

Using the write() function

c)

Using the open() function

d)

Using the input()

37.

How can you read data from a file in Python?

a)

Using the read() function

b)

Using the write() function

c)

Using the open() function

d)

Using the input() function

38.

What is the correct way to declare a variable in Python?

a)

variable x = 5

b)

x = 5

c)

set x = 5

d)

5 = x

39.

What function is used to display output to the console in Python?

a)

print()

b)

display()

c)

show()

d)

output()

40.

Which of the following is a comparison operator in Python?

a)

&

b)
c)

==

d)

!

41.

In a Python if statement, what is executed if the condition is not met?

a)

The elif block

b)

The else block

c)

The for loop

d)

The try block

42.

Which loop in Python repeats a block of code until a condition is met?

a)

for loop

b)

while loop

c)

if loop

d)

try loop

43.

What logical operator returns True if at least one operand is True in Python?

a)

and

b)

or

c)

not

d)

xor

44.

What is the correct syntax for defining a Python function?

a)

function my_function():

b)

def my_function():

c)

my_function(): def

d)

define my_function():

45.

What is the primary purpose of a Python dictionary?

a)

To store a sequence of items

b)

To calculate mathematical expressions

c)

To store key-value pairs

d)

To create conditional statements

46.

What is the key difference between a tuple and a list in Python?

a)

Tuples are immutable, lists are not

b)

Lists are ordered, tuples are not

c)

Lists can store multiple data types, tuples cannot

d)

Tuples are immutable, lists are not

47.

Which of the following is a bitwise operator in Python?

a)

&

b)

or

c)

not

d)

and

48.

What does the try block in Python exception handling do?

a)

Executes the code that may raise an exception

b)

Skips the execution of the code

c)

Terminates the program

d)

Displays an error message

49.

Which operator is used to access a specific value in a dictionary in Python?

a)

[]

b)

()

c)

{}

d)

<>

50.

How can you write data to a file in Python?

a)

Using the write() function

b)

Using the read() function

c)

Using the input() function

d)

Using the open() function

51.

What is the result of the expression 10 / 2 in Python?

a)

5.0

b)

5

c)

10

d)

0.5

52.

Which of the following is a valid string in Python?

a)

"Hello World"

b)

Hello World

c)

Hello_World

d)

HelloWorld

53.

What is the purpose of the len() function in Python?

a)

To calculate logarithms

b)

To find the length of a string or sequence

c)

To perform mathematical operations

d)

To round floating-point numbers

54.

Which data type is used to store decimal numbers in Python?

a)

String

b)

Float

c)

Integer

d)

List

55.

What is the output of print("Hello" + " " + "World") in Python?

a)

Hello World

b)

HelloWorld

c)

Hello_World

d)

Hello+World

56.

What is the purpose of the int() function in Python?

a)

To round floating-point numbers

b)

To convert a string to an integer

c)

To calculate logarithms

d)

To find the absolute value

57.

Which of the following is a valid Python variable name?

a)

2var

b)

my_variable

c)

variable-name

d)

#var

58.

What does the % operator do in Python?

a)

Exponentiation

b)

Floor division

c)

Modulo (remainder)

d)

Multiplication

59.

Which of the following is a boolean operator in Python?

a)

and

b)

or

c)

not

d)

All of the above

60.

What is the purpose of the print() function in Python?

a)

To get user input

b)

To perform mathematical operations

c)

To round floating-point numbers

d)

To display output to the console

61.

Which of the following is a valid Python data type for whole numbers?

a)

Float

b)

String

c)

Integer

d)

Boolean

62.

How can you add an element to the end of a list in Python?

a)

list.append(element)

b)

list.insert(0, element)

c)

list.add(element)

d)

list.extend(element)

63.

What is the correct way to access the third element in a list named my_list?

a)

my_list(2)

b)

my_list[2]

c)

my_list{2}

d)

my_list.third()

64.

Which of the following is a valid way to remove an element from a list in Python?

a)

list.remove(element)

b)

list.delete(element)

c)

list.pop(element)

d)

list.discard(element)

65.

What is the purpose of the range() function in a for loop?

a)

To generate a sequence of numbers

b)

To perform mathematical calculations

c)

To create conditional statements

d)

To handle exceptions

66.

What is the purpose of the “in” operator in Python?

a)

To perform mathematical calculations

b)

To check membership in a list or sequence

c)

To create conditional statements

d)

To handle exceptions

67.

How can you iterate over both the index and element of a list in a for loop?

a)

for index in my_list:

b)

for element in my_list:

c)

for index, element in enumerate(my_list):

d)

for element, index in my_list:

68.

How can you create a shallow copy of a list in Python?

a)

new_list = list.copy()

b)

new_list = list.clone()

c)

new_list = list[:]

d)

new_list = copy(list)

69.

What is the purpose of the break statement in a loop?

a)

To exit the loop prematurely

b)

To skip the current iteration and continue with the next

c)

To create an infinite loop

70.

What is the purpose of the break statement in a loop?

a)

To exit the loop prematurely

b)

To skip the current iteration and continue with the next

c)

To create an infinite loop

d)

To restart the loop from the beginning

71.

a = 2 b = 1 a = a + b print(a) What is the output?

a)

3

b)

1

c)

2

d)

4

72.

What does super() do in a subclass?

a)

Calls parent class methods

b)

Creates superclasses

c)

Superimposes data

d)

None of the above

73.

What is polymorphism in Python?

a)

Ability of objects to take multiple forms

b)

Multiple classes

c)

Multiple functions

d)

Multiple variables

74.

What is the purpose of init() in a class?

a)

To destroy objects

b)

To initialize object attributes

c)

To define class methods

d)

To inherit from parent

75.

self in a class method refers to:

a)

The class itself

b)

The instance of the class

c)

A global variable

d)

None of the above

76.

Which method sorts a list in ascending order?

a)

order()

b)

sort()

c)

arrange()

d)

reverse()

77.

What is the output of print({1: "one", 2: "two"}[1])?

a)

one

b)

two

c)

1

d)

Error

78.

What is the output of print(max([1, 5, 3]))?

a)

1

b)

3

c)

5

d)

Error

79.

What is the output of print("abc"[::-1])?

a)

abc

b)


cba

c)

bca

d)

Error

80.

Which method removes a key-value pair from a dictionary?

a)

clear()

b)

delete()

c)

remove()

d)

pop()

81.

If `x = 5` and `y = 2`, what is the result of `x % y`?

a)

2.5

b)

2

c)

1

d)

0

82.

What are the three main programming structures?

a)

Structured, object-oriented, procedural

b)

Java, Python, Visual Basic

c)


Sequence, selection, iteration

d)

Machine, assembly, high-level

83.

What is the output of the above program ?

a)

Am I here?

b)

Or here ?

c)

Am I here ? Or here ?

d)

Or here ? Or over here ?

84.

What is the output if the user types "Python"?

a)

Welcome

b)

Try again!

c)

Error

d)

Enter your password

85.

numbers = [5, 14, 9, 17]

for number in numbers:

if number % 3 == 0:

print(number)

The output will be?

a)

17

b)

9

c)

14

9

17

d)

14

17

86.
a)

for i in range(1, 5):   
print(str(i) * 5)

b)

for i in range(1, 5):   
print("i" * i)

c)

for i in range(1, 6):   
print("i" * i)

d)

for i in range(1, 6):   
print(str(i) * i)

87.

How many times does the following program print the word ‘computer’:

word= "computer"  
for num in range(1,6):  
    print(word)

a)

5

b)

6

c)

1

d)

0

88.

Which of the following programs prints ten lines?

a)

for i in range(10):

print("hi")

b)

for i = 1 to 10:

print("hi")

c)

for i in 1 - 10:

print("hi")

d)

for i from 0 to 9:

print("hi")

89.

Which of the following for loops would print the following numbers?

3

5

7

9

a)

for i in range(3,9):

print(i)

b)

for i in range(9):

print(i)

c)

for i in range(3, 9, 2):

print(i)

d)

for i in range(3, 10, 2):

print(i)

90.

What is the value of num when this loop completes?

num = 0

for i in range(2, 8, 2):

num += i

a)

2

b)

8

c)

12

d)

20

91.

drink_choices = ["coffee", "tea", "water", "juice", "soda"]

for drink in drink_choices:

print(drink)

a)

"coffee"

b)

["coffee", "tea", "water", "juice", "soda"]

c)

drink

d)


"coffee tea water juice soda"

92.

What would be the output of the following code:

for i in range(3):

print(5)

a)

0 1 2

b)

3 3 3 3 3

c)

5 5 5

d)

0 1 2 3 4

93.

numbers = [2, 4, 6, 8]

for number in numbers:

print("hello!")

a)

2 hello! 4 hello! 6 hello! 8 hello!

b)

hello!

c)

2 4 6 8

d)

hello! hello! hello! hello!

94.

How can you concatenate two lists in Python?

a)

list1.add(list2)

b)

list1.extend(list2)

c)

list1 + list2

d)

concat(list1, list2)

95.

a=[2,4,6,8] a[1]=3 print(a) What is the output?

a)

[3,4,6,8]

b)

[2,3,6,8]

c)

[3,2,4,6,8]

d)

3,4,6,8

96.

Which of the following create an empty dictionary (D)?

a)

D = {}

b)

D = []

c)

D = ()

d)

D=" "

97.

d = {'foo': 100, 'bar': 200, 'baz': 300}

What method call will delete the entry whose value is 200?

a)

delete d('bar')

b)


d.pop("bar")

c)

d.remove("bar")

d)

None of the above

98.

Please select all correct ways to empty the following dictionary

student = {

"name": "Emma",

"class": 9,

"marks": 75

a)

del student

b)

del student[0:2]

c)

student.clear()

d)

student[0:2]

99.

Which of the following will give error?

Suppose dict1={"a":1,"b":2,"c":3}

a)

print(len(dict1))

b)

print(dict1.get("b"))

c)

dict1["a"]=5

d)


None of these.

100.

Python is a general purpose programming language created by

a)

Dennis M. Ritchie

b)

Guido Van Rossum

c)

James Gosling

d)

Bjarne Stroustrup