Python Quiz - MCQ - Multiple Choice Questions

Python is a popular, versatile, and high-level programming language known for its ease of use and clean syntax. Whether you're just starting on your Python programming journey or revisiting the basics, these MCQs can provide a concise review. 

In this quiz, we curated 50+ MCQs on Python fundamentals. Note that each MCQ (Multiple Choice Question) is followed by the correct answer and an explanation to help reinforce your knowledge.

Topics covered:

  • Python Introduction
  • OOPS
  • File Handling
  • Data Types
  • Strings
  • Exception Handling
  • List
  • Functions
  • Operators
  • Dictionary
  • Variables

1. Which company developed the Python programming language?

a) Microsoft
b) Google
c) Mozilla
d) CWI (Centrum Wiskunde & Informatica)

Answer:

d) CWI (Centrum Wiskunde & Informatica)

Explanation:

Python was created by Guido van Rossum and was first released in 1991 while he was working at CWI.

2. Who developed the Python language?

a) Zim Den
b) Guido van Rossum
c) Niene Stom
d) Wick van Rossum

Answer:

b) Guido van Rossum

Explanation:

Python was created by Guido van Rossum and was first released in 1991. The other names listed are not associated with the development of the Python language.

3. In which programming language, Python was written?

a) Java
b) C
c) Python
d) JavaScript

Answer:

b) C

Explanation:

The primary implementation of Python, known as CPython, is written in the C programming language. While there are other implementations of Python (like Jython, which is written in Java), CPython is the most widely used and is considered the standard implementation.

4. Which one of the following is the correct extension of the Python file?

a) .pyt
b) .py
c) .python
d) .pt

Answer:

b) .py

Explanation:

Python scripts and programs are saved with the ".py" file extension. This is the standard file extension used for Python source code files.

5. What do we use to define a block of code in Python language?

a) Curly braces { }
b) Square brackets [ ]
c) Parentheses ( )
d) Indentation

Answer:

d) Indentation

Explanation:

Unlike many programming languages that use curly braces { } to define a block of code, Python uses indentation (usually spaces or tabs) to define blocks. Proper indentation is crucial in Python as it determines the scope and grouping of statements.

6. Which of the following is the correct way to print "Hello, World!" in Python?

a) print("Hello, World!")
b) echo "Hello, World!"
c) System.out.print("Hello, World!")
d) console.log("Hello, World!")

Answer:

a) print("Hello, World!")

Explanation:

In Python, the print() function is used to display output. Options b, c, and d are used in other programming languages, but not in Python.

7. Which of the following is used for single-line comments in Python?

a) //
b) /* */
c) #
d) --

Answer:

c) #

Explanation:

In Python, the # symbol is used to denote a single-line comment.

8. Which is not an OOP principle?

a) Inheritance
b) Polymorphism
c) Encapsulation
d) Concatenation

Answer:

d) Concatenation

Explanation:

All the others are OOP principles. Concatenation is an operation on strings.

9. Which is used to create an object in Python?

a) Function
b) Method
c) Class
d) Procedure

Answer:

c) Class

Explanation:

In OOP, a class definition is used to instantiate objects.

10. Which is used for private data members in Python?

a) - (dash)
b) _ (underscore)
c) ! (exclamation mark)
d) # (hash)

Answer:

b) _ (underscore)

Explanation:

Prefixing a data member with an underscore suggests it's meant to be private, though it's just a convention.

11. Which function opens a file for reading in Python?

a) openFile()
b) fileOpen()
c) open()
d) readFile()

Answer:

c) open()

Explanation:

The built-in open() function is used to open files in Python.

12. Which mode is used for binary reading in Python?

a) 'r'
b) 'rb'
c) 'r+'
d) 'w'

Answer:

b) 'rb'

Explanation:

The 'rb' mode is used to read files in binary mode.

13. Which of these is mutable?

a) List
b) Tuple
c) String
d) None of the above

Answer:

a) List

Explanation:

Among the options, only lists are mutable.

14. Which of the following can't be a dictionary key?

a) Integer
b) String
c) List
d) Tuple

Answer:

c) List

Explanation:

Dictionary keys must be immutable, so lists can't be keys.

15. What is the output of "Python".lower()?

a) PYTHON
b) python
c) PythoN
d) Error

Answer:

b) python

Explanation:

The lower() method converts all uppercase characters in a string to lowercase.

16. Which method is used to replace parts of a string?

a) switch()
b) change()
c) modify()
d) replace()

Answer:

d) replace()

Explanation:

The replace() method replaces specified values with other values.

17. Which block is executed no matter if an exception occurs or not?

a) except
b) try
c) finally
d) catch

Answer:

c) finally

Explanation:

The finally block always executes after the try and except blocks.

18. Which exception is raised when dividing by zero in Python?

a) ValueError
b) TypeError
c) ZeroDivisionError
d) ArithmeticError

Answer:

c) ZeroDivisionError

Explanation:

Division by zero raises a ZeroDivisionError exception.

19. What is the result of list("Python")?

a) [P, y, t, h, o, n]
b) ["Python"]
c) ["P", "y", "t", "h", "o", "n"]
d) Error

Answer:

c) ["P", "y", "t", "h", "o", "n"]

Explanation:

This function separates each character and creates a list of them.

20. How to add an element to the end of a list?

a) insert()
b) append()
c) add()
d) put()

Answer:

b) append()

Explanation:

append() adds an element to the end of a list.

21. Which keyword is used to define a function?

a) func
b) function
c) def
d) define

Answer:

c) def

Explanation:

In Python, functions are defined using the def keyword.

22. What does a function without a return statement return?

a) 0
b) None
c) "" (empty string)
d) Error

Answer:

b) None

Explanation:

If there's no return statement in a function, Python returns None.

23. Which operator is used for floor division?

a) /
b) *
c) //
d) %

Answer:

c) //

Explanation:

// is the floor division operator in Python.

24. Which operator is used for exponentiation?

a) ^
b) *
c) !
d) **

Answer:

d) **

Explanation:

** is the exponentiation operator in Python.

25. Which of these creates an empty dictionary?

a) dict{}
b) {}
c) []
d) ()

Answer:

b) {}

Explanation:

Curly braces {} are used for dictionary creation.

26. How to access the value for the key "apple" in a dictionary d?

a) d.apple
b) d["apple"]
c) d[apple]
d) d->apple

Answer:

b) d["apple"]

Explanation:

Values in dictionaries are accessed by their keys using square brackets.

27. Which of the following can be a valid variable name in Python?

a) 2apple
b) apple-pie
c) apple_pie
d) @apple

Answer:

c) apple_pie

Explanation:

Variable names can start with a letter or underscore and can have letters, numbers, or underscores in them.

28. Which method returns the position of the first occurrence of a specified String value?

a) find()
b) index()
c) locate()
d) Both a) and b)

Answer:

d) Both a) and b)

Explanation:

Both find() and index() can be used, but index() raises an exception if the value isn't found, whereas find() returns -1.

29. Which operator is used to concatenate two strings?

a) +
b) .
c) *
d) &

Answer:

a) +

Explanation:

The + operator is used to concatenate two strings.

30. Which statement raises an exception manually?

a) raise
b) trigger
c) throw
d) error

Answer:

a) raise

Explanation:

The raise statement allows the programmer to force a specific exception to occur.

31. Which method removes the specified item from the list?

a) discard()
b) delete()
c) remove()
d) pop()

Answer:

c) remove()

Explanation:

The remove() method removes the specified item.

32. What does [1, 2, 3] * 2 give?

a) [1, 2, 3, 1, 2, 3]
b) [2, 4, 6]
c) Error
d) [1, 1, 2, 2, 3, 3]

Answer:

a) [1, 2, 3, 1, 2, 3]

Explanation:

This operation replicates the list.

33. Which built-in function returns the absolute value?

a) abs()
b) fabs()
c) absolute()
d) all()

Answer:

a) abs()

Explanation:

The abs() function returns the absolute value of the specified number.

34. Which keyword is used for variable-length argument lists?

a) *args
b) **kwargs
c) &vars
d) Both a) and b)

Answer:

d) Both a) and b)

Explanation:

*args is used for non-keyworded variable-length arguments, and **kwargs is used for keyworded variable-length arguments.

35. Which of the following checks if the value is not in the list?

a) x not in y
b) x != y
c) x not of y
d) x !of y

Answer:

a) x not in y

Explanation:

The not in operator checks if a value is not present in the list.

36. What is the output of 5 // 2?

a) 2.5
b) 2.0
c) 2
d) 3

Answer:

c) 2

Explanation:

// is the floor division operator which truncates the decimal and rounds down.

37. Which method returns a list of all the values in the dictionary?

a) values()
b) keys()
c) items()
d) all()

Answer:

a) values()

Explanation:

The values() method returns a list of all the values in the dictionary.

38. Which method is used to remove all items from a dictionary?

a) clear()
b) delete()
c) discard()
d) empty()

Answer:

a) clear()

Explanation:

The clear() method removes all items from a dictionary.

39. Which of the following is true for variables in Python?

a) They need an explicit declaration
b) They are strongly typed
c) They are associated with a type at runtime
d) They are assigned a value using :=

Answer:

c) They are associated with a type at runtime

Explanation:

Variables in Python are dynamically typed, meaning they get their type at runtime based on the value they are assigned.

40. Which of the following creates a new reference to the same list?

a) list.copy()
b) list = list
c) list[:]
d) new_list = [] + list

Answer:

b) list = list

Explanation:

Assigning one list to another creates a reference to the same object, rather than a new copy.

41. Which method checks if the string starts with a particular substring?

a) startswith()
b) beginswith()
c) first()
d) isfirst()

Answer:

a) startswith()

Explanation:

The startswith() method is used to check if a string starts with a specified substring.

42. Which string method removes white spaces from both the beginning and the end?

a) strip()
b) clean()
c) trim()
d) wipe()

Answer:

a) strip()

Explanation:

The strip() method is used to remove any leading (spaces at the beginning) and trailing (spaces at the end) characters.

43. Which keyword can be used to handle an exception in Python?

a) catch
b) rescue
c) except
d) handle

Answer:

c) except

Explanation:

In Python, the except keyword is used to catch and handle exceptions.

44. What is the purpose of the global keyword in Python?

a) To create a global variable
b) To use a module-level variable inside a function
c) To define a global function
d) None of the above

Answer:

b) To use a module-level variable inside a function

Explanation:

The global keyword allows a function to use a variable defined at the module level.

45. Which of the following defines a lambda function that doubles a number?

a) double = lambda x: 2x
b) double = (lambda x: 2x)
c) double = lambda x: x*2
d) double(x) = lambda: x*2

Answer:

c) double = lambda x: x*2

Explanation:

Lambda functions are anonymous functions defined using the lambda keyword. The correct syntax is the one given in option c.

46. Which operator is used for element-wise multiplication in numpy arrays?

a) *
b) .
c) x
d) %

Answer:

a) *

Explanation:

In numpy, the * operator is used for element-wise multiplication, while the dot product requires the dot function.

47. Given a dictionary d = {"a": 1, "b": 2}, what will d.get("c", 3) return?

a) 1
b) 2
c) 3
d) None

Answer:

c) 3

Explanation:

The get method returns the value for the specified key if it exists, otherwise, it returns the default value provided (3 in this case).

48. What will list({"a": 1, "b": 2, "c": 3}.keys()) output?

a) ["a", "b", "c"]
b) [1, 2, 3]
c) ["a", 1, "b", 2, "c", 3]
d) {"a", "b", "c"}

Answer:

a) ["a", "b", "c"]

Explanation:

The keys method returns the keys in the dictionary. When converted to a list, it gives the list of keys.

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

a) _var
b) var1
c) 1var
d) var_name

Answer:

c) 1var

Explanation:

Variable names cannot start with a number in Python.

50. How can you make a variable private in a Python class?

a) Prefix it with __
b) Prefix it with _
c) Prefix it with private
d) Variables cannot be made private in Python

Answer:

a) Prefix it with __

Explanation:

Prefixing a variable with double underscores (__) makes it private. It's not truly private but is name-mangled to make it less accessible from the outside.


Comments