Master Python with These Unique MCQs (With Answers)

Python is a very popular programming language globally, famous for its ease and flexibility. It doesn’t matter if you are new to coding or a seasoned coder; trying out your skills with multiple-choice questions(MCQs) is an excellent way to reinforce your knowledge. In this blog post, we’ve rounded up 80 individual MCQs to enable you to test your skills and acquire new knowledge.

Python MCQs


  1. What is the output of the following code? python Copy print (type(5.0))
    b) float
    c) str
    d) None
    Answer: b) float
  2. Which of the following is a valid variable name in Python?
    a) 1var
    b) _var
    c) var-name
    d) var name
    Answer: b) _var
  3. What does the len() function do?
    a) Returns the type of an object
    b) Returns the length of an object
    c) Converts a string to lowercase
    d) None of the above
    Answer: b) Returns the length of an object
  4. Which keyword is used to define a function in Python?
    a) func
    b) define
    c) def
    d) function
    Answer: c) def
  5. What is the output of print(2 ** 3)?
    a) 6
    b) 8
    c) 9
    d) 23
    Answer: b) 8

  1. Which of the following is a mutable data type in Python?
    a) int
    b) str
    c) list
    d) tuple
    Answer: c) list
  2. What is the output of the following code?pythonCopyprint([1, 2, 3] + [4, 5])a) [1, 2, 3, 4, 5]
    b) [5, 7, 3]
    c) [1, 2, 3]
    d) Error
    Answer: a) [1, 2, 3, 4, 5]
  3. Which method is used to add an element to the end of a list?
    a) append()
    b) insert()
    c) add()
    d) extend()
    Answer: a) append()
  4. What is the output of the following code?pythonCopyprint((1, 2, 3) == (1, 2, 3))a) True
    b) False
    c) Error
    d) None
    Answer: a) True
  5. Which of the following is NOT a valid dictionary declaration?
    a) {1: 'one', 2: 'two'}
    b) dict([(1, 'one'), (2, 'two')])
    c) {1, 2, 3}
    d) {}
    Answer: c) {1, 2, 3}

  1. What is the output of the following code?pythonCopyfor i in range(3): print(i, end=” “)a) 0 1 2
    b) 1 2 3
    c) 0 1 2 3
    d) Error
    Answer: a) 0 1 2
  2. Which keyword is used to exit a loop prematurely?
    a) stop
    b) break
    c) exit
    d) return
    Answer: b) break
  3. What does the pass statement do?
    a) Exits the program
    b) Skips the current iteration
    c) Acts as a placeholder
    d) None of the above
    Answer: c) Acts as a placeholder
  4. What is the output of the following code?pythonCopyx = 10 if x > 5: print(“Greater”) else: print(“Smaller”)a) Greater
    b) Smaller
    c) Error
    d) None
    Answer: a) Greater
  5. Which loop is used to iterate over a sequence in Python?
    a) for
    b) while
    c) do-while
    d) repeat
    Answer: a) for

  1. What is the output of the following code?pythonCopydef add(a, b=5): return a + b print(add(10))a) 10
    b) 15
    c) Error
    d) None
    Answer: b) 15
  2. Which keyword is used to import a module in Python?
    a) include
    b) import
    c) require
    d) use
    Answer: b) import
  3. What is the output of the following code?pythonCopyimport math print(math.sqrt(16))a) 2
    b) 4
    c) 8
    d) Error
    Answer: b) 4
  4. What does the return statement do in a function?
    a) Exits the function
    b) Returns a value to the caller
    c) Both a and b
    d) None of the above
    Answer: c) Both a and b
  5. Which of the following is a built-in function in Python?
    a) print()
    b) input()
    c) len()
    d) All of the above
    Answer: d) All of the above

  1. Which mode is used to open a file for writing?
    a) ‘r’
    b) ‘w’
    c) ‘a’
    d) ‘x’
    Answer: b) ‘w’
  2. What is the output of the following code?pythonCopywith open(“test.txt”, “w”) as f: f.write(“Hello, World!”)a) Creates a file and writes “Hello, World!”
    b) Reads the file
    c) Deletes the file
    d) Error
    Answer: a) Creates a file and writes “Hello, World!”
  3. Which method is used to read a file line by line?
    a) read()
    b) readline()
    c) readlines()
    d) Both b and c
    Answer: d) Both b and c
  4. What does the close() method do?
    a) Deletes a file
    b) Closes the file and frees up resources
    c) Reads the file
    d) None of the above
    Answer: b) Closes the file and frees up resources
  5. Which mode is used to open a file for appending?
    a) ‘r’
    b) ‘w’
    c) ‘a’
    d) ‘x’
    Answer: c) ‘a’

  1. What is the output of the following code?pythonCopyclass MyClass: x = 5 obj = MyClass() print(obj.x)a) 5
    b) Error
    c) None
    d) 0
    Answer: a) 5
  2. Which keyword is used to create a class in Python?
    a) class
    b) def
    c) struct
    d) object
    Answer: a) class
  3. What is the output of the following code?pythonCopyclass MyClass: def __init__(self, name): self.name = name obj = MyClass(“Python”) print(obj.name)a) Python
    b) Error
    c) None
    d) MyClass
    Answer: a) Python
  4. Which method is used to define a constructor in Python?
    a) init()
    b) new()
    c) class()
    d) self()
    Answer: a) init()
  5. What is inheritance in Python?
    a) A way to hide data
    b) A way to reuse code
    c) A way to create loops
    d) None of the above
    Answer: b) A way to reuse code

  1. What is the output of the following code?pythonCopytry: print(10 / 0) except ZeroDivisionError: print(“Error”)a) Error
    b) 0
    c) None
    d) 10
    Answer: a) Error
  2. Which keyword is used to handle exceptions in Python?
    a) try
    b) catch
    c) except
    d) Both a and c
    Answer: d) Both a and c
  3. What is the output of the following code?pythonCopytry: print(“Hello”) finally: print(“World”)a) Hello
    b) World
    c) Hello World
    d) Error
    Answer: c) Hello World
  4. Which block is executed if no exception occurs?
    a) try
    b) except
    c) finally
    d) else
    Answer: d) else
  5. What is the purpose of the finally block?
    a) To handle exceptions
    b) To execute code regardless of exceptions
    c) To define a function
    d) None of the above
    Answer: b) To execute code regardless of exceptions

  1. What is the output of the following code?pythonCopyx = lambda a: a + 10 print(x(5))a) 5
    b) 10
    c) 15
    d) Error
    Answer: c) 15
  2. Which module is used for regular expressions in Python?
    a) re
    b) regex
    c) string
    d) None of the above
    Answer: a) re
  3. What is the output of the following code?pythonCopyimport re print(re.findall(r’\d’, ‘abc123’))a) [‘1’, ‘2’, ‘3’]
    b) [‘abc’]
    c) [‘123’]
    d) Error
    Answer: a) [‘1’, ‘2’, ‘3’]
  4. What is a decorator in Python?
    a) A function that modifies another function
    b) A type of loop
    c) A data structure
    d) None of the above
    Answer: a) A function that modifies another function
  5. What is the output of the following code?pythonCopydef decorator(func): def wrapper(): print(“Before”) func() print(“After”) return wrapper @decorator def say_hello(): print(“Hello”) say_hello()a) Before Hello After
    b) Hello
    c) Before After
    d) Error
    Answer: a) Before Hello After

MCQs (41 to 80)  These questions will cover advanced Python concepts, libraries, and practical applications.


  1. What is the output of the following code?pythonCopysquares = [x**2 for x in range(5)] print(squares)a) [0, 1, 4, 9, 16]
    b) [1, 4, 9, 16, 25]
    c) [0, 1, 2, 3, 4]
    d) Error
    Answer: a) [0, 1, 4, 9, 16]
  2. Which of the following is a generator expression?
    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: x**2 for x in range(5)}
    Answer: a) (x**2 for x in range(5))
  3. What is the output of the following code?pythonCopygen = (x for x in range(3)) print(next(gen)) print(next(gen))a) 0 1
    b) 1 2
    c) 0 1 2
    d) Error
    Answer: a) 0 1
  4. What is the advantage of using a generator over a list?
    a) Generators are faster
    b) Generators use less memory
    c) Generators are easier to write
    d) None of the above
    Answer: b) Generators use less memory
  5. What is the output of the following code?pythonCopydef my_gen(): yield 1 yield 2 for i in my_gen(): print(i, end=” “)a) 1 2
    b) 1
    c) 2
    d) Error
    Answer: a) 1 2

  1. What is the output of the following code?pythonCopymy_set = {1, 2, 3, 3, 2} print(len(my_set))a) 3
    b) 5
    c) 2
    d) Error
    Answer: a) 3
  2. Which method is used to add an element to a set?
    a) add()
    b) append()
    c) insert()
    d) extend()
    Answer: a) add()
  3. What is the output of the following code?pythonCopymy_dict = {‘a’: 1, ‘b’: 2} print(my_dict.get(‘c’, 3))a) 1
    b) 2
    c) 3
    d) Error
    Answer: c) 3
  4. Which method removes all elements from a dictionary?
    a) clear()
    b) remove()
    c) pop()
    d) delete()
    Answer: a) clear()
  5. What is the output of the following code?pythonCopymy_dict = {x: x**2 for x in range(3)} print(my_dict)a) {0: 0, 1: 1, 2: 4}
    b) {0: 0, 1: 1, 2: 2}
    c) {1: 1, 2: 4}
    d) Error
    Answer: a) {0: 0, 1: 1, 2: 4}

  1. Which library is used for numerical computations in Python?
    a) math
    b) numpy
    c) pandas
    d) random
    Answer: b) numpy
  2. What is the output of the following code?pythonCopyimport numpy as np arr = np.array([1, 2, 3]) print(arr.shape)a) (3,)
    b) (1, 3)
    c) (3, 1)
    d) Error
    Answer: a) (3,)
  3. Which library is used for data manipulation and analysis?
    a) numpy
    b) pandas
    c) matplotlib
    d) scipy
    Answer: b) pandas
  4. What is the output of the following code?pythonCopyimport pandas as pd data = {‘Name’: [‘Alice’, ‘Bob’], ‘Age’: [25, 30]} df = pd.DataFrame(data) print(df[‘Name’][1])a) Alice
    b) Bob
    c) 25
    d) 30
    Answer: b) Bob
  5. Which library is used for plotting graphs in Python?
    a) numpy
    b) pandas
    c) matplotlib
    d) scipy
    Answer: c) matplotlib

  1. What is the output of the following code?pythonCopydef outer(): x = 5 def inner(): nonlocal x x += 1 return x return inner() print(outer())a) 5
    b) 6
    c) Error
    d) None
    Answer: b) 6
  2. What is the purpose of the nonlocal keyword?
    a) To declare a global variable
    b) To declare a local variable
    c) To modify a variable in the nearest enclosing scope
    d) None of the above
    Answer: c) To modify a variable in the nearest enclosing scope
  3. What is the output of the following code?pythonCopydef func(*args): return sum(args) print(func(1, 2, 3))a) 6
    b) (1, 2, 3)
    c) Error
    d) None
    Answer: a) 6
  4. What does the *args parameter do in a function?
    a) Accepts keyword arguments
    b) Accepts a variable number of positional arguments
    c) Accepts a single argument
    d) None of the above
    Answer: b) Accepts a variable number of positional arguments
  5. What is the output of the following code?pythonCopydef func(**kwargs): return kwargs print(func(a=1, b=2))a) {‘a’: 1, ‘b’: 2}
    b) (1, 2)
    c) Error
    d) None
    Answer: a) {‘a’: 1, ‘b’: 2}

  1. Which method is used to check if a file exists?
    a) os.path.exists()
    b) os.exists()
    c) file.exists()
    d) None of the above
    Answer: a) os.path.exists()
  2. What is the output of the following code?pythonCopyimport os print(os.name)a) posix
    b) nt
    c) java
    d) Depends on the OS
    Answer: d) Depends on the OS
  3. Which method is used to rename a file?
    a) os.rename()
    b) os.move()
    c) os.replace()
    d) os.update()
    Answer: a) os.rename()
  4. What is the output of the following code?pythonCopyimport os print(os.getcwd())a) Current working directory
    b) Home directory
    c) Root directory
    d) Error
    Answer: a) Current working directory
  5. Which method is used to delete a file?
    a) os.delete()
    b) os.remove()
    c) os.unlink()
    d) Both b and c
    Answer: d) Both b and c

  1. What is the output of the following code?pythonCopytry: print(10 / 0) except ZeroDivisionError as e: print(“Error:”, e)a) Error: division by zero
    b) Error: ZeroDivisionError
    c) Error: 10 / 0
    d) None
    Answer: a) Error: division by zero
  2. Which module is used for logging in Python?
    a) log
    b) logging
    c) logger
    d) debug
    Answer: b) logging
  3. What is the output of the following code?pythonCopyimport logging logging.basicConfig(level=logging.DEBUG) logging.debug(“Debug message”)a) Debug message
    b) No output
    c) Error
    d) None
    Answer: a) Debug message
  4. Which method is used to raise an exception manually?
    a) raise
    b) throw
    c) except
    d) assert
    Answer: a) raise
  5. What is the purpose of the assert statement?
    a) To handle exceptions
    b) To debug code
    c) To test conditions
    d) Both b and c
    Answer: d) Both b and c

  1. What is the output of the following code?pythonCopyprint(__name__)a) main
    b) main
    c) None
    d) Error
    Answer: a) main
  2. Which module is used to work with dates and times?
    a) datetime
    b) time
    c) calendar
    d) All of the above
    Answer: d) All of the above
  3. What is the output of the following code?pythonCopyimport datetime print(datetime.datetime.now().year)a) Current year
    b) 2023
    c) Error
    d) None
    Answer: a) Current year
  4. Which module is used for working with JSON data?
    a) json
    b) xml
    c) yaml
    d) None of the above
    Answer: a) json
  5. What is the output of the following code?pythonCopyimport json data = ‘{“name”: “Alice”, “age”: 25}’ print(json.loads(data)[‘name’])a) Alice
    b) 25
    c) Error
    d) None
    Answer: a) Alice

  1. What is the output of the following code?pythonCopyprint(isinstance(5, int))a) True
    b) False
    c) Error
    d) None
    Answer: a) True
  2. Which method is used to sort a list in place?
    a) sorted()
    b) sort()
    c) arrange()
    d) order()
    Answer: b) sort()
  3. What is the output of the following code?pythonCopymy_list = [3, 1, 2] my_list.sort() print(my_list)a) [1, 2, 3]
    b) [3, 1, 2]
    c) Error
    d) None
    Answer: a) [1, 2, 3]
  4. Which method is used to reverse a list in place?
    a) reverse()
    b) reversed()
    c) sort(reverse=True)
    d) None of the above
    Answer: a) reverse()
  5. What is the output of the following code?pythonCopymy_list = [1, 2, 3] my_list.reverse() print(my_list)a) [3, 2, 1]
    b) [1, 2, 3]
    c) Error
    d) None
    Answer: a) [3, 2, 1]

Conclusion:
These 80 MCQs cover a wide range of Python topics, from basic syntax to advanced concepts. Use them to test your knowledge, prepare for interviews, or simply learn something new. Stay connected with us we will be covering wide range of topics here.

Do comment below if you need any assistance or have any doubts regarding this or any other topic. We (Our Team) will be for sure more than happy to help you out

For more Click Here


Computer Fundamentals Practice MCQ Question and Answer

Leave a Comment