Font size
WorksheetsPython Quiz
Total questions: 27
Worksheet time: 14mins
What will be the output of the following code? x = 5 y = "Hello" z = 3.0 print(type(x), type(y), type(z))
<class 'int'> <class 'string'> <class 'float'>
<class 'int'> <class 'str'> <class 'float'>
<class 'int'> <class 'string'> <class 'complex'>
<class 'float'> <class 'int'> <class 'str'>
Which of the following is the correct syntax to declare a function in Python?
def function_name:
def function_name[]:
def function_name():
define function_name():
What will be the result of the following operation? result = 7 // 2 print(result)
3.5
3
4
2.5
Which of the following is not a valid variable name in Python?
_myvar
my_var2
2varname
myVar
What will be the output of the following code? my_list = [1, 2, 3, 4, 5] my_list.append([6, 7]) print(my_list)
[1, 2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, [6, 7]]
[1, 2, 3, 4, 5, 6, [7]]
[1, 2, 3, 4, 5, 6, 7, [8]]
What is the output of the following code? my_list = [10, 20, 30, 40, 50] print(my_list[1:4])
[20, 30, 40]
[10, 20, 30, 40]
[30, 40, 50]
[20, 30, 40, 50]
Which method is used to add multiple elements to a list in Python?
append()
extend()
insert()
add()
What will be the output of the following code? my_dict = {'a': 1, 'b': 2, 'c': 3} print(my_dict['b'])
1
2
3
'b'
Which of the following is the correct way to delete an element from a dictionary?
del my_dict[key]
my_dict.remove(key)
my_dict.pop(key)
my_dict.clear()
What happens if you try to access a key that doesn't exist in a dictionary?
Returns None
Throws a KeyError
Adds the key with a value 0
Creates a new key-value pair
Which of the following will create a set in Python?
my_set = ()
my_set = {}
my_set = {1, 2, 3}
my_set = (1,2,3)
What will be the output of the following code? my_set = {1, 2, 3, 4, 5} my_set.add(6) print(my_set)
{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5, 6}
[1, 2, 3, 4, 5, 6]
{6, 5, 4, 3, 2, 1}
Which of the following is a valid tuple declaration?
my_tuple = (1,)
my_tuple = [1]
my_tuple = {1,}
my_tuple = (1)
What will be the output of the following code? class MyClass: def __init__(self, name): self.name = name obj = MyClass("John") print(obj.name)
John
name
None
Error
Which of the following statements is true about inheritance in Python?
A class can inherit only from one superclass.
A class can inherit from multiple classes.
Python does not support inheritance.
Classes cannot have methods in Python.
Which of the following describes polymorphism in Python?
Using the same method name in different classes.
Creating multiple objects from the same class.
Overriding methods from the parent class.
Inheriting from multiple classes.
What is method overriding in Python?
When two methods have the same name but different signatures.
When a subclass defines a method with the same name as a method in its parent class.
When a method in the same class is written twice.
Defining a method without parameters.
What will be the output of the following code?
[0, 1, 4] [3, 2, 1, 0, 1, 4] [0, 1, 4]
[0, 1, 4] [3, 2, 1, 0, 1, 4] [0, 1]
[0, 1, 4] [3, 2, 1, 0, 1, 4] [0, 1, 4]
[0, 1, 4] [0, 1] [0, 1]
What will be the result of the following code? a = [1, 2, 3] b = [1, 2, 3] print(a == b) print(a is b)
True True
True False
False True
False False
What is the output of the following code? def gfg(x=[]): x.append(1) return x print(gfg()) print(gfg())
[1] [1, 1]
[1] [1]
[1, 1] [1, 1]
[] []
What will be the output of the following code? class A: def __init__(self): self.__var = 10 a = A() print(a.__var)
10
AttributeError
None
SyntaxError
What will the following code print? class Test: def __init__(self): self.value = 1 def some_func(obj): obj.value += 10 test = Test() some_func(test) print(test.value)
1
10
11
Error
Which of the following decorators can be used to make a method of a class belong to the class rather than an instance of it?
@classmethod
@staticmethod
@abstractmethod
@property
What is the purpose of the global keyword in Python?
To define a variable inside a function.
To declare that a variable inside a function refers to a global variable.
To restrict access to a variable outside of a function.
To initialize a static variable.
x = [1, 2, 3]
y = x
y.append(4)
print(x)
print(y)
[1, 2, 3], [1, 2, 3, 4]
[1, 2, 3, 4], [1, 2, 3]
[1, 2, 3, 4], [1, 2, 3, 4]
Error: Cannot append to a list
What is the result of the following code?
class MyClass:
x = 10
a = MyClass()
b = MyClass()
a.x = 20
print(b.x)
10
20
Error
None
my_tuple = (1, 2, 3)
my_tuple[1] = 4
print(my_tuple)
(1, 4, 3)
(1, 2, 3)
TypeError: 'tuple' object does not support item assignment
[1, 4, 3]
