100 Python MCQ (Multiple Choice Questions) with Answers

1) What is the correct extension of a Python file?
  1. .python
  2. .p
  3. .py
  4. .pyt
Show Answer
Answer: c
Explanation
Python source files use the .py extension, for example script.py.

2) Who created the Python programming language?
  1. Guido van Rossum
  2. James Gosling
  3. Dennis Ritchie
  4. Bjarne Stroustrup
Show Answer
Answer: a
Explanation
Python was created by Guido van Rossum and first released in 1991.

3) Which of the following is the correct syntax to output “Hello World” in Python?
  1. echo(“Hello World”)
  2. print(“Hello World”)
  3. p(“Hello World”)
  4. System.out.println(“Hello World”)
Show Answer
Answer: b
Explanation
The built-in print() function displays output in Python: print("Hello World").

4) How do you insert comments in Python code?
  1. // This is a comment
  2. /* This is a comment */
  3. # This is a comment
  4. <!– This is a comment –>
Show Answer
Answer: c
Explanation
Single-line comments in Python begin with the # symbol.

5) Which data type is immutable in Python?
  1. List
  2. Dictionary
  3. Set
  4. Tuple
Show Answer
Answer: d
Explanation
A tuple is immutable — its contents cannot be changed after creation. Lists, dictionaries and sets are mutable.

6) What is the result of 3 ** 2 in Python?
  1. 6
  2. 9
  3. 8
  4. 5
Show Answer
Answer: b
Explanation
The ** operator means exponentiation. So 3 ** 2 means 3² = 9.

7) Which keyword is used to define a function in Python?
  1. func
  2. define
  3. def
  4. function
Show Answer
Answer: c
Explanation
Functions are defined using the def keyword, e.g. def my_function():.

8) Which of the following operators performs integer division in Python?
  1. /
  2. //
  3. %
  4. \
Show Answer
Answer: b
Explanation
// performs floor/integer division. For example, 7 // 2 gives 3.

9) What is the output of len([10, 20, 30])?
  1. 2
  2. 3
  3. 4
  4. Error
Show Answer
Answer: b
Explanation
The list contains three elements, so len([10, 20, 30]) returns 3.

10) How do you create a variable with the numeric value 5?
  1. x = 5
  2. int x = 5
  3. x = int(5)
  4. Both a and c
Show Answer
Answer: d
Explanation
Both x = 5 and x = int(5) create a variable with numeric value 5.

11) Which collection is ordered, changeable, and allows duplicate members?
  1. Tuple
  2. Dictionary
  3. Set
  4. List
Show Answer
Answer: d
Explanation
A list is ordered, mutable/changeable, and allows duplicate elements.

12) Which collection is unordered, unchangeable, and unindexed?
  1. Set
  2. List
  3. Tuple
  4. Dictionary
Show Answer
Answer: a
Explanation
A set is unordered and unindexed. Its items are not accessed by index and duplicates are not allowed.

13) What function is used to convert a value into an integer?
  1. integer()
  2. parse()
  3. int()
  4. str()
Show Answer
Answer: c
Explanation
The int() function converts a compatible value into an integer.

14) What will type(3.14) return?
  1. <class ‘int’>
  2. <class ‘float’>
  3. <class ‘double’>
  4. <class ‘decimal’>
Show Answer
Answer: b
Explanation
3.14 is a floating-point number, so type(3.14) returns <class 'float'>.

15) How do you start a for loop in Python?
  1. for x in y:
  2. for x = 1 to 10
  3. for (i = 0; i < 10; i++)
  4. foreach x in y
Show Answer
Answer: a
Explanation
Python uses the for x in y: syntax to iterate over items in an iterable.

16) Which method is used to remove whitespace from both the beginning and end of a string?
  1. pstrip()
  2. trim()
  3. strip()
  4. len()
Show Answer
Answer: c
Explanation
strip() removes leading and trailing whitespace from a string.

17) What keyword is used to raise an exception in Python?
  1. throw
  2. raise
  3. try
  4. catch
Show Answer
Answer: b
Explanation
The raise keyword is used to manually raise an exception in Python.

18) What is the correct syntax for an if statement in Python?
  1. if x == 5 then:
  2. if (x == 5)
  3. if x == 5:
  4. if x = 5:
Show Answer
Answer: c
Explanation
Python uses if condition: and == for comparison. The colon at the end is required.

19) Which operator is used to check if two values are equal?
  1. =
  2. ==
  3. ===
  4. is
Show Answer
Answer: b
Explanation
== checks equality of values. = is assignment, and is checks object identity.

20) What is the output of bool(“”)?
  1. True
  2. False
  3. None
  4. Error
Show Answer
Answer: b
Explanation
An empty string is considered falsey in Python, so bool("") returns False.

21) Which method adds an item to the end of a list?
  1. add()
  2. append()
  3. insert()
  4. push()
Show Answer
Answer: b
Explanation
append() adds a single item to the end of a list.

22) How do you access the value associated with the key “age” in dictionary d?
  1. d.age
  2. d[“age”]
  3. d(age)
  4. d[age]
Show Answer
Answer: b
Explanation
Dictionary values are accessed using square brackets with the key, e.g. d["age"].

23) What statement is used to exit a loop prematurely?
  1. exit
  2. return
  3. break
  4. stop
Show Answer
Answer: c
Explanation
The break statement immediately exits the nearest enclosing loop.

24) Which built-in function returns a range of numbers?
  1. sequence()
  2. range()
  3. loop()
  4. list()
Show Answer
Answer: b
Explanation
range() generates a sequence of numbers, commonly used in for loops.

25) What is the logical operator for logical AND in Python?
  1. &&
  2. and
  3. &
  4. AND
Show Answer
Answer: b
Explanation
Python uses the keyword and for logical AND, not &&.

26) Which symbol is used to pass variable-length positional arguments to a function?
  1. *
  2. **
  3. &
  4. #
Show Answer
Answer: a
Explanation
*args collects extra positional arguments into a tuple.

27) Which keyword is used to create a class in Python?
  1. struct
  2. object
  3. class
  4. define
Show Answer
Answer: c
Explanation
Classes are created using the class keyword, e.g. class MyClass:.

28) What will 2 + 3 * 4 evaluate to?
  1. 20
  2. 14
  3. 24
  4. 18
Show Answer
Answer: b
Explanation
Multiplication has higher precedence than addition: 3 * 4 = 12, then 2 + 12 = 14.

29) What is the output of “python”[1:4]?
  1. pyt
  2. yth
  3. ytho
  4. pyth
Show Answer
Answer: b
Explanation
Slicing [1:4] returns characters at indices 1, 2 and 3: y, t, h → yth.

30) How do you create an empty dictionary in Python?
  1. {}
  2. []
  3. ()
  4. set()
Show Answer
Answer: a
Explanation
{} creates an empty dictionary. [] is a list, () is a tuple and set() is an empty set.

31) What keyword handles exceptions in Python?
  1. catch
  2. except
  3. handle
  4. error
Show Answer
Answer: b
Explanation
Python uses try and except blocks to catch and handle exceptions.

32) Which built-in function returns the highest value in a list?
  1. top()
  2. max()
  3. highest()
  4. upper()
Show Answer
Answer: b
Explanation
max() returns the largest item in an iterable or among arguments.

33) What is the output of type([])?
  1. <class ‘set’>
  2. <class ‘tuple’>
  3. <class ‘array’>
  4. <class ‘list’>
Show Answer
Answer: d
Explanation
[] creates a list, so type([]) returns <class 'list'>.

34) What keyword is used to import a module in Python?
  1. import
  2. include
  3. require
  4. using
Show Answer
Answer: a
Explanation
Use import module_name to import a module in Python.

35) What is the default return value of a function that doesn’t explicitly return anything?
  1. 0
  2. False
  3. None
  4. Null
Show Answer
Answer: c
Explanation
If a function has no return statement, Python returns None by default.

36) Which method converts a string to all uppercase letters?
  1. uppercase()
  2. upper()
  3. toUpper()
  4. cap()
Show Answer
Answer: b
Explanation
upper() returns a copy of the string converted to uppercase.

37) How do you remove an item at a specific index from a list?
  1. remove()
  2. delete()
  3. pop()
  4. discard()
Show Answer
Answer: c
Explanation
pop(index) removes and returns the item at the given index.

38) What does PIP stand for in Python?
  1. Preferred Installer Program
  2. Python Installer Package
  3. Program Instruction Process
  4. Python Integration Program
Show Answer
Answer: a
Explanation
PIP is commonly expanded as Preferred Installer Program and is used to install Python packages.

39) Which operator is used for exponentiation?
  1. ^
  2. **
  3. pow
  4. exp
Show Answer
Answer: b
Explanation
The ** operator raises a number to a power, e.g. 2 ** 3 = 8.

40) What is the output of bool(0)?
  1. True
  2. False
  3. None
  4. Error
Show Answer
Answer: b
Explanation
Zero is falsey in Python, so bool(0) returns False.

41) Which statement skips the current iteration of a loop and continues with the next?
  1. pass
  2. skip
  3. continue
  4. next
Show Answer
Answer: c
Explanation
continue skips the rest of the current iteration and moves to the next iteration of the loop.

42) What function returns the absolute value of a number?
  1. absolute()
  2. abs()
  3. fabs()
  4. val()
Show Answer
Answer: b
Explanation
abs() returns the absolute value of a number, e.g. abs(-5) is 5.

43) Which special variable represents the class instance in class methods?
  1. this
  2. self
  3. instance
  4. me
Show Answer
Answer: b
Explanation
By convention, self refers to the current instance of the class inside instance methods.

44) What is the output of “hello”.find(“l”)?
  1. 2
  2. 3
  3. 1
  4. 0
Show Answer
Answer: a
Explanation
"hello".find("l") returns the index of the first l, which is 2 (0-based indexing).

45) Which data structure stores keys and values?
  1. List
  2. Tuple
  3. Dictionary
  4. Set
Show Answer
Answer: c
Explanation
A dictionary stores data as key-value pairs, e.g. {"name": "Alice", "age": 25}.

46) How do you create an anonymous or inline function in Python?
  1. def
  2. lambda
  3. func
  4. inline
Show Answer
Answer: b
Explanation
lambda creates a small anonymous function, e.g. lambda x: x + 1.

47) What method removes all items from a dictionary?
  1. deleteAll()
  2. remove()
  3. clear()
  4. purge()
Show Answer
Answer: c
Explanation
clear() removes all key-value pairs from a dictionary, leaving it empty.

48) What is the output of 10 % 3?
  1. 3
  2. 1
  3. 0
  4. 3.33
Show Answer
Answer: b
Explanation
% is the modulo operator. 10 % 3 gives the remainder 1.

49) Which method converts a list of strings into a single string separated by a delimiter?
  1. concat()
  2. join()
  3. merge()
  4. split()
Show Answer
Answer: b
Explanation
join() combines a list of strings using a specified delimiter, e.g. "-".join(["a", "b"]).

50) What is the result of str(123)?
  1. 123
  2. ‘123’
  3. Error
  4. [123]
Show Answer
Answer: b
Explanation
str(123) converts the integer 123 into the string '123'.

51) How do you open a file named “test.txt” for reading in Python?
  1. open(“test.txt”, “r”)
  2. open(“test.txt”, “w”)
  3. open(“test.txt”, “read”)
  4. file(“test.txt”)
Show Answer
Answer: a
Explanation
The mode "r" opens a file for reading: open("test.txt", "r").

52) What method splits a string into a list of substrings?
  1. slice()
  2. split()
  3. break()
  4. separate()
Show Answer
Answer: b
Explanation
split() breaks a string into a list of substrings based on a delimiter.

53) Which keyword is used to test if a sequence contains a specific item?
  1. in
  2. contains
  3. exists
  4. has
Show Answer
Answer: a
Explanation
Use in to test membership, e.g. 3 in [1, 2, 3] returns True.

54) What is the output of list(range(3))?
  1. [1, 2, 3]
  2. [0, 1, 2, 3]
  3. [0, 1, 2]
  4. [1, 2]
Show Answer
Answer: c
Explanation
range(3) produces 0, 1, 2, so converting it to a list gives [0, 1, 2].

55) Which error occurs when a variable is used before it is defined?
  1. ValueError
  2. TypeError
  3. NameError
  4. KeyError
Show Answer
Answer: c
Explanation
Using an undefined variable raises a NameError in Python.

56) What does “is” operator evaluate?
  1. Values equality
  2. Identity (same object in memory)
  3. Data types equality
  4. Length equality
Show Answer
Answer: b
Explanation
is checks whether two references point to the same object in memory, not just equal values.

57) What is the output of “a” + “b”?
  1. ab
  2. a b
  3. Error
  4. [“a”, “b”]
Show Answer
Answer: a
Explanation
The + operator concatenates strings, so "a" + "b" gives "ab".

58) How do you get the last item of a list lst?
  1. lst[last]
  2. lst[-1]
  3. lst[0]
  4. lst.last()
Show Answer
Answer: b
Explanation
Negative indexing works from the end, so lst[-1] returns the last item.

59) Which block is always executed whether an exception occurs or not?
  1. try
  2. except
  3. else
  4. finally
Show Answer
Answer: d
Explanation
The finally block always runs, whether an exception occurred or not.

60) What keyword creates a generator function?
  1. return
  2. yield
  3. generate
  4. produce
Show Answer
Answer: b
Explanation
A function containing yield returns a generator, producing values lazily one at a time.

61) What is the output of min([5, 2, 8, 1])?
  1. 5
  2. 2
  3. 8
  4. 1
Show Answer
Answer: d
Explanation
min() returns the smallest value, which is 1.

62) Which method adds all elements of an iterable to a list?
  1. append()
  2. extend()
  3. add()
  4. push()
Show Answer
Answer: b
Explanation
extend() adds each element from an iterable to the end of a list.

63) What will set([1, 2, 2, 3]) yield?
  1. {1, 2, 2, 3}
  2. {1, 2, 3}
  3. [1, 2, 3]
  4. (1, 2, 3)
Show Answer
Answer: b
Explanation
A set removes duplicates, so {1, 2, 2, 3} becomes {1, 2, 3}.

64) Which module in Python provides tools for working with regular expressions?
  1. regex
  2. re
  3. pyre
  4. string
Show Answer
Answer: b
Explanation
The built-in re module provides regular expression matching operations.

65) What is the correct syntax for a list comprehension that doubles each element in nums?
  1. [x * 2 for x in nums]
  2. for x in nums double(x)
  3. [double(x) : x in nums]
  4. [x for x * 2 in nums]
Show Answer
Answer: a
Explanation
A list comprehension follows the form [expression for item in iterable], so [x * 2 for x in nums] is correct.

66) What function returns the length of an object?
  1. count()
  2. size()
  3. len()
  4. length()
Show Answer
Answer: c
Explanation
len() returns the number of items in an object such as a string, list, tuple or dictionary.

67) What is the output of bool(None)?
  1. True
  2. False
  3. None
  4. Error
Show Answer
Answer: b
Explanation
None is falsey, so bool(None) returns False.

68) How do you convert a string to lowercase?
  1. lower()
  2. toLower()
  3. downcase()
  4. small()
Show Answer
Answer: a
Explanation
lower() returns a copy of the string converted to lowercase.

69) Which method reverses a list in place?
  1. reverse()
  2. reversed()
  3. flip()
  4. invert()
Show Answer
Answer: a
Explanation
reverse() reverses the list in place. reversed() returns an iterator instead.

70) What is the result of “Python”[::-1]?
  1. Python
  2. nohtyP
  3. P
  4. n
Show Answer
Answer: b
Explanation
The slice [::-1] reverses the string, so "Python"[::-1] gives nohtyP.

71) Which operator is used for bitwise AND?
  1. and
  2. &
  3. &&
  4. |
Show Answer
Answer: b
Explanation
& performs bitwise AND. and is the logical AND operator.

72) What built-in module gives access to math functions like sqrt and sin?
  1. math
  2. sys
  3. cmath
  4. numbers
Show Answer
Answer: a
Explanation
The math module provides functions such as sqrt(), sin(), cos() and constants like pi.

73) What is the primary constructor method for a Python class?
  1. __create__()
  2. __init__()
  3. __construct__()
  4. __main__()
Show Answer
Answer: b
Explanation
__init__() is the initializer/constructor method called automatically when a new instance is created.

74) What is the result of float(5)?
  1. 5
  2. 5.0
  3. “5.0”
  4. Error
Show Answer
Answer: b
Explanation
float(5) converts the integer 5 to the floating-point number 5.0.

75) What does the pass statement do?
  1. Terminate program execution
  2. Skip to the next iteration
  3. Do nothing (null statement)
  4. Return a value
Show Answer
Answer: c
Explanation
pass is a null statement. It does nothing and is used as a placeholder where code is syntactically required.

76) How do you pass keyword-based variable arguments to a function?
  1. *args
  2. **kwargs
  3. &kwargs
  4. #args
Show Answer
Answer: b
Explanation
**kwargs collects extra keyword arguments into a dictionary.

77) What exception is raised when dividing by zero?
  1. ArithmeticError
  2. ZeroDivisionError
  3. NullPointerError
  4. MathError
Show Answer
Answer: b
Explanation
Division by zero raises a ZeroDivisionError in Python.

78) Which built-in function pairs elements from two or more iterables?
  1. pair()
  2. zip()
  3. combine()
  4. map()
Show Answer
Answer: b
Explanation
zip() combines elements from multiple iterables into tuples, stopping at the shortest iterable.

79) What will all([True, True, False]) evaluate to?
  1. True
  2. False
  3. None
  4. Error
Show Answer
Answer: b
Explanation
all() returns True only if every element is true. Since one element is False, the result is False.

80) What will any([False, True, False]) evaluate to?
  1. True
  2. False
  3. None
  4. Error
Show Answer
Answer: a
Explanation
any() returns True if at least one element is true. Since one element is True, the result is True.

81) What is the output of 3 == 3.0?
  1. True
  2. False
  3. Error
  4. None
Show Answer
Answer: a
Explanation
In Python, 3 == 3.0 is True because their numeric values are equal.

82) How do you delete a variable in Python?
  1. delete x
  2. del x
  3. remove(x)
  4. drop x
Show Answer
Answer: b
Explanation
The del statement deletes a variable reference, e.g. del x.

83) Which statement opens a file and automatically closes it when finished?
  1. open file as f:
  2. using open(“file”) as f:
  3. with open(“file”) as f:
  4. file f = open(“file”)
Show Answer
Answer: c
Explanation
The with open("file") as f: statement ensures the file is automatically closed after use.

84) What is the output of “Hello, World!”.startswith(“Hello”)?
  1. True
  2. False
  3. None
  4. Error
Show Answer
Answer: a
Explanation
startswith("Hello") returns True because the string begins with “Hello”.

85) What does isinstance(5, int) return?
  1. False
  2. True
  3. int
  4. Error
Show Answer
Answer: b
Explanation
isinstance(5, int) checks whether 5 is an instance of int, which is True.

86) What is the value of x after x = 10; x += 5?
  1. 10
  2. 5
  3. 15
  4. 50
Show Answer
Answer: c
Explanation
x += 5 is equivalent to x = x + 5, so 10 + 5 = 15.

87) How do you copy a list lst without modifying the original?
  1. lst.copy()
  2. lst[:]
  3. list(lst)
  4. All of the above
Show Answer
Answer: d
Explanation
lst.copy(), lst[:] and list(lst) all create a shallow copy of the list.

88) What method inserts an element at a specific index in a list?
  1. append()
  2. add()
  3. insert()
  4. push()
Show Answer
Answer: c
Explanation
insert(index, item) inserts an item at the specified index in a list.

89) What module provides access to command-line arguments?
  1. os
  2. sys
  3. process
  4. env
Show Answer
Answer: b
Explanation
The sys module exposes command-line arguments through sys.argv.

90) What is the output of “Py” * 3?
  1. Py3
  2. PyPyPy
  3. Error
  4. Py Py Py
Show Answer
Answer: b
Explanation
String repetition with * repeats the string, so "Py" * 3 gives PyPyPy.

91) Which error occurs when a key is not found in a dictionary?
  1. IndexError
  2. ValueError
  3. KeyError
  4. LookupError
Show Answer
Answer: c
Explanation
Accessing a missing dictionary key raises a KeyError.

92) What function takes a function and an iterable and applies the function to all items?
  1. filter()
  2. map()
  3. reduce()
  4. apply()
Show Answer
Answer: b
Explanation
map(function, iterable) applies the function to every item in the iterable.

93) What function filters elements from an iterable based on a condition function?
  1. filter()
  2. select()
  3. map()
  4. extract()
Show Answer
Answer: a
Explanation
filter(function, iterable) keeps only the items for which the function returns True.

94) What is the output of round(3.57, 1)?
  1. 3.5
  2. 3.6
  3. 4.0
  4. 3
Show Answer
Answer: b
Explanation
round(3.57, 1) rounds to one decimal place, giving 3.6.

95) Which method returns a dictionary’s keys as a view object?
  1. dict.get_keys()
  2. dict.keys()
  3. dict.values()
  4. dict.items()
Show Answer
Answer: b
Explanation
dict.keys() returns a view object containing the dictionary’s keys.

96) What is the output of bool([0])?
  1. False
  2. True
  3. None
  4. Error
Show Answer
Answer: b
Explanation
A non-empty list is truthy even if it contains 0, so bool([0]) returns True.

97) How do you import only the pi constant from the math module?
  1. import pi from math
  2. from math import pi
  3. import math.pi
  4. using math.pi
Show Answer
Answer: b
Explanation
from math import pi imports only the pi constant from the math module.

98) What built-in method converts a list to a tuple?
  1. tuple()
  2. toTuple()
  3. cast_tuple()
  4. set()
Show Answer
Answer: a
Explanation
tuple() converts an iterable such as a list into a tuple.

99) What does the enumerate() function return?
  1. Only the elements of an iterable
  2. Only indices of an iterable
  3. Index-element pairs as tuples
  4. Sorted list of elements
Show Answer
Answer: c
Explanation
enumerate() returns index-element pairs, usually as tuples, while iterating.

100) What is the output of sum([1, 2, 3, 4])?
  1. 10
  2. 24
  3. 4
  4. Error
Show Answer
Answer: a
Explanation
sum([1, 2, 3, 4]) adds the numbers: 1 + 2 + 3 + 4 = 10.

 

MCQ Tutorial
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment