0% found this document useful (0 votes)
5 views

python MCQ

The document contains multiple-choice questions related to Python programming, covering various topics such as syntax, data types, functions, and operators. Each question is followed by four answer options, with the correct answer indicated. The questions are organized into units, suggesting a structured assessment format for students learning Python.

Uploaded by

Sandip Kumar
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)
5 views

python MCQ

The document contains multiple-choice questions related to Python programming, covering various topics such as syntax, data types, functions, and operators. Each question is followed by four answer options, with the correct answer indicated. The questions are organized into units, suggesting a structured assessment format for students learning Python.

Uploaded by

Sandip Kumar
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/ 33

S.D.

COLLEGE OF MANAGEMENT STUDIES MZN(732)


BS.c(CS)-2year (4sem) Python code-(0427103)
Multiple Choice Questions

UNIT-1

1. Who developed Python Programming Language?


a) Wick van Rossum
b) Rasmus Lerdorf
c) Guido van Rossum
d) Niene Stom
View Answer-c

2. Which type of Programming does Python support?


a) object-oriented programming
b) structured programming
c) functional programming
d) all of the mentioned
View Answer-d

3. Is Python case sensitive when dealing with identifiers?


a) no
b) yes
c) machine dependent
d) none of the mentioned
View Answer-b

4. Which of the following is the correct extension of the Python file?


a) .python
b) .pl
c) .py
d) .p
View Answer-c

5. Is Python code compiled or interpreted?


a) Python code is both compiled and interpreted
b) Python code is neither compiled nor interpreted
c) Python code is only compiled
d) Python code is only interpreted
View Answer-a

6. All keywords in Python are in _________


a) Capitalized
b) lower case
c) UPPER CASE
d) None of the mentioned
View Answer-d

7. What will be the value of the following Python expression?

4 + 3 % 5

a) 7
b) 2
c) 4
d) 1
View Answer-a

8. Which of the following is used to define a block of code in Python language?


a) Indentation
b) Key
c) Brackets
d) All of the mentioned
View Answer-a

9. Which keyword is used for function in Python language?


a) Function
b) def
c) Fun
d) Define
View Answer-b

10. Which of the following character is used to give single-line comments in Python?
a) //
b) #
c) !
d) /*
View Answer-b

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

i = 1
while True:
if i%3 == 0:
break
print(i)

i + = 1
a) 1 2 3
b) error
c) 1 2
d) none of the mentioned
View Answer-b
12. Which of the following functions can help us to find the version of python that we are
currently working on?
a) sys.version(1)
b) sys.version(0)
c) sys.version()
d) sys.version
View Answer-d

13. Python supports the creation of anonymous functions at runtime, using a construct
called __________
a) pi
b) anonymous
c) lambda
d) none of the mentioned
View Answer-c

14. What is the order of precedence in python?


a) Exponential, Parentheses, Multiplication, Division, Addition, Subtraction
b) Exponential, Parentheses, Division, Multiplication, Addition, Subtraction
c) Parentheses, Exponential, Multiplication, Addition, Division, Subtraction
d) Parentheses, Exponential, Multiplication, Division, Addition, Subtraction
View Answer-d

15. What will be the output of the following Python code snippet if x=1?

x<<2

a) 4
b) 2
c) 1
d) 8
View Answer-a

16. What does pip stand for python?


a) Pip Installs Python
b) Pip Installs Packages
c) Preferred Installer Program
d) All of the mentioned
View Answer-c

17. Which of the following is true for variable names in Python?


a) underscore and ampersand are the only two special characters allowed
b) unlimited length
c) all private members must have leading and trailing underscores
d) none of the mentioned
View Answer-b

18. What are the values of the following Python expressions?

2**(3**2)
(2**3)**2
2**3**2

a) 512, 64, 512


b) 512, 512, 512
c) 64, 512, 64
d) 64, 64, 64
View Answer-a

19. Which of the following is the truncation division operator in Python?


a) |
b) //
c) /
d) %
View Answer-b

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

l=[1, 0, 2, 0, 'hello', '', []]


list(filter(bool, l))

a) [1, 0, 2, ‘hello’, ”, []]


b) Error
c) [1, 2, ‘hello’]
d) [1, 0, 2, 0, ‘hello’, ”, []]
View Answer-c

21. Which of the following functions is a built-in function in python?


a) factorial()
b) print()
c) seed()
d) sqrt()
View Answer-b

22. Which of the following is the use of id() function in python?


a) Every object doesn’t have a unique id
b) Id returns the identity of the object
c) All of the mentioned
d) None of the mentioned
View Answer-b
23. The following python program can work with ____ parameters.
def f(x):
def f1(*args, **kwargs):
print("Sanfoundry")
return x(*args, **kwargs)
return f1

a) any number of
b) 0
c) 1
d) 2
View Answer-a

24. What will be the output of the following Python function?

min(max(False,-3,-4), 2,7)

a) -4
b) -3
c) 2
d) False
View Answer-d

25. Which of the following is not a core data type in Python programming?
a) Tuples
b) Lists
c) Class
d) Dictionary
View Answer-c

26. What will be the output of the following Python expression if x=56.236?

print("%.2f"%x)

a) 56.236
b) 56.23
c) 56.0000
d) 56.24
View Answer-d

27. Which of these is the definition for packages in Python?


a) A set of main modules
b) A folder of python modules
c) A number of files containing Python definitions and statements
d) A set of programs making use of Python modules
View Answer-b
28. What will be the output of the following Python function?

len(["hello",2, 4, 6])
a) Error
b) 6
c) 4
d) 3
View Answer-c

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

x = 'abcd'
for i in x:
print(i.upper())

a)

b) a b c d
c) error
d)

View Answer-d

30. What is the order of namespaces in which Python looks for an identifier?
a) Python first searches the built-in namespace, then the global namespace and finally
the local namespace
b) Python first searches the built-in namespace, then the local namespace and finally
the global namespace
c) Python first searches the local namespace, then the global namespace and finally the
built-in namespace
d) Python first searches the global namespace, then the local namespace and finally the
built-in namespace
View Answer-b
UNIT-2

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

for i in [1, 2, 3, 4][::-1]:


print(i, end=' ')

a) 4 3 2 1
b) error
c) 1 2 3 4
d) none of the mentioned
View Answer-a

32. What will be the output of the following Python statement?

1. >>>"a"+"bc"

a) bc
b) abc
c) a
d) bca
View Answer-b

33. Which function is called when the following Python program is executed?

f = foo()
format(f)

a) str()
b) format()
c) __str__()
d) __format__()
View Answer-c
34. Which one of the following is not a keyword in Python language?
a) pass
b) eval
c) assert
d) nonlocal
View Answer d
35. What will be the output of the following Python code?

1. class tester:
2. def __init__(self, id):
3. self.id = str(id)
4. id="224"
5.
6. >>>temp = tester(12)
7. >>>print(temp.id)
a) 12
b) 224
c) None
d) Error
View Answer-a

36. What will be the output of the following Python program?

def foo(x):
x[0] = ['def']
x[1] = ['abc']
return id(x)
q = ['abc', 'def']
print(id(q) == foo(q))

a) Error
b) None
c) False
d) True
View Answer-d

37. Which module in the python standard library parses options received from the
command line?
a) getarg
b) getopt
c) main
d) os
View Answer-b

38. What will be the output of the following Python program?

z=set('abc')
z.add('san')
z.update(set(['p', 'q']))
z

a) {‘a’, ‘c’, ‘c’, ‘p’, ‘q’, ‘s’, ‘a’, ‘n’}


b) {‘abc’, ‘p’, ‘q’, ‘san’}
c) {‘a’, ‘b’, ‘c’, ‘p’, ‘q’, ‘san’}
d) {‘a’, ‘b’, ‘c’, [‘p’, ‘q’], ‘san}
View Answer-c

39. What arithmetic operators cannot be used with strings in Python?


a) *
b) –
c) +
d) All of the mentioned
View Answer-b

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


print("abc. DEF".capitalize())

a) Abc. def
b) abc. def
c) Abc. Def
d) ABC. DEF
View Answer-a

41. Which of the following statements is used to create an empty set in Python?
a) ( )
b) [ ]
c) { }
d) set()
View Answer-d

42. Find the invalid variable among the following:

a. 1st_string

b. my_string_1

c. _

d. foo

Answer: (a) 1st_string

43. The order of precedence in the Python language is:

A) Exponential

B) Parentheses

C) Division

D) Multiplication

E) Subtraction

F) Addition

44. Which one of these is incorrect?

a. float(‘nan’)

b. float(‘inf’)

c. float(’12+34′)
d. float(’56’+’78’)

Answer: (c) float(’12+34′)

45. The value of the Python expression given below would be:

4+2**5//10

a. 77

b. 0

c. 3

d. 7

Answer: (d) 7

46. The return value for trunc() would be:

a. bool

b. float

c. int

d. None

Answer: (c) int

47. What is the output of the Python code given below, if the date of the system is June
21st, 2017 (Wednesday)?

[] or {}
{} or []

a.

[] []
b.

[]
{}

c.

{}

{}
d.

{}

[]
Answer: (d)

{}

[]
48. The output of this Python code would be:

s='{0}, {1}, and {2}’

s.format(‘hi’, ‘great’, ‘day’)

a. ‘hi, great, and day’

b. ‘hi great and day’

c. ‘hi, great, day’

d. Error

Answer: (a) ‘hi, great, and day’

49. The output of this Python code would be:

a = [‘mn’, ‘op’]

for i in a:

i.upper()

print(a)

a. [None, None]

b. [‘MN’, ‘OP’]

c. [‘mn’, ‘op’]

d. None of the above

Answer: (c) [‘mn’, ‘op’]

50. The output of this Python code would be:

print(“mno. PQR”.capitalize())
a. Mno. Pqr

b. Mno. pqr

c. MNO. PQR

d. mno. pqr

Answer: (b) Mno. pqr

51. Which arithmetic operators can we NOT use with strings?

a. –

b. +

c. *

d. All of the above

Answer: (a) –

52. Which function do we use to shuffle a list(say list1)?

a. shuffle(list1)

b. list1.shuffle()

c. random.shuffleList(list1)

d. random.shuffle(list1)

Answer: (d) random.shuffle(list1)

54. In the following statements of Python, which ones will result into the output: 6?

A = [[1, 2, 3],

[4, 5, 6],
[7, 8, 9]]
a. A[3][2]

b. A[2][3]

c. A[1][2]

d. A[2][1]

Answer: (c) A[1][2]


55. Is this code valid in Python?

>>> m=6,7,8,9

>>> m

a. No, many values will unpack

b. Yes, (6,7,8,9) will be printed

c. Yes, 6 will be printed

d. Yes, [6,7,8,9] will be printed

Answer: (b) Yes, (6,7,8,9) will be printed

56. Which function removes a set’s first and the last element from a list?

a. pop

b. remove

c. dispose

d. discard

Answer: (a) pop

57. The output of this Python code would be:

>>> x={1:”X”,2:”Y”,3:”Z”}

>>> del x

a. the del method does not exist for dictionary

b. the del would delete the values present in dictionary

c. the del would delete the entire dictionary

d. the del would delete all the keys in dictionary

Answer: (d) the del would delete all the keys in dictionary

58. The output of this Python code would be:

sum(1,2,3)

sum([2,4,6])
a. 6, 12

b. Error, Error

c. Error, 12

d. 6, Error

Answer: (c) Error, 12

59. The output of this Python code would be:

def find(x, **y):

print(type(y))

find(‘letters’,X=’1′,Y=’2′)

a. Dictionary

b. An exception is thrown

c. String

d. Tuple

Answer: (a) Dictionary

60. Which one of these is NOT true about recursion?

a. We can replace a recursive function by a non-recursive function

b. The memory space taken by the recursive functions is more than that of non-
recursive function

c. Running a recursive function is faster as compared to a non-recursive function

d. The process of recursion makes it easier for users to understand a program

Answer: (c) Running a recursive function is faster as compared to a non-recursive


function

UNIT-3

61. The output of this Python code would be:

a = [‘mn’, ‘op’]

print(len(list(map(list, a))))))
a. 4

b. 2

c. Not specified

d. Error

Answer: (d) Error

62. Which of these functions can NOT be defined under the sys module?

a. sys.argv

b. sys.readline

c. sys.path

d. sys.platform

Answer: (b) sys.readline

63. Which function doesn’t accept any argument?

a. re.compile

b. re.findall

c. re.match

d. re.purge

Answer: (d) re.purge

64. In Python, the primary use of the tell() method is that:

a. within the file, it tells the end position

b. within the file, it tells the current position

c. it tells us if the file is opened

d. none of the above

Answer: (b) within the file, it tells the current position

65. The hasattr(obj,name) is used to:

a. check if any specific attribute exists


b. set an attribute

c. access the object’s attribute

d. delete an attribute

Answer: (a) check if any specific attribute exists

66. Find out the private data field among the following:

def __init__(self):

__m = 1

self.__n = 1

self.__o__ = 1

__p__= 1

a. __m

b. __n

c. __o__

d. __p__

Answer: (b) __n

67. In Python, find which one isn’t an exception handling keyword.

a. accept

b. finally

c. try

d. except

Answer: (a) accept

68. Which of the following statements is true about Python?


a) Python is a compiled language
b) Python is a high-level language
c) Python is primarily used for numerical computation only
d) Python is not suitable for web development

Answer: b
Explanation: Python is a high-level programming language known for its readability
and simplicity.

69. Which symbol is used for single-line comments in Python?

a) //
b) #
c) –
d) /* */

Answer: b
Explanation: The hash symbol (#) is used for single-line comments.

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

x = 5y = 2print(x ** y) Copy Code

a) 10
b) 7
c) 25
d) 3

Answer: c

71. Which of the following is a valid Python variable name?

a) 2var
b) _myVar
c) my-var
d) my var

Answer: b

72. What does the range() function in Python return?

a) A list of numbers
b) A tuple of numbers
c) A sequence of numbers
d) A dictionary of numbers
Answer: c

73. Which of the following is not a valid data type in Python?

a) int
b) float
c) char
d) str

Answer: c

74. What does the strip() method do in Python?

a) Removes all leading and trailing spaces from a string


b) Splits a string into a list of substrings
c) Joins multiple strings into one
d) Replaces occurrences of a substring within a string

Answer: a

75. Which of the following statements is true about Python’s variable naming
convention?

a) Variables must start with a capital letter.


b) Variables cannot contain numbers.
c) Variable names can include underscores (_).
d) Variable names cannot be longer than 10 characters.

Answer: c

76. What will be the output of the following code snippet?

print(3 * 'abc')Copy Code

a) abcabcabcabc
b) abcabc
c) abcabcabc
d) Error: cannot multiply sequence by non-int of type ‘str’

Answer: c

77. Which of the following is used to open a file in Python for reading and writing?
a) r+
b) rw
c) a+
d) Both a and c

Answer: d

78. What does the __init__ method do in Python?

a) It initializes all the variables in a class.


b) It is a constructor method that is automatically called when a new instance of a
class is created.
c) It is used to delete an instance of a class.
d) It is a reserved keyword and cannot be used as a method name.

Answer: b

79. Which of the following is NOT a valid way to comment out multiple lines of code
in Python?

a) Using triple single quotes (”’ ”’)


b) Using triple double quotes (“”” “””)
c) Using the pound sign (#) at the beginning of each line
d) Using the semicolon (;) at the end of each line

Answer: d

80 What is the output of the following code snippet?

print(9 / 2)Copy Code

a) 4.5
b) 4
c) 4.0
d) Error: division by zero

Answer: a.

81. Which of the following is NOT a valid data type in Python?

a) tuple
b) array
c) set
d) dictionary
Answer: b

82. What does the break statement do in Python?

a) It terminates the program immediately.


b) It breaks out of the current loop and resumes execution at the next statement.
c) It raises an exception.
d) It continues to the next iteration of the loop.

Answer: b.

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

a) tuple
b) string
c) list
d) set

Answer: c

84. What will be the output of the following code snippet?

x = 10y = 5print(x > y and x < 15)Copy Code

a) True
b) False
c) Error: invalid syntax
d) Error: x and y are not defined

Answer: a

85. Which of the following statements is true about Python?

a) Python is a statically typed language


b) Python is not suitable for machine learning tasks
c) Python is not an open-source language
d) Python uses indentation to define code blocks

Answer: d
86. What does the len() function in Python return?

a) The length of a string


b) The number of elements in a list or tuple
c) The number of keys in a dictionary
d) All of the mentioned
Answer: d

87. Which of the following is the correct way to open a file named “example.txt” in
Python for reading?

a) file = open(“example.txt”, “r”)


b) file = open(“example.txt”, “read”)
c) file = open(“example.txt”, “w”)
d) file = open(“example.txt”, “write”)

Answer: a

88. What does the append() method do when used with lists in Python?

a) Adds an element to the end of the list


b) Removes the last element of the list
c) Sorts the elements of the list
d) Reverses the order of elements in the list

Answer: a

89. Which of the following is the correct way to define a function in Python?

a) def function_name:
b) function function_name():
c) define function_name():
d) def function_name():

Answer: d

90. What will be the output of the following code snippet?

my_list = [1, 2, 3, 4, 5]print(my_list[2:4])Copy Code

a) [1, 2]
b) [3, 4]
c) [2, 3]
d) [2, 4]

Answer: b

UNIT-4

91. Which of the following is the correct way to import the math module in Python?
a) import math
b) include math
c) use math
d) require math

Answer: a
92. Which of the following is not a valid comparison operator in Python?

a) <=
b) =>
c) ==
d) !=

Answer: b

93. What does the pop() method do when used with lists in Python?

a) Removes the first element of the list


b) Removes the last element of the list
c) Removes the element at the specified index
d) Adds an element to the end of the list

Answer: b
94. Which of the following is true about Python’s None keyword?

a) It represents an empty string


b) It represents the absence of a value
c) It represents infinity
d) It represents a boolean value

Answer: b

95. What is the output of 10 / 3 in Python?

a) 3.3333
b) 3.0
c) 3
d) 4

Answer: a

96. Which of the following is a valid way to create a dictionary in Python?

a) {key1: value1, key2: value2}


b) dict(key1=value1, key2=value2)
c) dict([key1, value1], [key2, value2])
d) Both a and b

Answer: d

97. What is the output of the following code?

my_string = "Hello World"print(my_string[::-1])Copy Code

a) dlroW olleH
b) Hello World
c) World Hello
d) olleH dlroW

Answer: a) dlroW olleH

98. What is the output of the following code?

print(type(3.5))Copy Code

a) <class ‘float’>
b) <class ‘int’>
c) <class ‘string’>
d) <class ‘number’>

Answer: a) <class ‘float’>

99. Which of the following is a valid variable name in Python?

a) 1_variable
b) variable_name
c) variable-name
d) None

Answer: b) variable_name

100. What is the result of the following expression?

'Hello' == 'hello' Copy Code

a) True
b) False
c) Error
d) None
Answer: b) False

101. What is the primary purpose of the if statement in Python?

a) To execute a block of code based on a condition

b) To perform mathematical operations

c) To repeat a block of code

d) To define a function

Answer: a

102. What will be the output of the following code snippet?

x = 10
if x > 5:
print("x is greater than 5")

a) x is greater than 5
b) x is less than 5
c) x is equal to 5
d) No output
Answer: a

103. Which keyword is used to execute a block of code if the condition in the if
statement is false?

a) else

b) elif

c) while

d) for

Answer: a

104. What is the purpose of the elif statement in Python?

a) To execute a block of code if the previous conditions are false

b) To define a loop

c) To perform arithmetic operations


d) To exit from a loop

Answer: a

105. What will be the output of the following code snippet?

x=5
if x < 3:
print("x is less than 3")
elif x == 3:
print("x is equal to 3")
else:
print("x is greater than 3")

a) x is less than 3 b) x is equal to 3 c) x is greater than 3 d) No output

Answer: c

106. How can you execute multiple statements under a single if block in Python?

a) Separate statements with a semicolon

b) Indent the statements to the same level

c) Use the and keyword between statements

d) Use the elif keyword

Answer: b

107. What will the following code snippet print?

x = 10
if x < 5:
print("x is less than 5")
elif x > 15:
print("x is greater than 15")
else:
print("x is between 5 and 15")

a) x is less than 5 b) x is greater than 15 c) x is between 5 and 15 d) No output

Answer: c

108. What is the purpose of the while loop in Python?

a) To execute a block of code repeatedly until a condition is false

b) To execute a block of code a fixed number of times


c) To define a function

d) To iterate over items in a sequence

Answer: a

109. What is the syntax for a while loop in Python?

a) while condition:

b) while condition():

c) while (condition):

d) while loop condition:

Answer: a

110. How can you exit a loop prematurely in Python?

a) Using the break statement


b) Using the continue statement
c) Using the pass statement
d) Using the exit function

Answer: a

111. What is the purpose of the for loop in Python?

a) To execute a block of code repeatedly until a condition is false


b) To iterate over items in a sequence
c) To execute a block of code a fixed number of times
d) To define a function

Answer: b

112. What is the syntax for a for loop in Python?

a) for item in sequence:


b) for item in range(n):
c) for index in range(len(sequence)):
d) All of the above

Answer: d

113. What will the following code snippet print?

fruits = ["apple", "banana", "cherry"]


for fruit in fruits:
print(fruit)

a) apple banana cherry


b) [“apple”, “banana”, “cherry”]
c) 0 1 2
d) No output

Answer: a

114. How can you skip the current iteration of a loop and continue with the next
iteration?

a) Using the skip statement


b) Using the pass statement
c) Using the break statement
d) Using the continue statement

Answer: d

115. What is the purpose of the range() function in Python?

a) To generate a sequence of numbers


b) To iterate over items in a sequence
c) To define a function
d) To execute a block of code repeatedly until a condition is false

Answer: a

116. What will the following code snippet print?

for i in range(3):
print(i)

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

Answer: a

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

for i in range(1, 6):


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

Answer: c

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

for i in range(3):
for j in range(3):
print(i + j, end=' ')
print()

a) 0 1 2
123
234

b) 0 1 2
123
234
345

c) 0 1 2
123
234
456

d) 0 1 2
234
456

Answer: a

119. What will be printed by the following code?

num = 5
while num > 0:
print(num)
num -= 1
if num == 3:
break
else:
print("Done")

a) 5 4 3 2 1
b) 5 4
c) Done
d) 5 4 3

Answer: b

120. What is the output of the following code?

x = 10
if x > 5:
print("A")
elif x > 7:
print("B")
else:
print("C")

a) A
b) B
c) C
d) A and B

Answer: a

UNIT-5

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

x=5
while x > 0:
print(x, end=" ")
x -= 2
if x == 1:
break
else:
print("Done")

a) 5 3 1
b) 5 3
c) 5 3 Done
d) 5 3 1 Done

Answer: a

Q22. What is the output of the following code?

for i in range(5):
if i == 2:
continue
print(i, end=" ")
a) 0 1 3 4
b) 0 1 2 3 4
c) 0 1 3 4 5
d) 0 1 2 3 4 5

Answer: a

123. What is the output of the following code?

num = 0
while num < 5:
print(num)
num += 1
else:
print("Loop completed.")

a) 0 1 2 3 4
b) 0 1 2 3 4 Loop completed.
c) Loop completed.
d) This code will result in an error.

Answer: b

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

x = 10
if x > 5:
print("Greater than 5")
if x > 8:
print("Greater than 8")
if x > 12:
print("Greater than 12")
else:
print("Equal or less than 12")

a) Greater than 5
Greater than 8
Equal or less than 12

b) Greater than 5
Greater than 8
Greater than 12
Equal or less than 12

c) Greater than 5
Greater than 8

d) Equal or less than 12


Answer: a

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

for i in range(3):
for j in range(3):
print(i * j, end=' ')
print()

a) 0 0 0
012
024

b) 0 1 2
024
036

c) 0 0 0
012
024
036

d) 0 0 0
012
012

Answer: a

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

num = 10
while num > 0:
print(num)
num -= 3
else:
print("Loop completed.")

a) 10 7 4 1 Loop completed.
b) 10 7 4 1
c) Loop completed.
d) This code will result in an error.

Answer: a

127. What will the following code snippet print?

for i in range(3):
pass
print(i)

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

Answer: c

128. What is the purpose of the pass statement in Python?

a) To exit from a loop prematurely


b) To skip the current iteration of a loop
c) To execute a block of code if a condition is false
d) To do nothing and act as a placeholder

Answer: d

129. What is the purpose of the else block in a loop in Python?

a) To execute if the loop encounters an error


b) To execute if the loop completes without encountering a break statement
c) To execute if the loop encounters a continue statement
d) To execute if the loop encounters a pass statement

Answer: b

130. What is the output of the following code?

x = 10
if x > 5:
print("Hello")
elif x > 8:
print("Hi")
else:
print("Hey")

a) Hey
b) Hi
c) Hello
d) Hello, Hi

Answer: c

131. What is the output of the following code?

for i in range(1, 6):


if i % 2 == 0:
print(i)
continue
print("*")

a) \n2\n\n4\n*
b) \n2\n\n4\n*\n
c) \n2\n\n*\n4\n
d) \n\n2\n*\n4\n

Answer: b

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

x=5
while x > 0:
print(x)
x -= 1
else:
print("Done")

a) 5\n4\n3\n2\n1\nDone
b) Done\n5\n4\n3\n2\n1
c) 5\n4\n3\n2\n1
d) Done

Answer: a

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

for i in range(1, 6):


if i == 3:
break
print(i)
else:
print("Loop completed.")

a) 1\n2
b) 1\n2\n3\n4\n5\nLoop completed.
c) 1\n2\n3\n4\n5
d) 1\n2\n3

Answer: a

You might also like