Python is one of the most popular programming languages because it’s easy and versatile. Whether you are just starting to learn or want to improve your Python knowledge, practicing MCQs is a great way to reinforce your learning.
We have gathered here 50 MCQs in Python that are fundamental and intermediate concepts including syntax, data types, functions, object-oriented programming, and so on. We will add an explanation for each question to help you understand the concept behind the answer.
1. Which of the following is the correct extension of the Python file?
- A) .pyt
- B) .py
- C) .python
- D) .pyo
Answer: B) .py
Python scripts are saved with the .py
extension, indicating that the file contains Python code.
2. Which of the following is the correct syntax to output “Hello World” in Python?
- A) print(“Hello World”)
- B) echo(“Hello World”)
- C) printf(“Hello World”)
- D) cout << “Hello World”
Answer: A) print(“Hello World”)
The print()
function in Python is used to display output to the console. It is the correct way to print text like “Hello World.”
3. Which of the following data types are immutable in Python?
- A) List
- B) Set
- C) Dictionary
- D) Tuple
Answer: D) Tuple
In Python, tuples are immutable, meaning their elements cannot be changed after they are created. Lists, sets, and dictionaries are mutable.
4. How do you create a variable that stores a string in Python?
- A) var name = “John”
- B) name = ‘John’
- C) string name = “John”
- D) string name = ‘John’
Answer: B) name = ‘John’
In Python, strings are created by enclosing the text in either single ('
) or double quotes ("
). The variable assignment follows the syntax variable_name = value
.
5. What will be the output of the following code?
pythonCopy codex = 5
y = 2
print(x // y)
- A) 2.5
- B) 3
- C) 2
- D) 2.0
Answer: C) 2
The //
operator in Python performs floor division, which returns the largest integer less than or equal to the result. Here, 5 // 2
equals 2
.
6. Which of the following is used to define a block of code in Python?
- A) Curly Braces
{}
- B) Indentation
- C) Parentheses
()
- D) Square Brackets
[]
Answer: B) Indentation
In Python, indentation is used to define blocks of code. It is mandatory and determines the grouping of statements within loops, functions, and classes.
7. What is the result of the following expression in Python?
pythonCopy codex = "Hello"
y = "World"
print(x + y)
- A) HelloWorld
- B) Hello World
- C) “Hello” + “World”
- D) Error
Answer: A) HelloWorld
The +
operator in Python is used for string concatenation. It combines the two strings "Hello"
and "World"
into "HelloWorld"
.
8. Which method is used to convert a string to lowercase in Python?
- A) lower()
- B) toLower()
- C) down()
- D) makeLower()
Answer: A) lower()
The lower()
method is used to convert all characters in a string to lowercase.
9. How do you create a function in Python?
- A) def my_function():
- B) function my_function():
- C) create my_function():
- D) def function my_function():
Answer: A) def my_function():
Functions in Python are defined using the def
keyword, followed by the function name and parentheses.
10. What will be the output of the following code?
pythonCopy codex = [1, 2, 3]
x.append(4)
print(x)
- A) [1, 2, 3, 4]
- B) [1, 2, 3]
- C) Error
- D) [4, 1, 2, 3]
Answer: A) [1, 2, 3, 4]
The append()
method adds an item to the end of the list, so the list becomes [1, 2, 3, 4]
.
11. What is the correct way to import the math module in Python?
- A) import math
- B) include math
- C) using math
- D) import module math
Answer: A) import math
To use the functions from the math module, you must import it with the import math
statement.
12. What will the following code output?
pythonCopy codex = [1, 2, 3, 4]
x.pop()
print(x)
- A) [1, 2, 3]
- B) [1, 2, 3, 4]
- C) [4, 3, 2, 1]
- D) Error
Answer: A) [1, 2, 3]
The pop()
method removes the last element of the list. After popping, the list becomes [1, 2, 3]
.
13. What will be the output of the following code?
pythonCopy codex = 5
y = 3
print(x > y and y > 1)
- A) True
- B) False
- C) 5
- D) Error
Answer: A) True
The and
operator returns True
if both conditions are true. Here, 5 > 3
and 3 > 1
are both true, so the result is True
.
14. Which of the following is a mutable data type in Python?
- A) Tuple
- B) List
- C) String
- D) Integer
Answer: B) List
Lists are mutable, meaning their elements can be changed. Tuples, strings, and integers are immutable in Python.
15. Which operator is used to check if two values are equal in Python?
- A) =
- B) ==
- C) !=
- D) ===
Answer: B) ==
The ==
operator is used to check if two values are equal in Python.
16. What is the output of the following code?
pythonCopy codex = 10
if x > 5:
print("Greater than 5")
else:
print("Less than or equal to 5")
- A) Greater than 5
- B) Less than or equal to 5
- C) Error
- D) 10
Answer: A) Greater than 5
The condition x > 5
is true since x
is 10, so the output is "Greater than 5"
.
17. How do you handle exceptions in Python?
- A) using try-except
- B) using try-catch
- C) using if-else
- D) using error-block
Answer: A) using try-except
In Python, exceptions are handled using a try
block followed by an except
block to catch errors.
18. Which of the following is the correct syntax to create a dictionary in Python?
- A) dict = (1: “apple”, 2: “banana”)
- B) dict = {1: “apple”, 2: “banana”}
- C) dict = [1, “apple”, 2, “banana”]
- D) dict = (1, “apple”, 2, “banana”)
Answer: B) dict = {1: “apple”, 2: “banana”}
A dictionary is defined using curly braces {}
with key-value pairs, separated by colons.
19. What is the output of the following code?
pythonCopy codex = [1, 2, 3]
x[1] = 5
print(x)
- A) [1, 2, 3]
- B) [1, 5, 3]
- C) Error
- D) [5, 2, 3]
Answer: B) [1, 5, 3]
The element at index 1 is changed from 2
to 5
. Therefore, the output is [1, 5, 3]
.
20. What is the purpose of the ‘break’ statement in Python?
- A) To skip the current iteration of a loop
- B) To exit from the loop
- C) To return a value
- D) To pause the program
Answer: B) To exit from the loop
The break
statement is used to exit from a loop prematurely, even if the loop’s condition is still true.
21. What is the correct way to define a class in Python?
- A) class MyClass()
- B) MyClass class()
- C) class MyClass:
- D) class = MyClass
Answer: C) class MyClass:
Classes in Python are defined using the class
keyword followed by the class name and a colon.
22. Which of the following is used to check the type of an object in Python?
- A) type()
- B) object_type()
- C) check_type()
- D) class_of()
Answer: A) type()
The type()
function is used to check the type of an object in Python.
23. How can you get the length of a list in Python?
- A) len()
- B) size()
- C) length()
- D) count()
Answer: A) len()
The len()
function returns the length of a list, i.e., the number of elements it contains.
24. Which of the following is the correct syntax to access the first element of a list in Python?
- A) list[1]
- B) list(1)
- C) list{1}
- D) list[0]
Answer: D) list[0]
In Python, indexing starts from 0, so the first element of a list is accessed using list[0]
.
25. Which method is used to add an item to the end of a list in Python?
- A) append()
- B) add()
- C) insert()
- D) extend()
Answer: A) append()
The append()
method adds an item to the end of a list in Python.
(Questions 26 to 50 would continue in a similar format, covering various Python concepts like loops, file handling, modules, etc.)
26. What is the output of the following code?
pythonCopy codex = [1, 2, 3]
y = x.copy()
y[0] = 10
print(x)
- A) [10, 2, 3]
- B) [1, 2, 3]
- C) [10, 2, 3]
- D) Error
Answer: B) [1, 2, 3]
The copy()
method creates a shallow copy of the list. Since the list y
is a separate copy, modifying y
does not affect x
. Therefore, the output is [1, 2, 3]
.
27. How do you remove a specific item from a list in Python?
- A) remove()
- B) delete()
- C) discard()
- D) pop()
Answer: A) remove()
The remove()
method removes the first occurrence of a specified item from a list in Python.
28. Which of the following methods can be used to join two strings in Python?
- A) append()
- B) + operator
- C) join()
- D) merge()
Answer: B) + operator
You can use the +
operator to concatenate two strings in Python.
29. What does the len()
function do in Python?
- A) Returns the data type of an object
- B) Returns the length of a string
- C) Returns the total number of elements in a sequence
- D) Returns the index of an item
Answer: C) Returns the total number of elements in a sequence
The len()
function returns the number of elements in a sequence (e.g., string, list, or tuple).
30. What does the range()
function do in Python?
- A) Generates a sequence of numbers
- B) Returns the maximum value in a list
- C) Converts a list into a tuple
- D) Checks if a number is within a range
Answer: A) Generates a sequence of numbers
The range()
function generates a sequence of numbers, often used in for
loops for iteration.
31. Which of the following is the correct way to define a variable that stores a float value in Python?
- A) float = 10.5
- B) float = 10
- C) 10.5 = float
- D) x = 10.5
Answer: D) x = 10.5
In Python, variables are defined by assigning a value to a variable name, such as x = 10.5
.
32. What will be the output of the following code?
pythonCopy codex = "Python"
y = " is awesome"
print(x + y)
- A) Python is awesome
- B) Pythonisawesome
- C) Error
- D) Python + is awesome
Answer: A) Python is awesome
The +
operator concatenates two strings, resulting in "Python is awesome"
.
33. Which of the following is NOT a valid way to create a set in Python?
- A) set1 = {1, 2, 3}
- B) set2 = set([1, 2, 3])
- C) set3 = {}
- D) set4 = set()
Answer: C) set3 = {}
An empty set is created with set()
, not with {}
, which is used for creating an empty dictionary.
34. What is the correct syntax for defining an infinite loop in Python?
- A) while 1:
- B) for i in 1:
- C) while True:
- D) repeat:
Answer: C) while True:
An infinite loop in Python can be created using while True:
, as True
is a constant that always evaluates to true.
35. Which of the following statements about Python is TRUE?
- A) Python is a compiled language
- B) Python supports both procedural and object-oriented programming
- C) Python does not support functions
- D) Python is case-insensitive
Answer: B) Python supports both procedural and object-oriented programming
Python supports multiple programming paradigms, including procedural and object-oriented programming.
36. What is the result of the following code?
pythonCopy codex = [1, 2, 3]
y = (1, 2, 3)
print(type(x))
print(type(y))
- A) <class ‘list’> <class ‘tuple’>
- B) <class ‘tuple’> <class ‘list’>
- C) <class ‘list’> <class ‘list’>
- D) <class ‘tuple’> <class ‘tuple’>
Answer: A) <class ‘list’> <class ‘tuple’>
The x
variable is a list, and y
is a tuple, so their types will be <class 'list'>
and <class 'tuple'>
.
37. What will be the output of the following code?
pythonCopy codex = [10, 20, 30, 40, 50]
print(x[-2])
- A) 20
- B) 30
- C) 40
- D) 50
Answer: C) 40
In Python, negative indices start counting from the end. x[-2]
refers to the second last element, which is 40
.
38. How can you find the maximum value in a list in Python?
- A) max()
- B) maximum()
- C) getMax()
- D) findMax()
Answer: A) max()
The max()
function returns the maximum value from a list or iterable in Python.
39. What will be the result of the following code?
pythonCopy codex = [1, 2, 3]
x.extend([4, 5])
print(x)
- A) [1, 2, 3, 4, 5]
- B) [1, 2, 3]
- C) [4, 5]
- D) Error
Answer: A) [1, 2, 3, 4, 5]
The extend()
method adds all elements of another iterable (like a list) to the end of the list x
, resulting in [1, 2, 3, 4, 5]
.
40. How do you check if a key exists in a dictionary in Python?
- A) key in dict
- B) dict.has_key(key)
- C) key.exists()
- D) key.contains()
Answer: A) key in dict
You can check if a key exists in a dictionary using the in
operator, e.g., key in dict
.
41. What is the correct way to define a Python class method?
- A) def method(self):
- B) def method():
- C) method(self):
- D) class method(self):
Answer: A) def method(self):
A class method is defined using def method(self):
, where self
refers to the instance of the class.
42. Which of the following operators is used for exponentiation in Python?
- A) **
- B) ^
- C) %%
- D) ***
Answer: A) **
In Python, the **
operator is used for exponentiation. For example, 2 ** 3
equals 8
.
43. How can you create a file in Python?
- A) open(“file.txt”)
- B) open(“file.txt”, “w”)
- C) create(“file.txt”)
- D) open(“file.txt”, “r”)
Answer: B) open(“file.txt”, “w”)
The open()
function is used to create a file in write mode ("w"
), which creates the file if it doesn’t exist.
44. What will be the output of the following code?
pythonCopy codex = 3
y = 5
print(x * y)
- A) 8
- B) 15
- C) 35
- D) 53
Answer: B) 15
The *
operator performs multiplication. The result of 3 * 5
is 15
.
45. What is the correct syntax to define an anonymous function (lambda) in Python?
- A) def lambda x: x * 2
- B) lambda x: x * 2
- C) function(x): x * 2
- D) function lambda(x): x * 2
Answer: B) lambda x: x * 2
A lambda function in Python is defined using the lambda
keyword followed by parameters and an expression.
46. What is the purpose of the continue
statement in a loop?
- A) To exit the loop completely
- B) To skip the current iteration of the loop
- C) To restart the loop
- D) To pause the loop execution
Answer: B) To skip the current iteration of the loop
The continue
statement is used to skip the rest of the code inside the current iteration and move to the next iteration of the loop.
47. Which method is used to find the index of an item in a list?
- A) index()
- B) find()
- C) locate()
- D) search()
Answer: A) index()
The index()
method returns the index of the first occurrence of an item in a list.
48. What does the zip()
function do in Python?
- A) Joins two strings
- B) Combines two or more iterables element-wise
- C) Compresses a file
- D) Zips files into a directory
Answer: B) Combines two or more iterables element-wise
The zip()
function combines iterables (like lists) element-wise into a tuple.
49. Which of the following is NOT a valid way to comment in Python?
- A) # Single-line comment
- B) /* Multi-line comment */
- C) ”’ Multi-line comment ”’
- D) “”” Multi-line comment “””
Answer: B) /* Multi-line comment */
In Python, multi-line comments are created with '''
or """
. The /* */
style is used in languages like C and Java.
50. What will be the output of the following code?
pythonCopy codex = 7
y = 3
print(x % y)
- A) 2
- B) 1
- C) 0
- D) 3
Answer: A) 2
The %
operator returns the remainder of the division. 7 % 3
equals 2
.
Also Read : Basic Computer MCQs with Answers
You may also like to Read This: DBMS MCQs for Exams
As a seasoned content writer specialized in the fitness and health niche, Arun Bhagat has always wanted to promote wellness. After gaining proper certification as a gym trainer with in-depth knowledge of virtually all the information related to it, he exercised his flair for writing interesting, informative content to advise readers on their healthier lifestyle. His topics range from workout routines, nutrition, and mental health to strategies on how to be more fit in general. His writing is informative but inspiring for people to achieve their wellness goals as well. Arun is committed to equipping those he reaches with the insights and knowledge gained through fitness.