Python MCQ Questions

Python MCQ Questions are designed to test your knowledge and understanding of Python programming concepts. Covering a wide range of topics, including Python syntax, data types, functions, libraries, object-oriented programming, and more, these multiple-choice questions are perfect for learners at all levels. Whether you're preparing for exams, interviews, or simply improving your Python skills, these MCQs will help reinforce key concepts, enhance problem-solving abilities, and build confidence in Python programming.

Q1. What is the correct file extension for Python files?

Correct Answer: C

Python files use the .py extension.

Q2. What will be the output of the following Python code? ```python print(2 ** 3) ```

Correct Answer: C

The ** operator is used for exponentiation, so 2 ** 3 equals 8.

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

Correct Answer: D

In Python, a dictionary is a data structure, but it is not a primitive data type.

Q4. What is the result of the following code snippet? ```python x = [1, 2, 3] x.append([4, 5]) print(len(x)) ```

Correct Answer: B

The append() method adds the entire list [4, 5] as one element, so the length becomes 4.

Q5. Which of the following methods can be used to remove an item from a Python list?

Correct Answer: D

Both remove() and pop() methods are used to remove items from a list.

Q6. In Python, which of the following is used to define a block of code?

Correct Answer: C

Python uses indentation to define the scope of loops, functions, classes, etc.

Q7. What is the output of the following code? ```python x = "Python" print(x[0:2]) ```

Correct Answer: A

The slicing x[0:2] extracts the characters from index 0 to 1.

Q8. Which Python module can be used to generate random numbers?

Correct Answer: A

The random module is used for generating random numbers in Python.

Q9. What is the output of the following code? ```python x = "Hello, World!" print(x[7:]) ```

Correct Answer: B

The slice x[7:] extracts the substring starting from index 7, which is "World!".

Q10. How do you start a comment in Python?

Correct Answer: B

Python comments are initiated with the # symbol.

Q11. Which of the following is the correct extension for Python files?

Correct Answer: A

Python files use the .py extension.

Q12. Which keyword is used to define a function in Python?

Correct Answer: A

def is used to define functions in Python.

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

Correct Answer: B

# is used for single-line comments.

Q14. Which data type is used to store text in Python?

Correct Answer: B

str (string) is used to store text.

Q15. Which of these is a valid Python variable name?

Correct Answer: B

Variable names can start with a letter or underscore.

Q16. Which function prints output to the screen in Python?

Correct Answer: B

print() outputs data to the console.

Q17. Which of the following is NOT a Python data type?

Correct Answer: C

Python does not have a separate Character type; single characters are strings.

Q18. Which operator is used for exponentiation in Python?

Correct Answer: B

** is used to raise a number to a power.

Q19. Which loop executes as long as a condition is True?

Correct Answer: B

while loop executes while the condition is True.

Q20. Which keyword is used to handle exceptions in Python?

Correct Answer: B

try is used to handle exceptions; except catches them.

Q21. Which of these is used to create a list in Python?

Correct Answer: B

Lists are defined using square brackets [].

Q22. Which function returns the length of a string or list?

Correct Answer: C

The len() function is used in Python to determine the number of elements in a sequence or collection. It can be used with strings, lists, tuples, dictionaries, sets, and other objects that support length. For a string, len() returns the total number of characters, while for a list, it returns the total number of elements. For example, len("Python") returns 6, and len([10, 20, 30]) returns 3. Therefore, among the given options, C. len() is the correct answer.

Q23. Which keyword is used to create a class in Python?

Correct Answer: A

class is used to define a class.

Q24. Which method adds an element to the end of a list?

Correct Answer: B

append() adds a single element at the end of a list.

Q25. Which of the following is used for multi-line comments in Python?

Correct Answer: C

Triple quotes """ """ are used for multi-line comments or docstrings.

Q26. Which function converts a string to an integer?

Correct Answer: A

int() converts a string or float to an integer.

Q27. Which operator checks equality of two values in Python?

Correct Answer: B

== checks if two values are equal.

Q28. Which keyword is used to exit a loop prematurely?

Correct Answer: C

break is used to exit a loop immediately.

Q29. Which function reads input from the user?

Correct Answer: B

input() reads data from the user via console.

Q30. Which method converts all characters in a string to uppercase?

Correct Answer: A

upper() converts all letters in a string to uppercase.

Latest Blogs