🧠 Python Quiz & Practice

Test your Python knowledge with interactive quizzes covering fundamentals, data structures, OOP, and more

📚 What's Included

  • 30+ interactive multiple-choice questions
  • Instant feedback with detailed explanations
  • Code-based questions to test practical understanding
  • Progressive difficulty from beginner to advanced
  • Track your progress and score
  • Comprehensive coverage of Python fundamentals
30
Total Questions
0
Answered
0
Correct
0%
Score
0%

Python Fundamentals Quiz

1. What is the output of: print(type([]))
(a) <class 'list'>
(b) <class 'array'>
(c) <class 'tuple'>
(d) <class 'dict'>
Explanation: The empty brackets [] create an empty list. The type() function returns <class 'list'> for list objects.
2. Which of the following is NOT a valid Python variable name?
(a) my_variable
(b) _private
(c) 2fast
(d) variable2
Explanation: Variable names cannot start with a number. "2fast" is invalid, while the others follow Python naming conventions.
3. What does the following code output?
print(10 // 3)
(a) 3.333...
(b) 3
(c) 4
(d) 3.0
Explanation: The // operator performs floor division, which returns the integer quotient without the remainder. 10 // 3 = 3.
4. Which data type is immutable in Python?
(a) List
(b) Dictionary
(c) Tuple
(d) Set
Explanation: Tuples are immutable, meaning their elements cannot be changed after creation. Lists, dictionaries, and sets are mutable.
5. What is the output of: len("Hello\nWorld")
(a) 10
(b) 11
(c) 12
(d) 13
Explanation: \n is a single newline character. "Hello\nWorld" has 5 + 1 + 5 = 11 characters total.
6. What does the following code return?
[1, 2, 3] + [4, 5]
(a) [1, 2, 3, 4, 5]
(b) [5, 7]
(c) [[1, 2, 3], [4, 5]]
(d) Error
Explanation: The + operator concatenates lists, combining all elements into a single list: [1, 2, 3, 4, 5].
7. Which keyword is used to define a function in Python?
(a) function
(b) def
(c) func
(d) define
Explanation: Python uses the "def" keyword to define functions: def my_function():
8. What is the output of: bool("")
(a) True
(b) False
(c) ""
(d) None
Explanation: An empty string is falsy in Python, so bool("") returns False. Non-empty strings are truthy.
9. What does the following code output?
x = [1, 2, 3] y = x y.append(4) print(len(x))
(a) 3
(b) 4
(c) Error
(d) None
Explanation: y = x creates a reference to the same list, not a copy. When y is modified, x is also affected. len(x) = 4.
10. Which method removes and returns the last element of a list?
(a) remove()
(b) pop()
(c) delete()
(d) del()
Explanation: The pop() method removes and returns the last element. pop(index) can remove at a specific index.

Data Structures & OOP Quiz

11. What is the correct way to create a dictionary?
(a) d = {}
(b) d = []
(c) d = ()
(d) d = <>
Explanation: Empty curly braces {} create an empty dictionary. [] creates a list, () creates a tuple.
12. What does the following list comprehension produce?
[x**2 for x in range(5) if x % 2 == 0]
(a) [0, 4, 16]
(b) [0, 1, 4, 9, 16]
(c) [1, 9]
(d) [0, 2, 4]
Explanation: This filters even numbers (0, 2, 4) and squares them: 0²=0, 2²=4, 4²=16. Result: [0, 4, 16].
13. What is the purpose of the __init__ method in a class?
(a) Constructor/initializer
(b) Destructor
(c) String representation
(d) Comparison method
Explanation: __init__ is the constructor/initializer method that runs when a new object is created.
14. What does the "self" parameter represent in a class method?
(a) The instance of the class
(b) The class itself
(c) A global variable
(d) A keyword
Explanation: "self" refers to the instance of the class, allowing access to instance variables and methods.
15. What is the output of: {1, 2, 3} & {2, 3, 4}
(a) {1, 2, 3, 4}
(b) {2, 3}
(c) {1, 4}
(d) Error
Explanation: The & operator performs set intersection, returning common elements: {2, 3}.
16. What does dict.get('key', 'default') do if 'key' doesn't exist?
(a) Returns 'default'
(b) Raises KeyError
(c) Returns None
(d) Returns empty string
Explanation: The get() method returns the default value when the key doesn't exist, avoiding KeyError.
17. Which of these is NOT an OOP principle?
(a) Encapsulation
(b) Inheritance
(c) Compilation
(d) Polymorphism
Explanation: The four OOP principles are: Encapsulation, Inheritance, Polymorphism, and Abstraction. Compilation is not an OOP principle.
18. What is the output of: "Python"[1:4]
(a) "yth"
(b) "Pyt"
(c) "ytho"
(d) "Pyth"
Explanation: String slicing [1:4] extracts characters from index 1 to 3 (not including 4): "yth".
19. What does the zip() function do?
(a) Compresses files
(b) Combines multiple iterables
(c) Sorts a list
(d) Encrypts data
Explanation: zip() combines multiple iterables element-by-element into tuples: zip([1,2], ['a','b']) → [(1,'a'), (2,'b')].
20. What is a lambda function?
(a) A named function
(b) An anonymous function
(c) A class method
(d) A built-in function
Explanation: Lambda functions are anonymous (unnamed) functions defined with the lambda keyword: lambda x: x * 2.

Advanced Concepts Quiz

21. What is a decorator in Python?
(a) A function that modifies another function
(b) A design pattern
(c) A class attribute
(d) A module
Explanation: Decorators are functions that modify the behavior of other functions using the @decorator syntax.
22. What does *args allow in a function?
(a) Variable number of positional arguments
(b) Variable number of keyword arguments
(c) Only one argument
(d) No arguments
Explanation: *args allows a function to accept any number of positional arguments as a tuple.
23. What is the difference between 'is' and '=='?
(a) 'is' checks identity, '==' checks value
(b) They are the same
(c) 'is' is for numbers, '==' for strings
(d) 'is' is deprecated
Explanation: 'is' checks if two variables refer to the same object (identity), while '==' checks if values are equal.
24. What does yield do in a function?
(a) Returns a value without ending the function
(b) Ends the function immediately
(c) Raises an exception
(d) Creates a class
Explanation: yield creates a generator, returning values one at a time while preserving function state between calls.
25. What is a context manager?
(a) Manages resource setup and cleanup
(b) A database manager
(c) A variable scope
(d) A threading tool
Explanation: Context managers (used with 'with' statement) handle resource setup and cleanup automatically, like file operations.
26. What is the GIL in Python?
(a) Global Interpreter Lock
(b) Graphics Interface Library
(c) General Input/output Loop
(d) Gateway Integration Layer
Explanation: GIL (Global Interpreter Lock) is a mutex that prevents multiple threads from executing Python bytecode simultaneously.
27. What does the enumerate() function return?
(a) Index-value pairs
(b) Only indices
(c) Only values
(d) A dictionary
Explanation: enumerate() returns an iterator of tuples containing index and value: enumerate(['a','b']) → [(0,'a'), (1,'b')].
28. What is list slicing [::−1] used for?
(a) Reversing the list
(b) Sorting the list
(c) Removing duplicates
(d) Finding maximum
Explanation: The slice [::−1] reverses a list by stepping backward through all elements.
29. What is the purpose of __str__ method?
(a) String representation for users
(b) Create a new string
(c) Compare strings
(d) Initialize string
Explanation: __str__ defines the user-friendly string representation of an object, used by print() and str().
30. What does the pass statement do?
(a) Does nothing (placeholder)
(b) Skips to next iteration
(c) Exits the program
(d) Raises an error
Explanation: pass is a null statement that does nothing, used as a placeholder in empty code blocks.
🎉 Quiz Complete! Check your results above. Review the explanations for any questions you missed to reinforce your understanding.
💡 Study Tips:
  • Review questions you got wrong and understand why
  • Practice coding the concepts you're unsure about
  • Retake the quiz to track improvement
  • Combine this with hands-on coding practice
  • Check the Python documentation for deeper understanding