NEW
Font size
WorksheetsPython Final
Total questions: 74
Worksheet time: 37mins
What is the correct data type for a whole number in Python?
A) Integer
B) Float
C) String
D) Boolean
How do you declare a string variable in Python?
A) var_name = 42
B) var_name = 3.14
C) var_name = "Hello"
var_name = True
Which function is used to take user input in Python?
A) input()
B) print()
C) read()
D) get()
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
What does the // operator do in Python?
A) Floor division
B) Exponentiation
C) Modulus
D) Addition
result = 5 + 3 * 2
print(result)
A) 16
B) 11
C) 13
D) 26
What is the value of the expression True and False?
A) True
B) False
C) Error
D) None
Which of the following represents a Boolean value in Python?
A) "Hello"
B) 42
C) 3.14
D) True
How is an if statement written in Python?
A) if condition:
B) if then:
C) if (condition)
D) condition if
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
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?
A) list.append()
B) list.insert()
C) list.extend()
D) list.add()
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
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
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
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
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
How do you create an empty tuple in Python?
A) empty_tuple = []
B) empty_tuple = {}
C) empty_tuple = ()
D) empty_tuple = set()
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
How do you remove a key-value pair from a dictionary in Python?
A) remove()
B) delete()
C) pop()
D) discard()
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
Which of the following is NOT a built-in exception in Python?
A) ValueError
B) TypeError
C) MyException
D) IndexError
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
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
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
What data type is used to store whole numbers in Python?
A) Float
B) String
C) Integer
D) Boolean
Which of the following is not a basic arithmetic operator in Python?
A) -
B) *
C)$
D) %
What are the two Boolean values in Python?
A) Yes and No
B) True and False
C) On and Off
D) Positive and Negative Correct
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
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 Correct
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 Correct
Which logical operator returns True if both operands are True in Python?
A) and
B) or
C) not
D) xor
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
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 Correct
Which statement is used to catch and handle exceptions in Python?
A) if-else
B) try-except
C) while
D) for
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 Correct
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
What function is used to display output to the console in Python?
A) print()
B) display()
C) show()
D) output()
Which of the following is a comparison operator in Python?
A) &
B) |
C) ==
D) !
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
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
What logical operator returns True if at least one operand is True in Python?
A) and
B) or
C) not
D) xor
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():
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
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
Which of the following is a bitwise operator in Python?
A) &
B) or
C) not
D) and
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
Which operator is used to access a specific value in a dictionary in Python?
A) []
B) ()
C) {}
D) <>
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
What is the result of the expression 10 / 2 in Python?
A) 5.0
B) 5
C) 10
D) 0.5
Which of the following is a valid string in Python?
A) "Hello World"
B) Hello World
C) Hello_World
D) HelloWorld
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
Which data type is used to store decimal numbers in Python?
A) String
B) Float
C) Integer
D) List
What is the output of print("Hello" + " " + "World") in Python?
A) Hello World
B) HelloWorld
C) Hello_World
D) Hello+World
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
Which of the following is a valid Python variable name?
A) 2var
B) my_variable
C) variable-name
D) #var
What does the % operator do in Python?
A) Exponentiation
B) Floor division
C) Modulo (remainder)
D) Multiplication
Which of the following is a boolean operator in Python?
A) and
B) or
C) not
D) All of the above
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
Which of the following is a valid Python data type for whole numbers?
A) Float
B) String
C) Integer
D) Boolean
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
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)
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()
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)
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
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
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:
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)
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
What is the key difference between the and and or logical operators?
A) and returns True if both operands are True; or returns True if at least one operand is True.
B) and returns True if at least one operand is True; or returns True if both operands are True.
C) and is used for bitwise operations; or is used for logical operations.
D) and is a unary operator; or is a binary operator.
Which bitwise operator is used for flipping the bits of a binary number in Python?
A) &
B) |
C) ~
D) ^
What is the purpose of the else clause in a for loop?
A) To create an infinite loop
B) To execute a block of code if the loop is interrupted by a break statement
C) To execute a block of code if the loop completes without interruption
D) To skip the current iteration and continue with the next
How can you concatenate two lists in Python?
A) list1.add(list2)
B) list1.extend(list2)
C) list1 + list2
D) concat(list1, list2)
What is the result of the expression 5 & 3 in Python?
A) 7
B) 1
C) 3
D) 0
