MTE Question Bank Python K21
MTE Question Bank Python K21
x = "2023"
y = int(x)
print(y+3)
a. 2026
b. "2026"
c. 20233
d. Error
Answer: a. 2026
a. variable_name
b. VariableName
c. variable-name
d. Variable_name
Answer: c. variable-name
x = [1,2,3,4,5]
print(x[2:4])
a. [3,4]
b. [2,3,4]
c. [4,5]
d. [1,2,3,4]
Answer: a. [3,4]
Answer: a. 1 3 5
a. 5
b. 2
c. 3
d. None
Answer: a. 5
x = [1, 2, 3, 4, 5]
for i in x:
if i == 3:
break
print(i)
a. 1 2
b. 1 2 3
c. 1 2 3 4
d. 1 2 3 4 5
Answer: a. 1 2
a. Error
b. Enter a number:
c. The input value entered by the user
d. None
a. Error
b. The sum of the two numbers entered by the user
c. Enter two numbers:
d. None
x = [1, 2, 3]
x[1] = 4
print(x)
a. [1,2,3]
b. [1,4,3]
c. [4,2,3]
d. [1,4,6]
Answer: b. [1,4,3]
try:
x = int("abc")
except ValueError:
print("ValueError Occured")
a. Error
b. ValueError Occured
c. 0
d. None
x = [1, 2, 3, 4, 5]
print(x[-1])
a. 5
b. 1
c. 0
d. -1
Answer: a. 5
x = [1, 2, 3, 4, 5]
x.append(6)
print(x)
a. [1, 2, 3, 4, 5, 6]
b. [1, 2, 3, 4, 6]
c. [1, 2, 3, 4, 5]
d. [6, 1, 2, 3, 4, 5]
Answer: a. [1, 2, 3, 4, 5, 6]
a. 2
b. 0
c. Error
d. None
Answer: b. 0
x = [1, 2, 3, 4, 5]
for i in x:
if i == 3:
continue
print(i)
else:
print("End of loop")
a. 1 2 4 5 End of loop
b. 1 2 End of loop
c. 1 2 4 5
d. 1 2 4 5 Error
x = [1, 2, 3, 4, 5]
for i in x:
if i == 3:
break
else:
print("End of loop")
a. 1 2 End of loop
b. 1 2
c. End of loop
d. 1 2 Error
x = [1, 2, 3, 4, 5]
y = type(x)
print(y)
a. list
b. <class 'list'>
c. str
d. int
a. HELLO
b. hello
c. Hi
d. None
Answer: a. HELLO
import os
x = os.getcwd()
print(x)
try:
x = int("abc")
except:
print("An error occured")
a. An error occured
b. 0
c. Error
d. None
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
x = Car("Toyota", "Camry", 2022)
print(x.make)
a. Toyota
b. Camry
c. 2022
d. Error
Answer: a. Toyota
x = [1, 2, 3, 4, 5]
y=x
y[2] = 6
print(x)
a. [1, 2, 6, 4, 5]
b. [1, 2, 3, 4, 5]
c. Error
d. [1, 2, 4, 5]
Answer: a. [1, 2, 6, 4, 5]
x = (1, 2, 3, 4, 5)
y = type(x)
print(y)
a. tuple
b. <class 'tuple'>
c. list
d. dict
x = [1, 2, 3, 4, 5]
print(x[1:4])
a. [1, 2, 3, 4]
b. [2, 3, 4]
c. [1, 4, 5]
d. [2, 3]
Answer: b. [2, 3, 4]
x = [1, 2, 3, 4, 5]
for i in x:
print(i)
else:
print("End of loop")
a. 1 2 3 4 5
b. 1 2 3 4 5 End of loop
c. End of loop
d. Error
a. 9
b. 7
c. 5
d. Error
Answer: a. 9
x = "Hello, World!"
print(x.split(","))
x = [1, 2, 3, 4, 5]
y=x
y = [6, 7, 8, 9, 10]
print(x)
a. [1, 2, 3, 4, 5]
b. [6, 7, 8, 9, 10]
c. Error
d. [1, 7, 8, 9, 10]
Answer: a. [1, 2, 3, 4, 5]
try:
x = int("123")
except:
print("An error occured")
else:
print(x)]
a. 123
b. An error occured
c. Error
d. None
Answer: a. 123
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
class ElectricCar(Car):
def __init__(self, make, model, year, battery_size):
super().__init__(make, model, year)
self.battery_size = battery_size
a. Error
b. "Tesla"
c. "Model S"
d. 100
Answer: b. "Tesla"
x = [1, 2, 3, 4, 5]
y = x[::-1]
print(y)
a. [1, 2, 3, 4, 5]
b. [5, 4, 3, 2, 1]
c. [5, 1, 4, 2, 3]
d. [1, 5, 2, 4, 3]
Answer: b. [5, 4, 3, 2, 1]
def f(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return f(n-1) + f(n-2)
print(f(6))
a. 1
b. 8
c. 13
d. 21
Answer: c. 13
def check_val(val):
return 'Hello' if val == 0 else 'Goodbye'
print(check_val(0))
a. Hello
b. Goodbye
c. 0
d. Error
Answer: a. Hello
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in range(len(x)):
x[i] = x[i]**2
print(x)
a. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b. [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
c. [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
d. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100]
def sum_list(lst):
total = 0
for num in lst:
total += num
return total
print(sum_list([1, 2, 3, 4, 5]))
a. 15
b. 10
c. 25
d. 20
Answer: a. 15
35. What will be the output of the following code?
def remove_duplicates(lst):
new_list = []
for num in lst:
if num not in new_list:
new_list.append(num)
return new_list
print(remove_duplicates([1, 2, 3, 2, 4, 5, 3, 6]))
a. [1, 2, 3, 4, 5, 6]
b. [1, 2, 3, 4, 5, 6, 7, 8, 9]
c. [1, 2, 3, 4, 5, 6, 2, 3]
d. [1, 3, 2, 4, 5, 6]
Answer: a. [1, 2, 3, 4, 5, 6]
36. Given the following code, select the correct code to raise an error if the
denominator is zero.
a. except ZeroDivisionError:
b. except:
c. except TypeError:
d. except ValueError
a. 200
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: func() takes 2 positional arguments but 3 were given
b. 200
200
c. Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: func() takes 2 positional arguments but 3 were given
200
d. Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: func() takes 2 positional arguments but 3 were given
File Open
File Read
File Close
a.
file = open("file.txt", "r")
print("File Open")
print(file.read())
print("File Read")
file.close()
print("File Close")
b.
file = open("file.txt", "r")
print("File Open")
file.read()
print("File Read")
file.close()
print("File Close")
c.
file = open("file.txt")
print("File Open")
print(file.read())
print("File Read")
file.close()
print("File Close")
d.
file = open("file.txt", "w")
print("File Open")
print(file.read())
print("File Read")
file.close()
print("File Close")
a. 5.0
Error
5.0
b. 5.0
Error
None
c. 5.0
None
Error
d. None
Error
None
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 30)
p2 = Person("Jane", 25)
print(p1.age)
print(p2.name)
a. 30
Jane
b. John
25
c. John
Jane
d. 30
25
10
20
30
a.
l = [10, 20, 30]
for i in l:
print(i)
b.
for i in 10, 20, 30:
print(i)
c.
for i in range(10, 31, 10):
print(i)
42. Which of the following statements will result in a TypeError when run in a Python
environment?
a. x = [2, 3, 4, 5]
x[2] = x[2] + 5
b. y = (2, 3, 4, 5)
y[2] = y[2] + 5
c. z = {2, 3, 4, 5}
z.add(z.pop() + 5)
Answer: b. y = (2, 3, 4, 5)
y[2] = y[2] + 5
a. Hello, John
Additional arguments: ('How are you?', "It's good to see you.", 'Have a nice day!')
b. Hello, John
Additional arguments: "How are you?" "It's good to see you." 'Have a nice day!'
c. Hello, John
Additional arguments: "How are you?",'It's good to see you.','Have a nice day!'
d. Hello, John
Additional arguments: "How are you?" "It's good to see you." "Have a nice day!"
a. 30
b. 28
c. 32
d. 24
Answer: a. 30
a. num1: 10
num2: 20
args: (30, 40, 50)
kwargs: {'x': 60, 'y': 70, 'z': 80}
b. num1: 10
num2: 20
args: 30
kwargs: {40: 50, 'x': 60, 'y': 70, 'z': 80}
c. num1: 10
num2: 20
args: (30, 40, 50)
kwargs: 60
d. num1: 10
num2: 20
args: 30
kwargs: 40
Answer: a. num1: 10
num2: 20
args: (30, 40, 50)
kwargs: {'x': 60, 'y': 70, 'z': 80}
try:
a = [1, 2, 3]
print(a[5])
except IndexError as e:
print("IndexError:", e)
except Exception as e:
print("Exception:", e)
finally:
print("Finally block")
class Shape:
def __init__(self, l, b):
self.length = l
self.breadth = b
def area(self):
return self.length * self.breadth
class Square(Shape):
def __init__(self, side):
super().__init__(side, side)
sq = Square(5)
print(sq.area())
a. 20
b. 25
c. 30
d. None of the above
Answer: b. 25
def func(x):
x=x+1
print(x)
a=5
func(a)
print(a)
a. 6 5
b. 6 6
c. 5 6
d. 5 5
Answer: a. 6 5
lst = [1, 2, 3, 4, 5]
print(lst[2:5][::-1])
a. [5, 4, 3]
b. [3, 4, 5]
c. [5, 4]
d. [4, 3, 2]
Answer: a. [5, 4, 3]
f = open("file.txt", "w")
f.write("Hello World")
f.close()
f = open("file.txt", "r")
print(f.read())
f.close()
a. Hello World
b. None
c. Error
d. Empty String
def func(x):
if isinstance(x, int):
return "integer"
elif isinstance(x, str):
return "string"
else:
return "unknown"
print(func(5))
print(func("hello"))
print(func([1, 2, 3]))
class Student:
def __init__(self, name, marks):
self.name = name
self.marks = marks
def __str__(self):
return f"{self.name} - {self.marks}"
s = Student("John", 90)
print(s)
a. John - 90
b. <main.Student object at 0x7f9e40c9e9e8>
c. Error
d. None of the above
Answer: a. John - 90
54. What would be the output of the following code snippet:
def func():
try:
a = 10 / 0
except ZeroDivisionError as e:
print("Zero Division Error")
func()
a. Zero Division Error
b. Error
c. None
d. Exception ZeroDivisionError
class Bank:
def __init__(self, balance):
self.balance = balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if self.balance >= amount:
self.balance -= amount
else:
print("Insufficient balance")
b = Bank(1000)
b.withdraw(500)
b.withdraw(500)
b.withdraw(500)
print(b.balance)
a. 1000
b. 500
c. 0
d. Error
Answer: c. 0
class Car:
def __init__(self, brand, model, price):
self.brand = brand
self.model = model
self.price = price
def __repr__(self):
return f"Brand: {self.brand} Model: {self.model} Price: {self.price}"
cars = [
Car("Toyota", "Camry", 35000),
Car("Honda", "City", 30000),
Car("Hyundai", "Elantra", 32000)
]
print(cars)
a. [Brand: Toyota Model: Camry Price: 35000, Brand: Honda Model: City Price: 30000, Brand:
Hyundai Model: Elantra Price: 32000]
b. [Toyota, Honda, Hyundai]
c. Error
d. None of the above
Answer: a. [Brand: Toyota Model: Camry Price: 35000, Brand: Honda Model: City Price: 30000,
Brand: Hyundai Model: Elantra Price: 32000]
def func(x):
def inner_func(y):
return x + y
return inner_func
add5 = func(5)
print(add5(10))
a. 15
b. 5
c. 10
d. None of the above
Answer: a. 15
Nums = [3, 5, 7, 8, 9]
temp = [[x for x in Nums] for x in range(3)]
print (temp)
Answer: (b)
a) [0, 2, 4, 6]
b) [0, 2, 4]
c) [0, 1, 2, 3, 4, 5, 6]
d) Runtime error
Answer: (a)
T1 = (1)
T2 = (3, 4)
T1 += 5
print(T1)
print(T1 + T2)
a) TypeError
b) (1, 5, 3, 4)
c) 1 TypeError
d) 6 TypeError
Answer: (d)
i) num = 12345
S = str(num)
print(int(S[::-1]))
Answer: c)
62. How will you extract 'love' from the string, S = 'I love Python'?
i) S[2:5]
ii) S[2:6]
iii) S[3:7]
iv) S[-11:-7]
v) S[-11:-8]
a) i) and ii)
b) ii) and iii)
c) i) and iv)
d) all of the above
Answer: c)
63. What will be the ‘comprehension equivalent’ for the following code snippet?
b)
[‘Raj’, 22]
[‘Simran’, 21]
[‘Rahul’, 40]
c)
1 [‘Raj’, 22]
2 [‘Simran’, 21]
3 [‘Rahul’, 40]
d)
‘Raj’
‘Simran’
‘Rahul’
def update(x):
x = [1, 2, 3, 4]
l = [5, 6, 7, 8]
update(l)
print(l)
A. [1, 2, 3, 4]
B. [5, 6, 7, 8]
C. Error
D. None of the above
Answer: B
66. Which of the following function can be used to check the type of an object in
Python?
A. typeof()
B. type()
C. classof()
D. class()
Answer: B
def user_input():
x = input("Enter a number: ")
return int(x) * 2
result = user_input()
print(result)
A. Error
B. None of the above
C. Depends on the input
D. The output will be the double of the entered number.
Answer: D
l = [1, 2, 3, 4, 5]
print(l[-2:])
A. [3, 4, 5]
B. [4, 5]
C. [2, 3, 4, 5]
D. [3, 4]
Answer: B
A. 2, 2, 15, 1.67
B. 8, 2, 15, 1.67
C. 8, 2, 15, 1.6
D. 8, 2, 15, 5/3
Answer: A
try:
a = 5/0
except ZeroDivisionError as e:
print("Error: ", e)
Answer: A
x = [1, 2, 3, 4, 5]
for i in x[::-1]:
print(i)
A. 5 4 3 2 1
B. 1 2 3 4 5
C. 5 3 1
D. None of the above
Answer: A
73. Which of the following is an in-built function in Python to read the contents of a
file?
A. readfile()
B. openfile()
C. read()
D. fileread()
Answer: C
a = [1, 2, 3, 4, 5]
b = a[:2]
b[0] = 10
print(a)
a. [1, 2, 3, 4, 5]
b. [10, 2, 3, 4, 5]
c. [10, 2]
d. [1, 2, 10, 4, 5]
Answer: b. [10, 2, 3, 4, 5]
a = [1, 2, 3, 4, 5]
b = a.pop()
print(a, b)
a. [1, 2, 3, 4] 5
b. [1, 2, 3, 4, 5] 5
c. [1, 2, 3, 4, 5] None
d. [1, 2, 3, 4] None
Answer: a. [1, 2, 3, 4] 5
a = [1, 2, 3, 4, 5]
b = a.remove(3)
print(a, b)
a. [1, 2, 4, 5] 3
b. [1, 2, 4, 5] None
c. [1, 2, 3, 4, 5] None
d. [1, 2, 3, 4, 5] 3
a = [1, 2, 3, 4, 5]
b = a.count(3)
print(b)
a. 0
b. 1
c. 2
d. 3
Answer: b. 1
a = [1, 2, 3, 4, 5]
b = a.extend([6, 7, 8])
print(a, b)
a. [1, 2, 3, 4, 5, 6, 7, 8] None
b. [1, 2, 3, 4, 5] [6, 7, 8]
c. [1, 2, 3, 4, 5, 6, 7, 8] [6, 7, 8]
d. [1, 2, 3, 4, 5] None
a = [1, 2, 3, 4, 5]
b = a.index(3)
print(b)
a. 0
b. 1
c. 2
d. 3
Answer: d. 3
Ans: a
numbers = (1, 2, 3)
numbers[0] = 10
a. (1, 2, 3)
b. (10, 2, 3)
c. TypeError: 'tuple' object does not support item assignment
d. None of the above
Ans: c
a. tuple[-1]
b. tuple[len(tuple) - 1]
c. tuple[len(tuple)]
d. None of the above
Ans: a
a. (1, 2, 3, 4, 5, 6)
b. [1, 2, 3, 4, 5, 6]
c. [1, 2, 3][4, 5, 6]
d. (1, 2, 3, 4, 5, 6)
Ans: a
t = (1, 2, 3)
t.pop()
a. (1, 2)
b. (2, 3)
c. AttributeError: 'tuple' object has no attribute 'pop'
d. None of the above
Ans: c
t = (1, 2, 3)
t[len(t):] = [4, 5, 6]
a. (1, 2, 3, 4, 5, 6)
b. (1, 2, 3)
c. TypeError: 'tuple' object does not support item assignment
d. None of the above
Ans: c
t = (1, 2, 3)
t = t + (4, 5, 6)
a. (1, 2, 3, 4, 5, 6)
b. (1, 2, 3)
c. TypeError: 'tuple' object does not support item assignment
d. None of the above
Ans: a
a. elem in tuple
b. tuple.index(elem)
c. elem not in tuple
d. None of the above
Ans: a
t = (1, 2, 3)
print(len(t))
a. 0
b. 3
c. [1, 2, 3]
d. None of the above
Ans: b
90. What is the correct way to update the value of key 'key1' to 'new_value1' in the
following dictionary?
a. d.update(key1 = 'new_value1')
b. d['key1'] = 'new_value1'
c. d[key1] = 'new_value1'
d. None of the above
a. value1
b. value2
c. Key not found
d. None of the above
a. 1
b. 2
c. 3
d. None of the above
Answer: b. 2
a. {'key1': 'value1'}
b. {'key2': 'value2'}
c. {'key1': 'value1', 'key2': 'value2'}
d. None of the above
a. 10 9 8 7 6 5 4 3 2 1
b. 9 8 7 6 5 4 3 2 1
c. 10 9 8 7 6 5 4 3 2
d. 9 8 7 6 5 4 3 2 1 0
Answer: a. 10 9 8 7 6 5 4 3 2 1
a. A new file named "file.txt" will be created and the text "Hello World" will be written to the file
b. A new file named "file.txt" will be created but nothing will be written to the file
c. The code will produce an error because the file does not exist
d. The code will overwrite an existing file named "file.txt" with the text "Hello World"
Answer: a. A new file named "file.txt" will be created and the text "Hello World" will be written to
the file
Answer: a. The code will print ["Hello World\n", "How are you"]