Font size
Worksheetspython
Total questions: 100
Worksheet time: 50mins
Which of the following is a valid variable name in Python?
a)3variable 3
b)variable-3
c)variable_3
d)variable&
What is the output of the following code?
x = 5 y = 3
print(x // y)
a)1
b)1.67
c)2
d)2.0
What does the following code output?
a = 10 b = 20
a, b = b, a
print(a, b)
a)10 10
b)20 10
c)10 20
d)20 20
Which of the following operators is used for exponentiation in Python?
a) ^
b)**
c)%
d)//
What is the output of the following code?
a = 10 b = 4
print(a % b)
a)2
b)0
c)4
d)6
What will the following code output?
x = True y = False
print(x and y)
a)True
b)False
c)1
d)0
Which of the following is a comparison operator in Python?
a) !=
b)//
c)and
d) **
What is the output of the following code?
if 3 > 2: print("Yes")
else: print("No")
a)Yes
b)No
c)Error
d)None of the above
What is the output of the following code?
x = 10 if x > 5:
print("Greater")
elif x == 5:
print("Equal") else: print("Less")
a)Greater
b)Equal
c)Less
d)Error
What is the result of the following expression? 5 2 + 3 * 2
a)19
b)31
c) 25
d)49
Which of the following is not a valid variable name in Python?
a)my_variable
b)_variable
c) 2variable
d) variable2
What will the following code output? x = 10 y = x x = 20 print(y)
a)20
b)10
c)Error
d)None
Which of the following is true about variables in Python?
a) Variables must be declared before they are used
b)Variables do not need to be declared with a type.
c) Variables cannot be changed once initialized
d)Variable names can contain spaces
What is the output of the following code?
a = 5 b = a a = 7
print(a, b)
a)5 5
b) 7 7
c) 5 7
d)7 5.
What happens if you try to use a variable that has not been initialized?
a)The program assigns it a default value.
b)The program gives an error
c)The variable gets the value None.
d)It depends on the Python version
Which of the following statements is correct about variable names in Python?
a)Variable names are caseinsensitive
b)Variable names are case-sensitive
c)Variable names can include spaces.
d)Variable names must begin with a number
What will the following code output?
x = 10 x += 5 print(x)
a)10
b)5
c) 15
d)Error
Which of the following is the correct way to delete a variable in Python?
a) delvariable_name
b)remove variable_name e
c)delete variable_name
d)destroy variable_nam
What will happen if you try to reassign a variable with a different data type in Python?
a)It raises a TypeError.
b) The variable is reassigned with the new value and data type
c)It causes the program to crash.
d)Python ignores the reassignme
Which of the following is an invalid variable name in Python?
a)myVariable
b)my_variable_1 ble
c)my.variable
d)_myVaria
What is the correct syntax for defining a function in Python?
a)function myFunction() {} def
b)ef myFunction(): ()
c)function: myFunction()
d)myFunction
What will be the output of the following code?
def add(a, b=5):
return a + b print(add(3))
a)3
b)5
c)8
d)Error
Which of the following statements is true regarding function arguments in Python?
a)Python does not support default arguments.
b)Functions can only have positional arguments
c)Keyword arguments must follow positional arguments na
d)All arguments must be defined as positio
What does the *args syntax allow you to do?
a)Pass a fixed number of arguments to a function
b)Pass any number of positional arguments to a function.
c)Pass any number of keyword arguments to a function
d)Return multiple values from a function
What will be the output of the following code?
def foo(x, y=[]):
y.append(x) return y print(foo(1))
print(foo(2, [])) print(foo(3))
a)[1], [2], [3]
b) [1], [2], [1, 3]
c)[1], [2], [1, 2, 3]
d)[1], [], [1]
What is the purpose of the return statement in a function?
a)To stop the execution of a program
b)To pass control back to the calling function and optionally return a value.
c)To define a function
d)To call another function
Which of the following is a valid way to call a function in Python?
a)myFunction;
b)myFunction[]
c)myFunction().
d)call myFunction()
What will the following code print?
def test(*args): print(args) test(1, 2, 3)
a)1 2 3
b) (1, 2, 3)
c)[1, 2, 3]
d)None
How do you define a function in Python?
a)function myFunc()
b)def myFunc:
c)def myFunc() :
d)func myFunc():
What is the output of the following code? def my_func(a, b=3):
return a + b print(my_func(2))
a)5
b)2
c)3
d)None
What is the correct way to declare a string in Python?
a)str = 'Hello'
b)str = Hello
c)str = 'Hello"
d)str = Hello World
What will be the output of the following code?
str1 = "Hello"
str2 = "World"
print(str1 + str2)
a)HelloWorld
b)Hello World
c)Hello_World
d)Hello World
How can you find the length of a string s in Python?
a)length(s)
b)len(s)
c)s.len()
d)size(s)
What will be the output of the following code?
s = "Python Programming"
print(s[0:6])
a)Pytho n
b)ython
c)Python
d)ython P
Which of the following methods can be used to convert a string to uppercase in Python?
a)toUpperCase()
b)upper()
c)convert_upper()
d)str_upper()
What will be the output of the following code?
s = "Hello World"
print(s.lower())
a)hello world
b)HELLO WORLD
c)Hello World
d)None
What does the strip() method do in Python?
a)Removes all characters from the string
b)Removes leading and trailing whitespaces
c)Removes leading whitespaces only
d)Removes trailing whitespaces only
Which of the following is a valid string slicing in Python for the string s = "Python"?
a)s[0:3]
b)s[:3]
c)s[1:]
d)All of the above
What will be the output of the following code?
s = "abcde"
print(s[::-1])
a)abcde
b)edcba
c)None
d)edcab
Which of the following statements will result in a string containing a newline character?
a)s = "Hello\nWorld"
b)s = 'Hello/nWorld'
c)s = "HelloWorld"
d)s = 'HelloWorld'
Which of the following is the correct syntax to create a list in Python?
a)list = {1, 2, 3}
b)list = (1, 2, 3)
c)list = [1, 2, 3]
d)list = <1, 2, 3>
What will be the output of the following code?
my_list = [1, 2, 3, 4, 5]
print(my_list[2])
a)2
b)3
c)1
d)4
How can you add an element to the end of a list in Python?
a)my_list.append(item)
b)my_list.add(item)
c)my_list.insert(item)
d)my_list.addElement(item)
What will be the output of the following code?
my_list = [1, 2, 3, 4]
my_list.insert(2, 10)
print(my_list)
a)[1, 10, 2, 3, 4]
b)[1, 2, 10, 3, 4]
c)[10, 1, 2, 3, 4]
d)[1, 2, 3, 4, 10]
Which of the following statements will remove the first occurrence of the value 3 from the list my_list = [1, 2, 3, 4, 3]?
a)my_list.remove(3)
b)my_list.pop(3)
c)del my_list[3]
d)my_list.delete(3)
What will be the output of the following code?
my_list = [1, 2, 3, 4, 5]
print(my_list[-1])
a)1
b)5 c)-1 d)4
Which method is used to combine two lists in Python?
a)combine()
b)append()
c)extend()
d)merge()
What will be the output of the following code?
my_list = [1, 2, 3]
my_list.append([4, 5])
print(my_list)
a)[1, 2, 3, 4, 5]
b)[1, 2, 3, [4, 5]]
c)[1, 2, 3, 5, 4]
d)[1, 2, [3, 4, 5]]
Which of the following will create a list of 5 elements, all initialized to 0?
a)list = [0] 5
b)list = 0 [5]
c)list = 0 5
d)list = [5 0]
What will be the result of the following code?
my_list = [1, 2, 3, 4, 5]
my_list[2:4] = []
print(my_list)
a)[1, 2, 3, 4, 5]
b)[1, 2, 4, 5]
c)[1, 2, 5]
d)[1, 2, 3, 5]
Which of the following is the correct syntax to create a tuple in Python?
a)tuple = [1, 2, 3]
b)tuple = (1, 2, 3)
c)tuple = {1, 2, 3}
d)tuple = <1, 2, 3>
What is the main difference between a list and a tuple in Python?
a)Tuples are mutable, lists are immutable
b)Lists are immutable, tuples are mutable
c)Tuples are immutable, lists are mutable
d)Tuples allow duplicate elements, lists do not
What will be the output of the following code?
my_tuple = (1, 2, 3)
print(my_tuple[1])
a)1
b)2
c)3
d)Error
Can you add an element to a tuple in Python?
a)Yes, using append()
b)Yes, using add()
c)No, tuples are immutable
d)Yes, using insert()
What will be the output of the following code?
my_tuple = (1, 2, 3, 4, 5)
print(my_tuple[-1]
a)1
b)5
c)-1
d)Error
How do you create a tuple with a single element in Python?
a)tuple = (1,)
b)tuple = (1)
c)tuple = [1]
d)tuple = {1}
What will happen if you try to change a value inside a tuple? my_tuple = (1, 2, 3) my_tuple[1] = 5
a)The value will be updated to 5
b)A new tuple will be created
c)Nothing will happen
d)An error will occur
Which of the following methods can be used with tuples?
a)append()
b)extend()
c)insert()
d)count()
What will be the output of the following code?
my_tuple = (1, 2, (3, 4))
print(my_tuple[2][1])
a)1
b)2
c)3
d)4
What will happen if you try to delete a tuple entirely?
a)1 2 3 will be printed
b)It will raise an error
c)The tuple will remain unchanged
d)The tuple will be printed as ()
Which of the following is the correct way to open a file for reading in Python?
a)open('file.txt', 'w')
b)open('file.txt', 'r')
c)open('file.txt', 'a')
d)open('file.txt')
What does the write() method do in Python file handling?
a)Reads data from a file
b)Writes data to a file
c)Appends data to a file
d)Closes the file
What will be the output of the following code if "file.txt" does not exist?
file = open('file.txt', 'r')
a)Creates the file
b)Opens an empty file
c)Raises an error
d)Opens the file in read mode
What is the default mode for opening a file in Python if no mode is specified?
a)Write mode
b)Append mode
c)Read mode
d)Binary mode
What will be the output of the following code?
file = open('file.txt', 'w')
file.write('Hello World')
file.close()
file = open('file.txt', 'r')
print(file.read())
a)None
b)Hello
c)Hello World
d)Error
Which method is used to close a file in Python?
a)file.close()
b)file.stop()
c)file.exit()
d)file.quit()
What is the purpose of the with statement in file handling?
with open('file.txt', 'r') as file:
data = file.read()
a)To handle file errors
b)To automatically close the file after the block is executed
c)To open the file in read mode
d)To write to the file
What will happen if you try to read from a file that is opened in write ('w') mode?
a)It will return an empty string
b)It will append content to the file
c)It will raise an error
d)It will read the file content
What does the readlines() method do in Python?
a)Reads the entire file content as a string
b)Reads one line from the file
c)Reads all lines and returns them as a list
d)Writes multiple lines to a file
What will be the result of the following code?
file = open('file.txt', 'a')
file.write('New data')
file.close()
a)Deletes the existing content and writes 'New data'
b)Appends 'New data' to the existing content
c)Raises an error
d)Opens the file in read mode
What is the correct syntax to handle exceptions in Python?
a)try: except:
b)try: catch:
c)try: throw:
d)try: handle:
What type of error does the following code produce?
try: x = 10 / 0
except ZeroDivisionError: print("Cannot divide by zero")
a)TypeError
b)ZeroDivisionError
c)ValueError
d)No error
Which of the following statements is true about the finally block in Python?
a)It is executed only if no exceptions are raised.
b)It is executed only if an exception is raised.
c)It is executed regardless of whether an exception is raised or not.
d)It is never executed if there is an exception
Which of the following will raise a KeyError exception in Python?
a) Accessing a key that does not exist in a dictionary.
b)Dividing by zero.
c)Accessing an element that does not exist in a list.
d)Referencing an undefined variable.
How do you raise an exception in Python?
a)throw Exception
b)raise Exception
c)generate Exception
d)error Exception
Which exception is raised when a file that doesn't exist is tried to be opened?
a)FileNotFoundError
b)IOError
c)OSError
d)EOFError
What is the output of the following code?
try:
print(1/0)
except ZeroDivisionError:
print("Error: Division by zero")
finally:
print("This will always execute")
a)Error: Division by zero
b)This will always execute
c)Error: Division by zero This will always execute
d)This will always execute Error: Division by zero
Which of the following exceptions is not a built-in Python exception?
a)NameError
b)MemoryError
c)KeyError
d)OutOfMemoryError
What happens if an exception is raised inside an except block?
a)The program will terminate immediately.
b)The exception will be ignored.
c)The program will crash after the
d)The exception will be propagated to the outer
Can the try block have multiple except blocks to handle different types of exceptions?
a)No, only one except block is allowed. .
b) Yes, multiple except blocks can be used.
c)No, the try block can only handle one exception type at a time.
d)Yes, but only two except blocks are allowed
What is a class in Python?
a)A blueprint for creating objects.
b)An instance of an object.
c)A specific value stored in memory.
d)A function that processes data.
What is the purpose of the init method in a class?
a) It is used to delete an object.
b)It initializes an object’s attributes.
c) It is used to inherit methods from another class.
d)It is used to define a class method.
What is encapsulation in Python?
a)A technique of hiding the implementation details from the user.
b)A way to inherit properties from another class.
c)A method to overload operators.
d)A function to handle errors.
What is inheritance in Python?
a)Creating multiple objects from one class.
b)Defining new classes using existing classes as a base.
c)Overloading operators to work with user-defined data types.
d)Hiding the functionality of a class from the user
Which of the following is used to refer to the instance of the class in Python?
a)self
b)cls
c)this
d)object
Which of the following allows a class to have multiple parent classes in Python?
a)Polymorphism
b)Encapsulation
c)Single Inheritance
d)Multiple Inheritance
What is the output of the following code?
class Example:
def init(self, value):
self.value = value
obj1 = Example(10)
obj2 = obj1
obj2.value = 20
print(obj1.value)
a) 10
b)20
c)Error
d)None
What is polymorphism in Python?
a)The ability to define multiple methods with the same name.
b)The ability of different objects to respond in a unique way to the same method call.
c)The ability to hide implementation details.
d)The ability to hide implementation details.
Which of the following statements about method overriding is correct?
a)Method overriding allows a subclass to provide a different implementation for a method that is already defined in its superclass.
b)Method overriding means using multiple methods with the same name but different parameters.
c)Method overriding hides the implementation details of the superclass
d)Method overriding is not allowed in Python
How is a private method defined in a class in Python?
a)By starting its name with a double underscore __.
b) By starting its name with a single underscore _.
c)By writing the keyword private before the method name.
d)Private methods are not allowed in Python.
What is the purpose of the super() function in Python?
a)It allows access to the methods of the parent class.
b)It is used to create a new class.
c)It is used to define a method as static.
d) It is used to access private attributes of a class.
What will be the output of the following code?
class Animal:
def speak(self):
return "Animal speaks"
class Dog(Animal):
def speak(self):
return "Dog barks"
dog = Dog()
print(dog.speak())
a)Animal speaks
b)Dog barks
c) Error
d)None
Which of the following is true about Python constructors?
a)Constructors are used to initialize an object’s state.
b) Python doesn’t support constructors.
c) A constructor must always be defined in every class.
d)Constructors can only take one argument.
Which keyword is used to define a class in Python?
a)function
b)class
c)object
d)def
What is the difference between a class method and a static method in Python?
a) A class method takes cls as the first parameter, while a static method does not take any special first parameter.
b)A class method takes self as the first parameter, while a static method takes cls as the first parameter.
c) A class method can access instance attributes, while a static method cannot.
d)A static method can modify class state, while a class method cannot.
What is inheritance in Python?
a)A technique to define multiple methods with the same name.
b)A mechanism that allows one class to inherit properties and methods from another class.
c)A way to hide data from users.
d)A method to override operator behavior.
What is the correct syntax for defining a subclass that inherits from a superclass in Python?
a)class Subclass Superclass:
b)class Subclass(Superclass):
c)class Subclass inherits Superclass:
d)class Superclass(Subclass):
Which of the following types of inheritance is not supported in Python?
a)Single inheritance
b)Multiple inheritance
c)Multilevel inheritance
d)Hybrid inheritance
What does the super() function do in the context of inheritance?
a)It returns the instance of the
b)It calls the method of the child class. 99. parent class.
c)It allows the child class to call methods from the parent class.
d) It creates a new instance of the child class.
What is the output of the following code?
class A:
def show(self):
print("Class A")
class B(A):
def show(self):
print("Class B")
a)Class A e
b)Class B
c)Error
d)Non
