Best 100+ Python Multiple Choice Questions With Revision Notes

Python Multiple Choice Questions: Python is most effective language for web development, data science, scientific computing, and artificial intelligence. Python was introduced in 1991, and it was developed by Guido van Rossum.

Python Multiple Choice Questions

Python Multiple Choice Questions With Revision Notes

Python Syntax and Basics

  • Python uses indentation to define code blocks, making it readable and clean.
  • Variables hold data, and you don’t need to declare their types explicitly.
  • Basic data types include integers, floating-point numbers, strings, lists, tuples, and dictionaries.
  • Arithmetic operators (+, -, *, /, %) perform mathematical operations.
  • Comparison operators (==, !=, <, >, <=, >=) compare values.
  • Logical operators (and, or, not) combine conditions.

Control Flow

  • if-elif-else: Used for conditional branching.
  • for loop: Iterates over a sequence (e.g., list, string).
  • while loop: Runs code repeatedly when a condition is true.
  • break and continue: Control loop execution.

Functions

  • Functions are blocks of reusable code defined with the def keyword.
  • They can take parameters (inputs) and return values (outputs).
  • They can accept input parameters and output return values.
  • To send a value back from a function, use return.

Data Structures

  • Lists: Ordered, mutable collections of items.
  • Tuples: Ordered, immutable collections of items.
  • Dictionaries: Unordered collections of key-value pairs.
  • Sets: Unordered collections of unique items.
  • Indexing and slicing: Accessing elements in data structures.

File Handling

  • Python can read and write files using the open() function.
  • Modes include ‘r’ (read), ‘w’ (write), ‘a’ (append), and ‘b’ (binary).
  • Always close files using close() to free resources.

Exception Handling

  • Use try, except, else, and finally to handle exceptions gracefully.
  • Avoid catching broad exceptions unless necessary.

Modules and Libraries

  • Modules are reusable Python files containing functions and variables.
  • Libraries like NumPy, pandas, and Matplotlib extend Python’s capabilities for specific tasks.
  • Import modules using import.

Object-Oriented Programming (OOP)

  • Classes define objects with attributes and methods.
  • Inheritance allows you to build new classes on top of existing ones
  • Encapsulation hides implementation details with access control.

List Comprehensions

  • A concise way to create lists based on existing lists.
  • Simplifies code and makes it more readable.

Virtual Environments:

  • Use virtual environments to isolate project dependencies using virtualenv or venv.

Testing and Debugging

  • Write unit tests using the unittest module.
  • Use debugging tools like print statements and debuggers (e.g., pdb) to identify and fix issues.

Concurrency and Parallelism

  • Python supports concurrent programming using threads and parallel programming using processes.
  1. What do Python keywords mean?
    a. Unique names for variables
    b. Reserved words with special meanings
    c. User-defined functions
    d. Data types
  1. What is the purpose of a comment in Python?
    a. To execute code
    b. To improve program performance
    c. To provide explanations and documentation
    d. To define variables
  1. Which of the following is not an acceptable Python variable name?
    a. my_var_1
    b. 123var
    c. _variable
    d. myVar
  1. Which Python data type is mutable?
    a. Integer
    b. Float
    c. Tuple
    d. List
  1. What does Python’s type conversion mean?
    a. Changing the data type of a variable
    b. Converting integers to floats
    c. Converting strings to lists
    d. Changing variable names
  1. Which Python function is used to read user input?
    a. input()
    b. read()
    c. print()
    d. get()
  1. What does Python’s the % operator do?
    a. Exponentiation
    b. Division
    c. Modulus (remainder)
    d. Floor division
  1. What is a namespace in Python?
    a. A region of memory used for data storage
    b. A data type
    c. A variable name
    d. A container for organizing code
  1. Which statement allows a block of code to only run if a certain condition is met?
    a. for
    b. while
    c. if
    d. else
  1. What does a for loop in Python primarily do?
    a. Defining functions
    b. Making decisions based on conditions
    c. Iterating over a sequence of elements
    d. Handling exceptions
  1. What keyword does Python use to define a function?
    a. def
    b. function
    c. define
    d. func
  1. What are arguments means in a Python function?
    a. Descriptions of the function’s purpose
    b. Values passed to the function when it’s called
    c. Comments within the function
    d. Variable names
  1. What is recursion in Python?
    a. A function that calls itself
    b. A loop that runs forever
    c. A data structure
    d. A type of exception
  1. ___________________ is known as anonymous function in Python?
    a. Lambda function
    b. Alpha function
    c. Omega function
    d. Beta function
  1. Why python if statements used in python.
    a. To create loops
    b. To execute code conditionally
    c. To define functions
    d. To import modules
  1. In Python which keyword is used to check multiple conditions in an if statement?
    a. elif
    b. else
    c. while
    d. for
  1. What is the primary purpose of a for loop in Python?
    a. Defining functions
    b. Making decisions based on conditions
    c. Iterating over a sequence of elements
    d. Handling exceptions
  1. How do you iterate through the items in a list called “my_list” in a for loop?
    a. for item in my_list:
    b. for index in my_list:
    c. for i in range(my_list):
    d. for item in range(my_list):
  1. What is the primary distinction between a Python while loop and a for loop?
    a. A for loop is used for counting, while a while loop is used for iteration.
    b. A for loop always runs forever.
    c. A while loop is used for iteration, while a for loop is used for counting.
    d. There is no difference; they are interchangeable.
  1. What does Python’s break statement do?
    a. Terminates the program
    b. Exits the current loop prematurely
    c. Skips the current iteration and continues with the next
    d. Halts the execution of the code block
  1. What does Python’s continue statement do?
    a. Exits the program
    b. Exits the current loop prematurely
    c. Skips the current iteration and continues with the next
    d. Halts the execution of the code block
  1. What does the pass statement do in Python?
    a. Exits the program
    b. Exits the current loop prematurely
    c. Skips the current iteration and continues with the next
    d. Acts as a placeholder and does nothing
  1. When the Python pass statement used?
    a. To exit a loop
    b. To skip an iteration
    c. To define a function
    d. To temporarily leave a code block empty
  1. Which of the following comments regarding the pass statement is true?
    a. It is required in every Python program.
    b. It has no effect and is used for demonstration purposes.
    c. It raises an error when executed.
    d. It exits the program immediately.
  1. What do you mean by function in Python?
    a. A variable that stores data
    b. A sequence of statements that performs a specific task
    c. A reserved keyword in Python
    d. An operator for mathematical calculations
  1. How do you define a function in Python?
    a. Using the function keyword
    b. Using the def keyword followed by the function name and parameters
    c. Using the return keyword
    d. By declaring it as a variable
  1. What is a function argument in Python?
    a. A statement that defines a function
    b. Value will passed to a function when it is called
    c. A reserved keyword in Python
    d. An operator for mathematical calculations
  1. What do you mean by recursion in Python?
    a. A type of function that cannot call itself
    b. A loop construct
    c. A function that calls itself
    d. A reserved keyword in Python
  1. What does a recursive function’s base case mean?
    a. The case where recursion is not needed
    b. The first case in a function
    c. The last case in a function
    d. A reserved keyword in Python
  1. ___________ is known as anonymous function in Python?
    a. Lambda function
    b. Alpha function
    c. Omega function
    d. Beta function
  1. What keyword does Python use to define an anonymous function?
    a. function
    b. def
    c. lambda
    d. return
  1. What is a local variable in Python?
    a. A variable declared outside of any function
    b. A variable declared inside a function
    c. A built-in Python variable
    d. A reserved keyword in Python
  1. Why the Python global keyword used for?
    a. A variable declared outside of any function
    b. A variable declared inside a function
    c. A variable declared with the global keyword
    d. A reserved keyword in Python
  1. What is the purpose of the global keyword in Python?
    a. To declare a variable as local
    b. To declare a variable as global
    c. To create a new variable
    d. To define a function
  1. When should you use the global keyword in Python?
    a. To make a variable local to a specific function
    b. To create a new variable
    c. To declare a variable as global within a function
    d. To define an anonymous function
  1. What is a Python module?
    a. A Python package
    b. A built-in Python function
    c. A file containing Python code and definitions
    d. A reserved keyword in Python
  1. How do you import a Python module?
    a. Using the import statement followed by the module name
    b. Using the module keyword
    c. Using the def keyword
    d. By copying and pasting the module code into your program
  1. What is a Python package?
    a. A Python module
    b. A collection of related Python modules organized in directories
    c. A built-in Python function
    d. A reserved keyword in Python
  1. What is the result of 5 / 2 in Python?
    a. 2.5
    b. 2
    c. 2.0
    d. 3
  1. Which of the following is not a valid Python number type?
    a. Integer
    b. Float
    c. Complex
    d. Decimal
  1. How you will access element in python list.
    a. Using parentheses
    b. Using square brackets and the index
    c. Using curly braces
    d. Using angle brackets
  1. What is the result of len([1, 2, 3])?
    a. 1
    b. 2
    c. 3
    d. 0
  1. How are Python tuples different from lists?
    a. Tuples are mutable, while lists are immutable.
    b. Tuples are ordered, while lists are unordered.
    c. Tuples use square brackets, while lists use parentheses.
    d. Tuples use parentheses, while lists use square brackets.
  1. Can you modify the elements of a tuple after it is created?
    a. Yes, tuples are mutable.
    b. No, tuples are immutable.
    c. Only the first element can be modified.
    d. Tuples can be modified, but only by using special methods.
  1. How you will create multiline string in Python?
    a. str = ‘This is a multiline string.’
    b. str = “This is a multiline string.”
    c. str = ”’This is a multiline string.”’
    d. str = “This is a\nmultiline string.”
  1. What is the result of ‘Python’ + ‘ is fun’?
    a. Python is fun
    b. Pythonisfun
    c. Python + is fun
    d. TypeError
  1. How you can describes python set?
    a. An ordered collection of elements
    b. A collection of key-value pairs
    c. An unordered collection of unique elements
    d. A mutable collection of elements
  1. What is the result of len({1, 2, 3})?
    a. 1
    b. 2
    c. 3
    d. 0
  1. What do you mean by Python dictionary?
    a. An ordered collection of elements
    b. A collection of key-value pairs
    c. An unordered collection of unique elements
    d. A mutable collection of elements
  1. How may a value be accessed from a Python dictionary?
    a. Using parentheses
    b. Using square brackets and the index
    c. Using curly braces
    d. Using square brackets and the key
  1. What happens if you attempt to use a key that isn’t listed in a dictionary?
    a. It returns the value None.
    b. It raises a KeyError exception.
    c. It adds the key to the dictionary with a default value.
    d. It returns the value False.
  1. Which mode is used for opening a file in Python for reading?
    a. ‘r’
    b. ‘w’
    c. ‘a’
    d. ‘x’
  1. What does the ‘w’ mode do when opening a file in Python?
    a. Reads the file
    b. Writes to the file (creates a new file or truncates an existing file)
    c. Appends to the file
    d. Throws an error
  1. How do you alter the current working directory in Python?
    a. Using the cd command
    b. Using the os.chdir() function
    c. Using the pwd command
    d. Using the os.getcwd() function
  1. What is the purpose of the os.listdir() function in Python?
    a. To create a new directory
    b. To list the contents of a directory
    c. To rename a file
    d. To delete a directory
  1. What is an exception in Python?
    a. A standard function
    b. A type of variable
    c. An error that occurs when running a program.
    d. A reserved keyword in Python
  1. Which of the following doesn’t qualify as a Python built-in exception?
    a. ValueError
    b. SyntaxError
    c. CustomError
    d. TypeError
  1. What are the Python try and except blocks used for?
    a. To define a function
    b. To execute code unconditionally
    c. To handle exceptions gracefully
    d. To skip the current iteration in a loop
  1. What is the purpose of the finally block in Python exception handling?
    a. To define a function
    b. To execute code unconditionally
    c. To handle exceptions gracefully
    d. To skip the current iteration in a loop
  1. How can a unique (user-defined. exception class be made in Python?
    a. By inheriting from the built-in Exception class
    b. By using the try and except blocks
    c. By importing a module
    d. By using the raise keyword
  1. What does it mean to throw a custom exception in Python?
    a. To suppress errors
    b. To create a new exception type for specific situations
    c. To terminate the program
    d. To define a function
  1. In Python, how do you raise a custom exception?
    a. By using the try and except blocks
    b. By using the import statement
    c. By using the throw keyword
    d. By using the raise keyword
  1. What is Object-Oriented Programming (OOP) in Python?
    a. A programming language used for the development of websites
    b. A programming paradigm that uses objects and classes
    c. A built-in Python function
    d. A data type in Python
  1. What is the main advantage of using OOP in Python?
    a. Simplicity
    b. Code reusability and organization
    c. Speed of execution
    d. Lack of complexity
  1. What is a class in Python?
    a. A reserved keyword
    b. A built-in function
    c. A blueprint for creating objects
    d. A data type
  1. In Python, how do you make an instance (object) of a class?
    a. Using the new keyword
    b. Using the create function
    c. By calling the class constructor
    d. By importing the class
  1. What is inheritance in Python?
    a. A built-in Python function
    b. A mechanism for sharing attributes and methods between classes
    c. A way to create objects
    d. A type of loop
  1. What is a base class (superclass) in Python?
    a. A class that inherits from another class
    b. A class that has no attributes
    c. A class at the top of the inheritance hierarchy
    d. A reserved keyword
  1. What is operator overloading in Python?
    a. Creating new operators in Python
    b. Overriding built-in operators for custom classes
    c. Using operators excessively in code
    d. Converting operators to numbers
  1. Which technique is utilized in Python to overload the addition (+) operator?
    a. add()
    b. add()
    c. plus()
    d. overload_add()
  1. What is the purpose of operator overloading in Python?
    a. To create new operators
    b. To increase the speed of code execution
    c. To use operators with custom objects
    d. To remove built-in operators
  1. What is an iterator in Python?
    a. A built-in data type
    b. A sequence of numbers
    c. An object that can be iterated (looped. over)
    d. A reserved keyword in Python
  1. Which approach is used in Python to get the next item from an iterator?
    a. next()
    b. get()
    c. fetch()
    d. retrieve()
  1. What is a generator in Python?
    a. A built-in data type
    b. A sequence of numbers
    c. A function that yields values one at a time
    d. A reserved keyword in Python
  1. In Python, how do you define a generator function?
    a. Using the generate keyword
    b. By using a list comprehension
    c. Using the def keyword with yield statements
    d. By importing a module
  1. What is a closure in Python?
    a. A built-in data type
    b. A way to close a file
    c. A function that has access to variables from its containing function
    d. A reserved keyword in Python
  1. What is the main benefit of using Python closures?
    a. Improved performance
    b. Code reusability
    c. Access to variables in an outer function’s scope
    d. Reduced memory usage
  1. What is a decorator in Python?
    a. A built-in data type
    b. A way to decorate code with comments
    c. A function that modifies the behavior of another function
    d. A reserved keyword in Python
  1. How may a decorator be used on a Python function?
    a. By using the apply keyword
    b. By wrapping the function with the decorator function using the @ symbol
    c. By using the decorate keyword
    d. By importing a module
  1. What is a property in Python?
    a. A built-in data type
    b. A way to define class attributes
    c. A function that computes an attribute’s value on the fly
    d. A reserved keyword in Python
  1. Which built-in Python function creates a property?
    a. create_property()
    b. property()
    c. add_property()
    d. set_property()
  1. What does “regex” stand for?
    a. Regular expression
    b. Reverse expression
    c. Recursive expression
    d. Reserved expression
  1. Which module is used for regular expressions in Python?
    a. regex
    b. re
    c. reg
    d. exp
  1. What is the purpose of the re.match() function in Python?
    a. To search for a pattern anywhere in a string
    b. To search for a pattern at the beginning of a string
    c. To replace a pattern with another string
    d. To divide a string into parts depending on a pattern
  1. What is the main function of the Python datetime module?
    a. To perform mathematical calculations
    b. To work with dates and times
    c. To manipulate strings
    d. To handle exceptions
  1. In order to work with dates and times in Python, which module should you import?
    a. math
    b. calendar
    c. datetime
    d. time
  1. What can you do with Python datetime’s strftime() method?
    a. Parse a string to a datetime object
    b. Format a datetime object as a string
    c. Retrieve the current date and time
    d. Convert a timestamp to a datetime object
  1. What format code is applied to the year with century when it is expressed as a decimal number?
    a. %y
    b. %Y
    c. %m
    d. %d
  1. What does the strftime format code %H stand for?
    a. Hour (00-23)
    b. Hour (01-12)
    c. Minute (00-59)
    d. Second (00-59)
  1. What can you do with Python datetime’s strptime() method?
    a. Parse a string to a datetime object
    b. Format a datetime object as a string
    c. Retrieve the current date and time
    d. Get the current timestamp
  1. Which format string should be passed to strptime() in order to parse a date in the format “dd-mm-yyyy”?
    a. “%d-%m-%Y”
    b. “%Y-%m-%d”
    c. “%m-%d-%Y”
    d. “%Y-%d-%m”
  1. How do you obtain the current date and time in Python?
    a. Using the now() method of the datetime class
    b. Using the get_current_time() function
    c. By importing the current_time module
    d. By calling the get_datetime() method
  1. What is the difference between datetime.now() and datetime.today()?
    a. There is no difference; they are equivalent.
    b. now() returns the current date and time, while today() returns only the current date.
    c. today() returns the current date and time, while now() returns only the current date.
    d. now() returns the current date in a different format than today().
  1. Which module in Python provides functions to work with time-related operations?
    a. datetime
    b. time
    c. calendar
    d. timestamp
  1. How can the Python time module be used to get the current time (hour, minute, and second.?
    a. current_time()
    b. get_current_time()
    c. time.localtime()
    d. time.now()
  1. How do you change a timestamp in Python into a datetime object?
    a. Using the timestamp_to_datetime() function
    b. By directly assigning the timestamp to a datetime variable
    c. Using the datetime.fromtimestamp() method
    d. There is no way to perform this conversion.
  1. What is a timestamp in Python?
    a. A string representing the current date and time
    b. A floating-point number representing the number of seconds since the epoch
    c. A specific date and time in the future
    d. A datetime object
  1. What is the primary purpose of the time module in Python?
    a. To work with dates and times
    b. To perform mathematical calculations
    c. To manipulate strings
    d. To handle exceptions
  1. Which Python module provides functions for delaying program execution?
    a. datetime
    b. time
    c. calendar
    d. delay
  1. What is the purpose of the time.sleep() function in Python?
    a. To determine how quickly code is executed
    b. To generate random delays in code
    c. To pause program execution for a specified number of seconds
    d. To control the order of execution of statements
  1. Which argument does the time.sleep() function accept?
    a. A function to execute after the sleep time
    b. The number of milliseconds to sleep
    c. The number of seconds to sleep
    d. The time zone to use during sleep
  1. What will happen if you provide a negative value as an argument to time.sleep()?
    a. It will raise an exception.
    b. It will sleep for the specified negative duration.
    c. It will sleep indefinitely.
    d. It will have no effect on program execution.
  1. What will the following code produce as a result?
    x = 5
    y = 2
    z = x % y
    print(z)

    a. 2
    b. 2.5
    c. 1
    d. 0
  1. Which of the following is not a valid variable name in python?
    a. my_var
    b. 123var
    c. _var123
    d. Var123
  1. What will the following code produce as a result?
    my_list = [1, 2, 3, 4, 5]
    print(my_list[2:4])

    a. [2, 3, 4]
    b. [3, 4]
    c. [2, 3]
    d. [3]
  1. Which of the following statements correctly initializes an empty dictionary in Python?
    a. my_dict = {}
    b. my_dict = dict()
    c. my_dict = dict[]
    d. my_dict = {None}
  1. What will the following code produce as a result?
    def add_numbers(a, b.:
    return a + b
    result = add_numbers(3, 4)

    a. 7
    b. “7”
    c. [7]
    d. None
  1. What does the import statement do in Python?
    a. Exports a module
    b. Imports a module
    c. Deletes a module
    d. Renames a module
  1. What will the following code produce as a result?
    x = 10
    def func():
    x = 5
    func()
    print(x)

    a. 10
    b. 5
    c. Error
    d. None
  1. How should a file named “data.txt” be opened in read mode?
    a. file = open(“data.txt”, “w”)
    b. file = open(“data.txt”, “r”)
    c. file = open(“data.txt”, “a”)
    d. file = open(“data.txt”, “rb”)
  1. Given a list my_list = [1, 2, 3, 4, 5], what is the result of my_list[2:4]?
    a. [1, 2]
    b. [2, 3]
    c. [3, 4]
    d. [4, 5]
  1. Which of the following is used to define a function in Python?
    a. define
    b. func
    c. def
    d. function
  1. What will the following code produce as a result?
    x = 5
    y = x
    x = 10
    print(y)

    a. 5
    b. 10
    c. 0
    d. None
  1. Which of the following methods does not constitute a legal technique to comment out a single line of Python code?
    a. # This is a comment
    b. /* This is a comment */
    c. ”’ This is a comment ”’
    d. // This is a comment
  1. What will the following code produce as a result?
    num_list = [1, 2, 3, 4, 5]
    result = sum(num_list)

    a. 15
    b. [1, 2, 3, 4, 5]
    c. 10
    d. None
  1. What is the output of the following code?
    my_string = “Hello, World!”
    print(my_string[7:])

    a. “Hello”
    b. “World”
    c. “World!”
    d. “ld!”
  1. What is the correct way to write a comment that spans multiple lines in Python?
    a. # This is a multi-line comment
    b. /* This is a multi-line comment */
    c. ”’ This is a multi-line comment ”’
    d. // This is a multi-line comment
  1. What will the following code produce as a result?
    my_dict = {“name”: “John”, “age”: 30}
    del my_dict[“age”]
    print(my_dict)

    a. {“name”: “John”, “age”: 30}
    b. {}
    c. {“name”: “John”}
    d. None
  1. Which data structure in Python is ordered and indexed but does not allow duplicate elements?
    a. List
    b. Tuple
    c. Dictionary
    d. Set
  1. What is the correct way to check if a variable x is of type int in Python?
    a. if type(x) == “int”:
    b. if x is int:
    c. if isinstance(x, int):
    d. if x == int:
  1. What does the len() function in Python return?
    a. The length of a string
    b. The number of elements in a list or tuple
    c. The size of a file
    d. The value of a variable
  1. What is the output of the following code?
    a = 5
    b = 3
    a, b = b, a
    print(a, b.
    a. 5 3
    b. 3 5
    c. 8
    d. Syntax Error
  1. Which Python operator is used to perform exponentiation?
    a. **
    b. ^^
    c. //
    d. ^
  1. What will the following code produce as a result?
    my_list = [1, 2, 3]
    my_list.append(4)
    print(my_list)

    a. [1, 2, 3]
    b. [2, 3, 4]
    c. [1, 2, 3, 4]
    d. [4]
  1. How can you define a function with an arbitrary number of positional arguments in Python?
    a. Using the def keyword
    b. Using the *args parameter
    c. Using the **kwargs parameter
    d. Using the @staticmethod decorator
  1. What does the range() function in Python return?
    a. A list of integers
    b. A generator object
    c. A dictionary
    d. A tuple

error: Content is protected !!