0% found this document useful (0 votes)
111 views36 pages

MTE Question Bank Python K21

The document contains 41 multiple choice questions about Python programming concepts such as variables, data types, operators, control flow, functions, classes, exceptions, files and more. Each question has 4 answer options and identifies the single correct answer. The questions test a range of core Python skills and knowledge.

Uploaded by

R Akhil Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views36 pages

MTE Question Bank Python K21

The document contains 41 multiple choice questions about Python programming concepts such as variables, data types, operators, control flow, functions, classes, exceptions, files and more. Each question has 4 answer options and identifies the single correct answer. The questions test a range of core Python skills and knowledge.

Uploaded by

R Akhil Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

1. What will be the output of the following code?

x = "2023"
y = int(x)
print(y+3)

a. 2026
b. "2026"
c. 20233
d. Error

Answer: a. 2026

2. Which of the following is not a valid naming convention in Python?

a. variable_name
b. VariableName
c. variable-name
d. Variable_name

Answer: c. variable-name

3. What will be the output of the following code?

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]

4. What will be the output of the following code?

for i in range(0, 6):


if i%2 == 0:
continue
print(i)
a. 1 3 5
b. 2 4
c. 1 3 5 None
d. None 1 3 5

Answer: a. 1 3 5

5. What will be the output of the following code?

def add_numbers(a, b):


return a + b
x = add_numbers(2, 3)
print(x)

a. 5
b. 2
c. 3
d. None

Answer: a. 5

6. What will be the output of the following code?

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

7. What will be the output of the following code?

x = input("Enter a number: ")


print(x)

a. Error
b. Enter a number:
c. The input value entered by the user
d. None

Answer: c. The input value entered by the user

8. What will be the output of the following code?

x, y = map(int, input("Enter two numbers: ").split())


print(x+y)

a. Error
b. The sum of the two numbers entered by the user
c. Enter two numbers:
d. None

Answer: b. The sum of the two numbers entered by the user

9. What will be the output of the following code?

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]

10. What will be the output of the following code?

try:
x = int("abc")
except ValueError:
print("ValueError Occured")

a. Error
b. ValueError Occured
c. 0
d. None

Answer: b. ValueError Occured


11. What will be the output of the following code?

x = [1, 2, 3, 4, 5]
print(x[-1])

a. 5
b. 1
c. 0
d. -1

Answer: a. 5

12. What will be the output of the following code?

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]

13. What will be the output of the following code?

def add_numbers(a, b=0):


return a + b
x = add_numbers(2)
print(x)

a. 2
b. 0
c. Error
d. None

Answer: b. 0

14. What will be the output of the following code?

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

Answer: a. 1 2 4 5 End of loop

15. What will be the output of the following code?

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

Answer: c. End of loop

16. What will be the output of the following code?

x = [1, 2, 3, 4, 5]
y = type(x)
print(y)

a. list
b. <class 'list'>
c. str
d. int

Answer: b. <class 'list'>

17. What will be the output of the following code?


x = "hello"
y = x.upper()
print(y)

a. HELLO
b. hello
c. Hi
d. None

Answer: a. HELLO

18. What will be the output of the following code?

import os
x = os.getcwd()
print(x)

a. The current working directory


b. Error
c. None
d. /

Answer: a. The current working directory

19. What will be the output of the following code?

try:
x = int("abc")
except:
print("An error occured")

a. An error occured
b. 0
c. Error
d. None

Answer: a. An error occured

20. What will be the output of the following code?

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

21. What will be the output of the following code?

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]

22. What will be the output of the following code?

x = (1, 2, 3, 4, 5)
y = type(x)
print(y)

a. tuple
b. <class 'tuple'>
c. list
d. dict

Answer: b. <class 'tuple'>

23. What will be the output of the following code?

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]

24. What will be the output of the following code?

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

Answer: b. 1 2 3 4 5 End of loop

25. What will be the output of the following code?

def add_numbers(a, b, c=0):


return a + b + c
x = add_numbers(2, 3, 4)
print(x)

a. 9
b. 7
c. 5
d. Error

Answer: a. 9

26. What will be the output of the following code?

x = "Hello, World!"
print(x.split(","))

a. ['Hello', ' World!']


b. ['Hello, World']
c. Error
d. ['Hello']

Answer: a. ['Hello', ' World!']

27. What will be the output of the following code?

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]

28. What will be the output of the following code?

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

29. What will be the output of the following code?

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

my_tesla = ElectricCar("Tesla", "Model S", 2021, 100)


print(my_tesla.make)

a. Error
b. "Tesla"
c. "Model S"
d. 100

Answer: b. "Tesla"

30. What will be the output of the following code?

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]

31. What will be the output of the following code?

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

32. What will be the output of the following code?

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

33. What will be the output of the following code?

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]

Answer: b. [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

34. What will be the output of the following code?

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.

def divide(a, b):


try:
return a / b
except:
return 'Error: Denominator cannot be zero.'
print(divide(10, 5))

a. except ZeroDivisionError:
b. except:
c. except TypeError:
d. except ValueError

37. What will be the output of the following code?

def func(a, b):


return a * b
print(func(10, 20))
print(func(10, 20, 30))

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

38. What is the correct code for the following output?

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")

39. What will be the output of the following code?

def div(a, b):


try:
return a/b
except:
print("Error")
print(div(10, 2))
print(div(10, 0))

a. 5.0
Error
5.0

b. 5.0
Error
None

c. 5.0
None
Error

d. None
Error
None

40. What will be the output of the following code?

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

41. What is the correct code for the following output?

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)

d. w = {'a': 2, 'b': 3, 'c': 4, 'd': 5}


w['c'] = w['c'] + 5

Answer: b. y = (2, 3, 4, 5)
y[2] = y[2] + 5

43. What will be the output of the following code snippet:

def greetings(name, *args):


print("Hello, ", name)
print("Additional arguments: ", args)
greetings("John", "How are you?","It's good to see you.", "Have a nice day!")

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!"

Answer: a. Hello, John


Additional arguments: ('How are you?', "It's good to see you.", 'Have a nice day!')

44. What will be the output of the following code snippet:

def add_numbers(a, b=0, *args, **kwargs):


result = a + b
for num in args:
result += num
for key, value in kwargs.items():
result += value
return result
print(add_numbers(1, 2, 3, 4, x=5, y=6, z=7))

a. 30
b. 28
c. 32
d. 24

Answer: a. 30

45. What will be the output of the following code snippet:

def foo(num1, num2, *args, **kwargs):


print("num1: ", num1)
print("num2: ", num2)
print("args: ", args)
print("kwargs: ", kwargs)
foo(10, 20, 30, 40, 50, x=60, y=70, z=80)

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}

46. What will be the output of the following code snippet:

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")

a. Exception: list index out of range


b. IndexError: list index out of range
c. Finally block
d. None of the above

Answer: b. IndexError: list index out of range

47. What would be the output of the following code snippet:

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

48. What would be the output of the following code snippet:

def func(x, *args, **kwargs):


print(x, args, kwargs)
func(1, 2, 3, 4, a=5, b=6)

a. 1 (2, 3, 4) {'a': 5, 'b': 6}


b. 1 (2, 3, 4) {a: 5, b: 6}
c. 1 (2, 3, 4, 5, 6) {}
d. None of the above

Answer: a. 1 (2, 3, 4) {'a': 5, 'b': 6}

49. What would be the output of the following code snippet:

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

50. What would be the output of the following code snippet:

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]

51. What would be the output of the following code snippet:

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

Answer: a. Hello World

52. What would be the output of the following code snippet:

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]))

a. integer string unknown


b. string integer unknown
c. Error unknown unknown
d. integer string error

Answer: a. integer string unknown

53. What would be the output of the following code snippet:

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

Answer: a. Zero Division Error

55. What would be the output of the following code snippet:

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

56. What would be the output of the following code snippet:

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]

57. What would be the output of the following code snippet:

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

58. What is the output of the following program?

Nums = [3, 5, 7, 8, 9]
temp = [[x for x in Nums] for x in range(3)]
print (temp)

a) [[[3, 5, 7, 8, 9]], [[3, 5, 7, 8, 9]], [[3, 5, 7, 8, 9]]]


b) [[3, 5, 7, 8, 9], [3, 5, 7, 8, 9], [3, 5, 7, 8, 9]]
c) [[[3, 5, 7, 8, 9]], [[3, 5, 7, 8, 9]]]
d) None of these

Answer: (b)

59. What is the output of the following program?

data = [x for x in range(10,-1,-1)]


temp = [x for x in range(7) if x in data and x%2==0]
print(temp)

a) [0, 2, 4, 6]
b) [0, 2, 4]
c) [0, 1, 2, 3, 4, 5, 6]
d) Runtime error

Answer: (a)

60. What is the output of the following program?

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)

61. Which code cannot be used to reverse the integer?

i) num = 12345
S = str(num)
print(int(S[::-1]))

ii) num = 12345


for i in str(num[::-1]):
print(i, end=””)
iii)num = 12345
Rev = 0
for i in range(5):
mod = num%10
rev *= 10
rev += mod
num //= 10

iv) num = 12345


print(num[::-1])

a) iv) and iii)


b) i) and ii)
c) iv
d) i) and iii)

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?

for sentence in paragraph:


for word in sentence.split():
single_word_list.append(word)

a) word for sentence in paragraph for word in sentence.split()


b) [word for sentence in paragraph for word in sentence.split()]
c) word for word in sentence.split() for sentence in paragraph
d) [word for word in sentence.split() for sentence in paragraph]

64. What will the output be of the following code?

D = {1:['Raj', 22], 2:['Simran', 21], 3:['Rahul', 40]}


for val in D:
print(val)
a)
1
2
3

b)
[‘Raj’, 22]
[‘Simran’, 21]
[‘Rahul’, 40]

c)
1 [‘Raj’, 22]
2 [‘Simran’, 21]
3 [‘Rahul’, 40]

d)
‘Raj’
‘Simran’
‘Rahul’

65. What will be the output of the following code?

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

67. What will be the output of the following code?

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

68. What will be the output of the following code?

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

69. Which of the following is a mutable data type in Python?


A. str
B. int
C. tuple
D. list
Answer: D

70. What will be the output of the following code?

def add(a, b):


return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
return a / b
print(add(5, 3))
print(subtract(5, 3))
print(multiply(5, 3))
print(divide(5, 3))

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

71. What will be the output of the following code?

try:
a = 5/0
except ZeroDivisionError as e:
print("Error: ", e)

A. Error: division by zero


B. Error: None
C. Error:
D. None of the above

Answer: A

72. What will be the output of the following code?

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

74. What is the output of the following code?

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]

75. What is the output of the following code?

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

76. What is the output of the following code?

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

Answer: b. [1, 2, 4, 5] None

77. What is the output of the following code?

a = [1, 2, 3, 4, 5]
b = a.count(3)
print(b)

a. 0
b. 1
c. 2
d. 3

Answer: b. 1

78. What is the output of the following code?

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

Answer: a. [1, 2, 3, 4, 5, 6, 7, 8] None


79. What is the output of the following code?

a = [1, 2, 3, 4, 5]
b = a.index(3)
print(b)
a. 0
b. 1
c. 2
d. 3

Answer: d. 3

80. What is the correct syntax to create an empty tuple in Python?


a. empty_tuple = ()
b. empty_tuple = []
c. empty_tuple = {}
d. empty_tuple = list()

Ans: a

81. What is the result of the following code?

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

82. How to access the last element of a tuple in Python?

a. tuple[-1]
b. tuple[len(tuple) - 1]
c. tuple[len(tuple)]
d. None of the above

Ans: a

83. What is the output of the following code?


a = (1, 2, 3)
b = (4, 5, 6)
print(a + b)

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

84. What is the result of the following code?

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

85. What is the output of the following code?

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

86. What is the result of the following code?

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

87. How to check if an element exists in a tuple in Python?

a. elem in tuple
b. tuple.index(elem)
c. elem not in tuple
d. None of the above

Ans: a

88. What is the result of the following code?

t = (1, 2, 3)
print(len(t))

a. 0
b. 3
c. [1, 2, 3]
d. None of the above

Ans: b

89. What will be the output of the following code?

d = {'key1': 'value1', 'key2': 'value2'}


print(d['key3'])
a. value1
b. value2
c. KeyError: 'key3'
d. None of the above

Answer: c. KeyError: 'key3'

90. What is the correct way to update the value of key 'key1' to 'new_value1' in the
following dictionary?

d = {'key1': 'value1', 'key2': 'value2'}

a. d.update(key1 = 'new_value1')
b. d['key1'] = 'new_value1'
c. d[key1] = 'new_value1'
d. None of the above

Answer: b. d['key1'] = 'new_value1'

91. What will be the output of the following code?

d = {'key1': 'value1', 'key2': 'value2'}


print(d.get('key3', 'Key not found'))

a. value1
b. value2
c. Key not found
d. None of the above

Answer: c. Key not found

92. What will be the output of the following code?

d = {'key1': 'value1', 'key2': 'value2'}


d.update({'key3': 'value3', 'key4': 'value4'})
print(d)

a. {'key1': 'value1', 'key2': 'value2'}


b. {'key3': 'value3', 'key4': 'value4'}
c. {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4'}
d. None of the above

Answer: c. {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4'}

93. What will be the output of the following code?

d = {'key1': 'value1', 'key2': 'value2'}


print(len(d))

a. 1
b. 2
c. 3
d. None of the above

Answer: b. 2

94. What is the output of the following code?

d = {'key1': 'value1', 'key2': 'value2'}


del d['key2']
print(d)

a. {'key1': 'value1'}
b. {'key2': 'value2'}
c. {'key1': 'value1', 'key2': 'value2'}
d. None of the above

Answer: a. {'key1': 'value1'}

95. What will be the output of the following code?

d = {'key1': 'value1', 'key2': 'value2'}


print('key3' in d)
a. True
b. False
c. value1
d. None of the above

96. Given the following code:


string = "Today is {0} {1}, {2}"
print(string.format("Monday", "January", "10"))
What will be the output?

a. Today is Monday January, 10


b. Today is 10 Monday, January
c. Today is January Monday, 10
d. Today is Monday 10, January

Answer: a. Today is Monday January, 10

97. Given the following code:


i = 10
while i > 0:
print(i)
i -= 1
What is the output of the code?

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

98. Given the following code:


i = 10
while True:
print(i)
i -= 1
What is the behavior of the code?

a. The code will run forever


b. The code will run 10 times
c. The code will stop after 5 iterations
d. The code will run 5 times

Answer: a. The code will run forever

99. Given the following code:


with open("file.txt", "w") as f:
f.write("Hello World")
What will be the result of this code?

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

100. Given the following code:


with open("file.txt", "r") as f:
print(f.readline())
What will be the result of this code if file.txt exists and contains the text "Hello World"?

a. The code will print "Hello World"


b. The code will print "Hello World\n"
c. The code will produce an error because the file is not in write mode
d. The code will produce an error because the file does not exist

Answer: a. The code will print "Hello World"

101. Given the following code:


with open("file.txt", "r") as f:
print(f.readlines())
What will be the result of this code if file.txt exists and contains the text "Hello
World\nHow are you"?

a. The code will print ["Hello World\n", "How are you"]


b. The code will print ["Hello World\nHow are you"]
c. The code will print ["Hello World", "How are you"]
d. The code will produce an error because the file is not in write mode

Answer: a. The code will print ["Hello World\n", "How are you"]

You might also like