Python - MCQ Questions and Answers

Test your knowledge of Python with these interactive multiple-choice questions.

« First 1 2 3 4 Last »
« Previous Page Next Page »

1. What is the output of print(type(5/2)) in Python 3?

  • a) <class 'int'>
  • b) <class 'float'>
  • c) <class 'double'>
  • d) <class 'number'>
Answer: B - Division in Python 3 always returns a float (2.5 in this case).

2. Which method is used to add an element at the end of a list?

  • a) append()
  • b) insert()
  • c) add()
  • d) extend()
Answer: A - append() adds a single element to the end: my_list.append(item).

3. What does the __init__ method do in a class?

  • a) Initializes object attributes
  • b) Destroys the object
  • c) Converts object to string
  • d) Imports modules
Answer: A - It's the constructor method called when an object is created.

4. Which operator performs integer division in Python?

  • a) /
  • b) //
  • c) %
  • d) |
Answer: B - // returns the floor value (e.g., 5//2 gives 2).

5. What is the output of print("Hello" * 3)?

  • a) HelloHelloHello
  • b) Hello3
  • c) TypeError
  • d) Hello Hello Hello
Answer: A - Strings can be multiplied by integers for repetition.

6. Which collection is unordered and mutable in Python?

  • a) list
  • b) tuple
  • c) set
  • d) str
Answer: C - Sets are unordered and mutable: {1, 2, 3}.

7. What does range(1, 10, 2) generate?

  • a) [1, 3, 5, 7, 9]
  • b) [1, 2, 3,..., 9]
  • c) [2, 4, 6, 8]
  • d) [0, 1, 2,..., 9]
Answer: A - range(start, stop, step) generates numbers from 1 to 9 (exclusive) with step 2.

8. How do you open a file for reading in Python?

  • a) open("file.txt", "r")
  • b) open("file.txt", "read")
  • c) open("file.txt", "w")
  • d) open("file.txt")
Answer: A - The "r" mode opens a file for reading (default mode if omitted).

9. Which decorator is used to define a class method?

  • a) @classmethod
  • b) @staticmethod
  • c) @method
  • d) @class
Answer: A - @classmethod receives the class (cls) as first argument.

10. What is the output of print([i for i in range(5) if i%2==0])?

  • a) [0, 2, 4]
  • b) [1, 3]
  • c) [0, 1, 2, 3, 4]
  • d) [2, 4]
Answer: A - This list comprehension filters even numbers from 0 to 4.

11. Which function converts a string to lowercase?

  • a) lower()
  • b) casefold()
  • c) tolower()
  • d) Both A and B
Answer: D - Both lower() and casefold() convert strings to lowercase (casefold is more aggressive).

12. What is the output of print("Python"[1:4])?

  • a) Pyt
  • b) yth
  • c) ytho
  • d) Python
Answer: B - Slicing from index 1 to 4 (exclusive) gives 'yth'.

13. Which module is used for working with regular expressions?

  • a) re
  • b) regex
  • c) pyre
  • d) string
Answer: A - The re module provides regex support: import re.

14. How do you create a virtual environment in Python?

  • a) python -m venv myenv
  • b) virtualenv myenv
  • c) pip create venv myenv
  • d) Both A and B
Answer: D - Both venv (built-in) and virtualenv (third-party) can create virtual environments.

15. What is the purpose of __name__ == "__main__"?

  • a) Checks if the script is run directly
  • b) Imports a module
  • c) Defines a class
  • d) Creates a generator
Answer: A - It allows code to execute only when the script is run directly (not imported).

16. Which method is used to remove a key from a dictionary?

  • a) pop()
  • b) remove()
  • c) delete()
  • d) discard()
Answer: A - my_dict.pop(key) removes the key and returns its value.

17. What does lambda x: x**2 create?

  • a) A function that squares its input
  • b) A list comprehension
  • c) A generator expression
  • d) A dictionary
Answer: A - Lambda creates anonymous functions: square = lambda x: x**2.

18. Which exception is raised when a key is not found in a dictionary?

  • a) KeyError
  • b) ValueError
  • c) IndexError
  • d) LookupError
Answer: A - Accessing a non-existent key raises KeyError.

19. What is the output of print("Hello".upper())?

  • a) HELLO
  • b) hello
  • c) Hello
  • d) hELLO
Answer: A - upper() converts all characters to uppercase.

20. Which Python built-in is used to iterate over a sequence with indexes?

  • a) enumerate()
  • b) range()
  • c) zip()
  • d) iter()
Answer: A - for i, item in enumerate(my_list): provides index-value pairs.
« First 1 2 3 4 Last »
« Previous Page Next Page »

Learn Computer Skills with PCBooks

Master computer skills with PCBooks! Explore interactive tutorials, MCQ tests, and online exams for programming, web development, and IT fundamentals. Perfect for beginners and advanced learners.

Online MCQ Tests for Programming

Practice programming MCQ questions and improve your coding skills with our online quizzes. Whether you're learning Python, Java, or C programming, PCBooks has you covered.

Free Web Development Tutorials

Learn HTML, CSS, and JavaScript with our free web development tutorials. Test your knowledge with interactive quizzes and online exams.