Ready to sharpen your Python skills? Dive into our interactive quiz practice questions, designed to test your knowledge from the fundamentals to more advanced concepts. Whether you're a beginner looking to solidify your understanding or an experienced developer aiming to refresh your memory, these quizzes offer a fantastic way to learn and improve. Each question is crafted to challenge you and reinforce key Python principles. Get ready to code, learn, and master Python, one question at a time! Good luck!

Chapter 1: Introduction to Python

1. Who created Python?

  • (a) Guido van Rossum
  • (b) Larry Wall
  • (c) Yukihiro Matsumoto
  • (d) Bjarne Stroustrup

2. Which of the following is NOT a feature of Python?

  • (a) Interpreted language
  • (b) Statically typed
  • (c) Object-oriented
  • (d) High-level language

3. What is the standard file extension for Python code?

  • (a) .pyt
  • (b) .pt
  • (c) .py
  • (d) .px

Chapter 2: Variables and Data Types

4. Which of the following is a valid variable name in Python?

  • (a) 2my_var
  • (b) my-var
  • (c) _my_var
  • (d) global

5. What is the data type of the value 10.5?

  • (a) int
  • (b) str
  • (c) float
  • (d) bool

6. What will be the output of print(type(True))?

  • (a) <class 'int'>
  • (b) <class 'bool'>
  • (c) <class 'str'>
  • (d) <class 'NoneType'>

7. How do you create a multiline string in Python?

  • (a) Using single quotes: 'This is a multiline string'
  • (b) Using double quotes: "This is a multiline string"
  • (c) Using triple quotes: """This is a multiline string"""
  • (d) Using backticks: `This is a multiline string`

Chapter 3: Operators

8. What is the result of the expression 10 // 3?

  • (a) 3.33
  • (b) 3
  • (c) 1
  • (d) 0

9. Which operator is used for exponentiation?

  • (a) ^
  • (b) **
  • (c) *
  • (d) %

10. What is the value of x after: x = 5; x += 2?

  • (a) 2
  • (b) 5
  • (c) 7
  • (d) 10

11. Which of the following is a logical operator?

  • (a) &
  • (b) or
  • (c) |
  • (d) in

Chapter 4: Control Flow - Conditional Statements

12. What keyword is used for conditional execution in Python if a condition is true?

  • (a) then
  • (b) if
  • (c) when
  • (d) case

13. What does the elif keyword signify?

  • (a) A nested if statement
  • (b) The end of an if statement
  • (c) Else if, a condition to check if the previous if or elif conditions were false
  • (d) An optional part of an if statement that always executes

14. Consider: if x > 10: print("A") elif x > 5: print("B") else: print("C"). If x = 7, what is printed?

  • (a) A
  • (b) B
  • (c) C
  • (d) A and B

Chapter 5: Control Flow - Loops

15. Which type of loop is used when the number of iterations is known beforehand?

  • (a) while loop
  • (b) for loop
  • (c) do-while loop (Python doesn't have a direct do-while)
  • (d) infinite loop

16. What does the break statement do in a loop?

  • (a) Skips the current iteration and continues with the next
  • (b) Exits the loop immediately
  • (c) Restarts the loop from the beginning
  • (d) Pauses the loop execution

17. What will range(5) produce?

  • (a) [0, 1, 2, 3, 4, 5]
  • (b) [1, 2, 3, 4, 5]
  • (c) [0, 1, 2, 3, 4]
  • (d) [5]

18. Consider: i = 0; while i < 3: print(i); i += 1. What is the last number printed?

  • (a) 0
  • (b) 1
  • (c) 2
  • (d) 3

Chapter 6: Data Structures - Lists

19. How do you create an empty list in Python?

  • (a) list = {}
  • (b) list = []
  • (c) list = ()
  • (d) list = new List()

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

  • (a) insert()
  • (b) add()
  • (c) append()
  • (d) extend()

21. If my_list = [10, 20, 30, 40], what is my_list[1:3]?

  • (a) [10, 20]
  • (b) [20, 30]
  • (c) [20, 30, 40]
  • (d) [10, 20, 30]

22. Which of the following is TRUE about Python lists?

  • (a) They are immutable.
  • (b) They can only store elements of the same data type.
  • (c) They are ordered and mutable.
  • (d) They are accessed by key-value pairs.

Chapter 7: Data Structures - Tuples

23. Which of the following creates a tuple?

  • (a) my_tuple = [1, 2]
  • (b) my_tuple = {1, 2}
  • (c) my_tuple = (1, 2)
  • (d) my_tuple = <1, 2>

24. What is a key characteristic of tuples compared to lists?

  • (a) Tuples are mutable.
  • (b) Tuples are immutable.
  • (c) Tuples can only store integers.
  • (d) Tuples are unordered.

25. What happens if you try to change an element in a tuple after creation?

  • (a) The element is changed.
  • (b) It raises a TypeError.
  • (c) It raises an IndexError.
  • (d) It converts the tuple to a list.

Chapter 8: Data Structures - Dictionaries

26. How is an empty dictionary created?

  • (a) my_dict = []
  • (b) my_dict = ()
  • (c) my_dict = {} (Note: {} also creates an empty set, context matters or use dict())
  • (d) my_dict = new Dictionary()

27. How do you access a value in a dictionary?

  • (a) By its index
  • (b) By its key
  • (c) Using the get_value() method
  • (d) By its order of insertion

28. If my_dict = {"name": "Alice", "age": 30}, what is my_dict.get("city", "Unknown")?

  • (a) Alice
  • (b) 30
  • (c) Unknown
  • (d) It raises a KeyError

29. Which method is used to add a new key-value pair to a dictionary or update an existing key?

  • (a) add()
  • (b) append()
  • (c) my_dict[key] = value
  • (d) insert()

Chapter 9: Data Structures - Sets

30. How do you create an empty set correctly to avoid confusion with an empty dictionary?

  • (a) my_set = {}
  • (b) my_set = []
  • (c) my_set = set()
  • (d) my_set = ()

31. What is a primary characteristic of a Python set?

  • (a) Ordered and mutable
  • (b) Ordered and immutable
  • (c) Unordered and contains unique elements
  • (d) Unordered and can contain duplicate elements

32. Which operation returns the common elements between two sets?

  • (a) Union (|)
  • (b) Difference (-)
  • (c) Intersection (&)
  • (d) Symmetric Difference (^)

Chapter 10: Functions

33. What keyword is used to define a function in Python?

  • (a) fun
  • (b) define
  • (c) def
  • (d) function

34. What is the purpose of the return statement in a function?

  • (a) To print a value to the console
  • (b) To stop the execution of the function and optionally send a value back to the caller
  • (c) To define a variable within the function
  • (d) To import a module

35. A function defined with def greet(name="Guest"): ... is using:

  • (a) A required argument
  • (b) A keyword argument
  • (c) A default argument value
  • (d) A variable-length argument

36. What does *args in a function definition allow?

  • (a) Passing a fixed number of keyword arguments
  • (b) Passing a variable number of non-keyword (positional) arguments
  • (c) Passing a variable number of keyword arguments
  • (d) Passing a single list argument

Chapter 11: Modules and Packages

37. What keyword is used to bring external code into your current Python script?

  • (a) include
  • (b) import
  • (c) use
  • (d) load

38. If you import a module as import math, how do you access its sqrt function?

  • (a) sqrt()
  • (b) math.sqrt()
  • (c) math->sqrt()
  • (d) import.sqrt()

39. What is the purpose of the __init__.py file in a Python package directory?

  • (a) It contains the main executable code for the package.
  • (b) It's a script that installs the package.
  • (c) It signifies that the directory should be treated as a package and can contain package initialization code.
  • (d) It's a documentation file for the package.

Chapter 12: File Handling

40. Which function is used to open a file in Python?

  • (a) file_open()
  • (b) open_file()
  • (c) open()
  • (d) read_file()

41. What mode is used to open a file for writing, which overwrites the file if it exists?

  • (a) "r"
  • (b) "a"
  • (c) "w"
  • (d) "x"

42. Which method is commonly used to read the entire content of a file into a single string?

  • (a) readline()
  • (b) readlines()
  • (c) read()
  • (d) fetch()

43. What is the recommended way to work with files to ensure they are properly closed, even if errors occur?

  • (a) Using a try...except block and manually calling close()
  • (b) Using the with open(...) as f: statement
  • (c) Relying on Python's automatic garbage collection to close files
  • (d) Opening files only in read mode

Chapter 13: Exception Handling

44. What block of code is used to catch and handle potential errors in Python?

  • (a) if...else
  • (b) for...in
  • (c) try...except
  • (d) do...while

45. Which keyword is used to specify the code that will run regardless of whether an exception occurred or not?

  • (a) else (in a try block)
  • (b) catch
  • (c) finally
  • (d) ensure

46. What type of error occurs if you try to access an index that is out of bounds for a list?

  • (a) TypeError
  • (b) ValueError
  • (c) IndexError
  • (d) NameError

47. How can you raise a custom exception in Python?

  • (a) Using the throw keyword
  • (b) Using the error keyword
  • (c) Using the raise keyword followed by an exception class or instance
  • (d) Using the exception keyword

Chapter 14: Object-Oriented Programming (OOP) - Basics

48. What is a blueprint for creating objects in Python?

  • (a) Function
  • (b) Module
  • (c) Class
  • (d) Variable

49. What is an instance of a class called?

  • (a) Method
  • (b) Attribute
  • (c) Object
  • (d) Constructor

50. What is the name of the special method that initializes an object when it's created?

  • (a) start()
  • (b) __init__()
  • (c) constructor()
  • (d) initialize()

51. What does the self parameter in a class method refer to?

  • (a) The class itself
  • (b) The parent class
  • (c) The instance of the class on which the method is called
  • (d) A global variable

Chapter 15: OOP - Inheritance

52. What is the concept of a new class deriving properties and methods from an existing class called?

  • (a) Encapsulation
  • (b) Polymorphism
  • (c) Inheritance
  • (d) Abstraction

53. If class B inherits from class A, then class A is called the:

  • (a) Subclass or Derived class
  • (b) Superclass or Base class
  • (c) Child class
  • (d) Instance class

54. How do you call a method from the parent class within a method of a child class?

  • (a) parent.method_name()
  • (b) super().method_name()
  • (c) base.method_name()
  • (d) self.parent.method_name()

Chapter 16: OOP - Polymorphism & Encapsulation

55. What OOP concept allows objects of different classes to respond to the same method call in different ways?

  • (a) Inheritance
  • (b) Polymorphism
  • (c) Encapsulation
  • (d) Abstraction

56. Bundling data (attributes) and methods that operate on the data within a single unit (class) is known as:

  • (a) Inheritance
  • (b) Polymorphism
  • (c) Encapsulation
  • (d) Modularity

57. In Python, prefixing an attribute name with a double underscore (__attribute) results in:

  • (a) Making the attribute strictly private and inaccessible
  • (b) Name mangling, to make it harder (but not impossible) to access directly from outside
  • (c) Making the attribute public
  • (d) Declaring the attribute as static

Chapter 17: List Comprehensions & Generators

58. What is the primary benefit of using list comprehensions?

  • (a) They make lists immutable.
  • (b) They provide a concise way to create lists.
  • (c) They can only be used with numerical data.
  • (d) They execute faster than for loops for all scenarios.

59. Which of the following is a correct list comprehension to create a list of squares of numbers from 0 to 4?

  • (a) [x^2 for x in range(5)]
  • (b) [x**2 for x in range(5)]
  • (c) {x**2 for x in range(5)}
  • (d) (x**2 for x in range(5))

60. What does a generator function use to produce a sequence of values lazily?

  • (a) return statement for each value
  • (b) yield statement
  • (c) generate statement
  • (d) produce statement

Chapter 18: Lambdas, Map, Filter, Reduce

61. What is a lambda function in Python?

  • (a) A multi-line function
  • (b) A small, anonymous (unnamed) inline function
  • (c) A function that can only be used for mathematical operations
  • (d) A function defined within a class

62. The map() function is used to:

  • (a) Filter elements from an iterable based on a condition.
  • (b) Apply a function to each item in an iterable and return a map object (iterator) of the results.
  • (c) Reduce an iterable to a single cumulative value by applying a function.
  • (d) Create a list of anonymous functions.

63. Which function would you use to create a new list containing only the even numbers from an existing list numbers?

  • (a) map(lambda x: x % 2 == 0, numbers)
  • (b) filter(lambda x: x % 2 == 0, numbers)
  • (c) reduce(lambda x, y: x if x % 2 == 0 else y, numbers)
  • (d) select(lambda x: x % 2 == 0, numbers)

Chapter 19: Regular Expressions

64. Which module in Python is used for working with regular expressions?

  • (a) regex
  • (b) re
  • (c) string
  • (d) pattern

65. What does the re.search() method do?

  • (a) Returns a match object if the pattern is found anywhere in the string, otherwise None.
  • (b) Returns a match object only if the pattern is found at the beginning of the string.
  • (c) Returns a list of all non-overlapping matches in the string.
  • (d) Splits the string by the occurrences of the pattern.

66. In a regular expression, what does \d typically match?

  • (a) Any whitespace character
  • (b) Any non-digit character
  • (c) Any digit character (0-9)
  • (d) The literal character 'd'

Chapter 20: Working with Dates and Times

67. Which module is primarily used for working with dates and times in Python?

  • (a) time
  • (b) date
  • (c) datetime
  • (d) calendar

68. How can you get the current date and time using the datetime module?

  • (a) datetime.now()
  • (b) datetime.current()
  • (c) datetime.today_and_time()
  • (d) datetime.get_current_datetime()

69. The strftime() method is used to:

  • (a) Parse a string into a datetime object.
  • (b) Format a datetime object into a string representation.
  • (c) Calculate the difference between two dates.
  • (d) Get the current timestamp.