Python MCQ (Multiple Choice Questions)
1. Who developed Python Programming Language?
a) Wick van Rossum
b) Rasmus Lerdorf
c) Guido van Rossum
d) Niene Stom
Answer: c
Explanation: Python language is designed by a Dutch programmer Guido van Rossum in
the Netherlands.
2. Which type of Programming does Python support?
a) object-oriented programming
b) structured programming
c) functional programming
d) all of the mentioned
Answer: d
Explanation: Python is an interpreted programming language, which supports object-
oriented, structured, and functional programming.
3. Is Python case sensitive when dealing with identifiers?
a) no
b) yes
c) machine dependent
d) none of the mentioned
Answer: b
Explanation: Case is always significant while dealing with identifiers in python.
4. Which of the following is the correct extension of the Python file?
a) .python
b) .pl
c) .py
d) .p
Answer: c
Explanation: ‘.py’ is the correct extension of the Python file. Python programs can be written
in any text editor. To save these programs we need to save in files with file extension ‘.py’.
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
Answer: a
Explanation: Many languages have been implemented using both compilers and
interpreters, including C, Pascal, and Python.
6. All keywords in Python are in _________
a) Capitalized
b) lower case
c) UPPER CASE
d) None of the mentioned
Answer: d
Explanation: Most keywords are in lowercase, but some like True, False, and None are
capitalized.
7. What will be the value of the following Python expression?
print(4 + 3 % 5)
a) 7
b) 2
c) 4
d) 1
Answer: a
Explanation: In Python, the modulus operator % has higher precedence than addition +. So,
the expression is evaluated as 4 + (3 % 5), which is 4 + 3 = 7.
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
Answer: a
Explanation: In Python, to define a block of code we use indentation. Indentation refers to
whitespaces at the beginning of the line.
9. Which keyword is used for function in Python language?
a) Function
b) def
c) Fun
d) Define
Answer: b
Explanation: The def keyword is used to create, (or define) a function in python.
10. Which of the following character is used to give single-line comments in Python?
a) //
b) #
c) !
d) /*
Answer: b
Explanation: To write single-line comments in Python use the Hash character (#) at the
beginning of the line. It is also called number sign or pound sign. To write multi-line
comments, close the text between triple quotes.
Example: “”” comment
text “””
1. What is the purpose of the if statement in Python?
A
To define a function
B
To declare a variable
C
To execute a block of code conditionally
D
To create a loop
Explanation
The if statement is used to execute a block of code only if a specified condition is true.
Question 2
2. Which of the following operators is used for equality comparison in Python?
2==
B
=
C
===
D
>
Explanation
The == operator is used for equality comparison.
3. What will be the output of the following code snippet?
x=5
if x > 0:
print("Positive")
else:
print("Negative")
A
Positive
B
Negative
C
Zero
D
Error
Explanation
4. The code checks if x is greater than 0 and prints "Positive" accordingly.
What is the purpose of the elif statement in Python?
A
To handle exceptions
B
To create a loop
C
To define a function
D
To check additional conditions after the initial if statement
Explanation
The elif statement is used to check additional conditions if the initial if statement is
false.
5. what is the purpose of the else statement within an if-else block?
A
To define a function
B
To check additional conditions
C
To execute a block of code when the if condition is false
D
To create a loop
Explanation
The else statement is executed when the if condition is false.
6. What is the output of the following code snippet?
x = 10
if x > 5:
print("Greater than 5")
elif x > 8:
print("Greater than 8")
else:
print("Less than or equal to 5")
A
Greater than 5
B
Greater than 8
C
Less than or equal to 5
Explanation
The if condition is checked first and since 10 > 5 is true, it prints "Greater than 5" and
skips the remaining conditions.
7. Which statement is used to exit a loop prematurely in Python?
A
break
B
exit
C
return
D
continue
Explanation
The break statement is used to exit a loop prematurely.
8. In Python, what is the purpose of the try-except block?
A
To create a loop
B
To check multiple conditions
C
To handle exceptions
D
To define a function
Explanation
The try-except block is used to handle exceptions in Python.
9. What is the output of the following code snippet?
for i in range(5):
if i == 3:
continue
print(i)
A
01234
B
0124
C
012
Explanation
The continue statement skips the iteration when i is 3.
Which of the following is true about lists in Python?
A
Lists are dynamic sized arrays
B
Lists store references at items contiguous locations
C
A list can contain elements of different types.
D
All of the above
Question 2
What are strings in Python?
A
Arrays of bytes representing Unicode characters
B
Arrays of integers representing characters
C
Single characters only
D
Arrays of floats representing characters
Explanation
Strings in Python are sequences (arrays) of Unicode characters used to store and
work with text.
Question 3
What does the string method .join() do in Python?
A
Splits a string into a list
B
Concatenates a list of strings into one string
C
Reverses a string
D
Returns the index of a substring in the string
Explanation
The join() method in Python is a built-in string method that is used to concatenate
(join) a sequence of strings into a single string.
Question 4
Which method can be used to handle strings with both single and double quotes
simultaneously in Python?
A
Using triple quotes
B
Using backslash ()
C
Using parentheses
D
Using single quotes only
Explanation
Triple quotes allow you to use both single and double quotes inside the string without
escaping them.
Example:
str = '''She said, "I'm learning Python."'''
print str;
# Output: She said, "I'm learning Python."
Question 5
How can you print a string without resolving escape sequences in Python?
A
By using the repr() function
B
By using the eval() function
C
By using the str() function
D
By using the format() function
Explanation
In python repr() function ensure that the escape sequences are displayed as part of
the string rather than being processed.
Example:
string = "Hello\nWorld\t!"
print(repr(string)) # Output: 'Hello\nWorld\t!'
Question 6
What is the primary reason that updating or deleting characters from a string in Python is
not allowed?
A
Strings are mutable data types
B
Python strings are immutable data types
C
Python doesn't support character-level operations on strings
D
Python string operations are not efficient
Explanation
Python strings are immutable. This means that once a string is created, it cannot be
modified. Any operation that seems to modify a string will instead create a new string.
Example :
s = "Hello"
s[0] = "h" # This will raise an error.
Question 7
What is the purpose of the find() function in Python strings?
A
It returns the position of the last occurrence of a substring within a string.
B
It returns true if the string begins with a specified prefix.
C
It returns the position of the first occurrence of a substring within a string.
D
It returns true if all the letters in the string are lowercased.
Explanation
In Python, the find() method is used to search for a substring within a string. It returns
the index of the first occurrence of the substring. If the substring is not found, it
returns -1.
Example :
text = "Hello, World!"
index = [Link]("World")
print(index) # Output: 7
Question 9
What does the title() method do in Python strings?
A
It converts the first letter of every word to uppercase and others to lowercase.
B
It swaps the cases of all letters in the string.
C
It converts all letters to uppercase.
D
It converts all letters to lowercase.
Explanation
The title() method in Python converts the first character of each word in a string to
uppercase and the rest of the characters to lowercase.
Example:
text = "hello world"
result = [Link]()
print(result) # Output: "Hello World"
Question 10
What is the time complexity of the find() function in Python?
A
O(log n)
B
O(n^2)
C
O(n)
D
O(1)
Incorrect Answer!
Explanation
For a string of length n and a substring of length m, the find() method compares each
possible starting position in the string where the substring could fit, and checks for a
match. If no match is found, it will continue until the end of the string.
Therefore the time complexity for find() in O(n) where n is the length of the string.
Question 11
What does the count() function in Python do?
A
It counts the total number of characters in a string.
B
It counts the occurrences of a specified substring within a string.
C
It returns true if all characters in the string are alphabets.
D
It returns true if all characters in the string are numbers.
Explanation
In Python, the count() method counts the occurrences of a substring within a string. It
returns the number of non-overlapping occurrences of the specified substring.
Example:
text = "hello world, hello Python"
result = [Link]("hello")
print(result) # Output: 2
Question 1
What is the purpose of the range function in a for loop?
A
To create a list of numbers
B
To define the start and end of the loop
C
To generate a sequence of numbers
D
To specify the step size of the loop
Explanation
The range function is used to generate a sequence of numbers that can be iterated
over in a for loop.
Question 2
What is the purpose of the enumerate function in a for loop?
A
To create an enumerated list
B
To generate a sequence of numbers
C
To iterate over two lists simultaneously
D
To access both the index and the value of elements in an iterable
Explanation
The enumerate function is used to iterate over both the index and the value of
elements in an iterable.
Question 3
How can you iterate over a string in reverse order using a for loop?
A
for char in reversed(string):
B
for char in string[::-1]:
C
for char in [Link]():
D
Reverse iteration is not possible on strings
Explanation
The reversed function can be used to iterate over a string in reverse order.
Question 4
What will the following code output?
numbers = [3, 7, 2, 8, 5]
for i, num in enumerate(numbers):
if i == num:
break
print(num, end=' ')
A
37285
B
37
C
372
D
728
Explanation
The loop breaks when the index matches the value, so it stops at i == 2, printing 3 7 2.
Question 5
What is the purpose of the else clause in a for loop?
A
To execute an alternative block of code
B
To handle exceptions
C
To specify the step size of the loop
D
To execute code when the loop is terminated normally
Explanation
The else clause is executed when the loop completes its iterations without
encountering a break statement.
Question 6
What will the following code output?
for char in 'hello':
if char == 'l':
break
print(char, end=' ')
A
he
B
hel
C
hell
D
hello
Explanation
The code prints 'h' and 'e' and terminates when 'l' is encountered due to the break
statement.
Question 7
What will the following code output?
numbers = [1, 2, 3, 4, 5]
for i, num in enumerate(numbers):
if i % 2 == 0:
continue
print(num, end=' ')
A
135
B
24
C
12345
D
01234
Explanation
The code skips printing elements at even indices (i % 2 == 0), so only odd-indexed
elements are printed.
Question 8
What will the following code output?
numbers = [1, 2, 3, 4, 5]
for i in range(len(numbers) - 1, -1, -1):
print(numbers[i], end=' ')
A
54321
B
12345
C
55555
D
135
Explanation
The code prints the elements of the list in reverse order.
Question 1
How do you concatenate two strings in Python?
A
str1 . str2
B
str1 + str2
C
str1 , str2
D
concat(str1, str2)
Explanation
In Python, you can concatenate (join) two strings simply by using the + operator.
Here’s a short example with running code:
Python
s1 = "Hello"
s2 = "World"
# Using + operator
res = s1 + " " + s2
print(res)
Output
Concatenated String: Hello World
Question 2
What is the purpose of the __dict__ attribute in Python?
A
To access the dictionary of a list
B
To store the attributes of an object
C
To define a dictionary in Python
D
To convert a dictionary to a list
Explanation
__dict__ attribute contains a dictionary of an object's writable attributes and their
values, useful for inspecting or modifying an object's properties dynamically
Question 3
How do you convert a floating-point number to an integer in Python?
A
int(number)
B
float_to_int(number)
C
[Link]()
D
convert(int, number)
Question 5
How do you convert a floating-point number to an integer in Python?
A
int(number)
B
float_to_int(number)
C
[Link]()
D
convert(int, number)
Question 6
How do you convert a list of strings to a single string in Python?
A
"".join(list)
B
str(list)
C
convert(list, str)
D
" ".join(list)
Question 7
What is the output of the following code?
x = [1, 2, 3, 4]
y = filter(lambda a: a % 2 == 0, x)
print(list(y))
A
[1, 3]
B
[2, 4]
C
[1, 2, 3, 4]
D
[]
Question 8
What is the purpose of the enumerate() function in Python?
A
To get the index and value of each item in an iterable
B
To count the occurrences of an element in an iterable
C
To enumerate through a list
D
To enumerate through a dictionary
Question 9
How do you check if a key is present in a dictionary?
A
key in dictionary
B
[Link](dictionary)
C
[Link](key)
D
has_key(key, dictionary)
Question 10
How do you find the length of a list in Python?
A
len(list)
B
length(list)
C
[Link]()
D
count(list)
Question 11
What is the purpose of the max() function in Python?
A
To find the maximum value in a list
B
To get the maximum length of a string
C
To compare two variables
D
To find the maximum of two numbers
Question 12
What is the output of the following code?
x = "Hello"
print(x[1:4])
A
"Hell"
B
"ello"
C
"ell"
D
Error
Explanation
String slicing [1:4] extracts characters starting at index 1 up to (but not including) index
4, so it returns "ell"
Question 13
What is a variable in Python?
A
A reserved word
B
A data type
C
A location in memory to store data
D
A function
Explanation
A variable in Python is a named location in memory used to store data values.
Question 14
What is the output of the following code?
x=5
y=2
print(x // y)
A
2.5
B
2
C
2.0
D
3
Explanation
he // operator is floor division, which divides and rounds down to the nearest integer. 5
// 2 equals 2
Question 1
Which of these is not a core data type?
A
Lists
B
Dictionary
C
Tuples
D
Class
Explanation
Class is a user defined data type
Question 2
What is the data type of variable x in the following code?
x = [1, 23, ‘hello’, 1]
A
List
B
Dictionary
C
Tuple
D
Array
Explanation
[ ] defines a list
Question 3
Which of the following function convert a string x to a float in python?
A
int(x [,base])
B
long(x [,base] )
C
float(x)
D
str(x)
Explanation
float(x) − Converts x to a floating-point number
Question 4
Which Python datatype maintains the order of elements inserted?
A
set
B
list
C
dict
D
Both B and C
Explanation
Lists always maintain insertion order. Starting from Python 3.7, dictionaries (dict) also
maintain the insertion order of keys by default.
Question 5
What is the purpose of the continue statement in a while loop?
A
Terminates the loop.
B
Skips the remaining code in the loop and moves to the next iteration.
C
Exits the loop prematurely.
D
Breaks out of the loop entirely.
Explanation
The continue statement skips the remaining code inside the loop and moves to the
next iteration.
Question 1
What is the output of the following program?
tup = (1, 2, 3, 4)
[Link]( (5, 6, 7) )
print(len(tup))
A
1
B
2
C
5
D
Error
Explanation
In this case, an exception will be thrown as tuples are immutable and don’t have an
append method.
Question 2
What is the output of the following program?
tup = {}
tup[(1,2,4)] = 8
tup[(4,2,1)] = 10
tup[(1,2)] = 12
sum1 = 0
for k in tup:
sum1 += tup[k]
print(len(tup) + sum1)
A
30
B
31
C
32
D
33
Explanation
{} creates an empty dictionary, not a tuple. The dictionary tup is assigned three
key-value pairs where the keys are tuples: {(1,2,4): 8, (4,2,1): 10, (1,2): 12}.
Therefore dictionary length is 3.
Summing the values: 8 + 10 + 12 = 30. Adding the dictionary length (3 + 30)
hence, the final output is 33.
Question 3
What is the output of the following program?
tup1 = (1, 2, 4, 3)
tup2 = (1, 2, 3, 4)
print(tup1 < tup2)
A
Error
B
True
C
False
D
Unexpected
Explanation
In this case elements will be compared one by one. So, when it compares 4 with 3 it
will return False.
Question 4
What is the output of the following program?
tup = (1, 2, 3)
print(2 * tup)
A
(1, 2, 3, 1, 2, 3)
B
(1, 2, 3, 4, 5, 6)
C
(3, 6, 9)
D
Error
Explanation
The ‘*’ operator is used to concatenate tuples.
Question 5
What is the output of the following program?
tup=("Check")*3
print(tup)
A
Unexpected
B
3Check
C
CheckCheckCheck
D
Syntax Error
Explanation
Here (“Check”) will be treated as is a string not a tuple as there is no comma after the
element.