Site icon FahmidasClassroom

99 multiple-choice questions (MCQs) about Python programming

99 Python Mcq

1. What is the correct file extension for Python files?

Answer: C) .py

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

Answer: B) variable_2

3. What will the following code snippet print?

pythonCopy codeprint(type(5 / 2))

Answer: B) <class 'float'>

4. How do you insert comments in Python code?

Answer: B) # comment

5. What is the output of the following code?

pythonCopy codeprint("Hello" + " World")

Answer: B) Hello World

6. How do you create a list in Python?

Answer: A) list = []

7. Which of the following is a mutable data type in Python?

Answer: C) list

8. What is the output of the following code?

pythonCopy codex = [1, 2, 3]
x.append(4)
print(x)

Answer: A) [1, 2, 3, 4]

9. How do you define a function in Python?

Answer: A) def function_name():

10. What will this code output?

pythonCopy codex = [1, 2, 3]
print(x[0:2])

Answer: A) [1, 2]

11. What will be the output of the following code?

pythonCopy codex = "Python"
print(x[1:4])

Answer: B) yth

12. How do you handle exceptions in Python?

Answer: A) try/except

13. What does the len() function do in Python?

Answer: D) All of the above

14. What will the following code print?

pythonCopy codefor i in range(3):
    print(i)

Answer: A) 0 1 2

15. Which method is used to remove whitespace from the beginning and end of a string?

Answer: A) strip()

16. What will the following code output?

pythonCopy codex = {1, 2, 2, 3}
print(len(x))

Answer: A) 3

17. Which keyword is used to create a class in Python?

Answer: A) class

18. How do you access the last item of a list in Python?

Answer: A) list[-1]

19. What is the result of 3 ** 2 in Python?

Answer: A) 9

20. How do you concatenate two strings in Python?

Answer: A) string1 + string2

21. Which of the following is used to start a loop in Python?

Answer: D) Both B and C

22. How do you create a dictionary in Python?

Answer: A) dict = {key1: value1, key2: value2}

23. What will this code output?

pythonCopy codex = "Python"
print(x.upper())

Answer: A) PYTHON

24. How do you create a tuple in Python?

Answer: B) tuple = ()

25. What will the following code output?

pythonCopy codex = "hello"
print(x * 2)

Answer: A) hellohello

26. What will the following code output?

pythonCopy codex = [1, 2, 3]
print(x.pop())

Answer: A) 3

27. How do you define a variable in Python?

Answer: A) var = value

28. What will the following code output?

pythonCopy codeprint("A" * 3)

Answer: A) AAA

29. How do you check if a key exists in a dictionary?

Answer: A) key in dict

30. What does the pop() method do on a list?

Answer: A) Removes and returns the last item

31. What is the output of the following code?

pythonCopy codex = "Python"
print(x[:3])

Answer: A) Pyt

32. How do you write a single-line comment in Python?

2/2ChatGPT

Certainly! Here’s a set of 99 multiple-choice questions (MCQs) about Python programming:

1. What is the correct file extension for Python files?

Answer: C) .py

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

Answer: B) variable_2

3. What will the following code snippet print?

pythonCopy codeprint(type(5 / 2))

Answer: B) <class 'float'>

4. How do you insert comments in Python code?

Answer: B) # comment

5. What is the output of the following code?

pythonCopy codeprint("Hello" + " World")

Answer: B) Hello World

6. How do you create a list in Python?

Answer: A) list = []

7. Which of the following is a mutable data type in Python?

Answer: C) list

8. What is the output of the following code?

pythonCopy codex = [1, 2, 3]
x.append(4)
print(x)

Answer: A) [1, 2, 3, 4]

9. How do you define a function in Python?

Answer: A) def function_name():

10. What will this code output?

pythonCopy codex = [1, 2, 3]
print(x[0:2])

Answer: A) [1, 2]

11. What will the following code output?

pythonCopy codex = "Python"
print(x[1:4])

Answer: B) yth

12. How do you handle exceptions in Python?

Answer: A) try/except

13. What does the len() function do in Python?

Answer: D) All of the above

14. What will the following code print?

pythonCopy codefor i in range(3):
    print(i)

Answer: A) 0 1 2

15. Which method is used to remove whitespace from the beginning and end of a string?

Answer: A) strip()

16. What will the following code output?

pythonCopy codex = {1, 2, 2, 3}
print(len(x))

Answer: A) 3

17. Which keyword is used to create a class in Python?

Answer: A) class

18. How do you access the last item of a list in Python?

Answer: A) list[-1]

19. What is the result of 3 ** 2 in Python?

Answer: A) 9

20. How do you concatenate two strings in Python?

Answer: A) string1 + string2

21. Which of the following is used to start a loop in Python?

Answer: D) Both B and C

22. How do you create a dictionary in Python?

Answer: A) dict = {key1: value1, key2: value2}

23. What will this code output?

pythonCopy codex = "Python"
print(x.upper())

Answer: A) PYTHON

24. How do you create a tuple in Python?

Answer: B) tuple = ()

25. What will the following code output?

pythonCopy codex = "hello"
print(x * 2)

Answer: A) hellohello

26. What will the following code output?

pythonCopy codex = [1, 2, 3]
print(x.pop())

Answer: A) 3

27. How do you define a variable in Python?

Answer: A) var = value

28. What will the following code output?

pythonCopy codeprint("A" * 3)

Answer: A) AAA

29. How do you check if a key exists in a dictionary?

Answer: A) key in dict

30. What does the pop() method do on a list?

Answer: A) Removes and returns the last item

31. What is the output of the following code?

pythonCopy codex = "Python"
print(x[:3])

Answer: A) Pyt

32. How do you write a single-line comment in Python?

Answer: A) # This is a comment

33. What will be the output of this code?

pythonCopy codex = [10, 20, 30]
x[1] = 40
print(x)

Answer: A) [10, 40, 30]

34. What does the isinstance() function do?

Answer: A) Checks if an object is of a specified type

35. What will this code output?

pythonCopy codex = [1, 2, 3]
print(len(x))

Answer: A) 3

36. Which method is used to add an item at the end of a list?

Answer: B) append()

37. How do you convert a string to an integer in Python?

Answer: A) int(string)

38. Which of the following statements is correct for Python dictionaries?

Answer: B) Dictionary keys must be immutable.

39. What is the output of this code?

pythonCopy codex = [1, 2, 3]
x.insert(1, 4)
print(x)

Answer: A) [1, 4, 2, 3]

40. What does the join() method do?

Answer: B) Joins elements of an iterable into a string

41. What will this code output?

pythonCopy codex = "Python"
print(x.find("o"))

Answer: A) 4

42. How do you create an empty set in Python?

Answer: D) set = set()

43. What is the output of the following code?

pythonCopy codex = (1, 2, 3)
print(type(x))

Answer: B) <class 'tuple'>

44. How do you remove an item from a set?

Answer: A) set.remove(item)

45. What will the following code print?

pythonCopy codex = 5
print(f"{x} is a number")

Answer: A) 5 is a number

46. How can you convert a list to a tuple in Python?

Answer: A) tuple(list)

47. Which method is used to add a key-value pair to a dictionary?

Answer: B) update()

48. What will this code output?

pythonCopy codex = "Hello, World!"
print(x[7:])

Answer: A) World!

49. Which of the following operators is used for exponentiation in Python?

Answer: B) **

50. What will the following code output?

pythonCopy codex = [1, 2, 3]
x.extend([4, 5])
print(x)

Answer: A) [1, 2, 3, 4, 5]

51. How do you remove all items from a list in Python?

Answer: A) list.clear()

52. What will the following code output?

pythonCopy codex = 7
if x % 2 == 0:
    print("Even")
else:
    print("Odd")

Answer: B) Odd

53. What does the split() method do on a string?

Answer: A) Splits the string into a list based on a separator

54. Which of the following methods can be used to remove whitespace from both ends of a string?

Answer: A) strip()

55. How do you define a class method in Python?

Answer: A) def method(self):

56. What will this code print?

pythonCopy codex = [1, 2, [3, 4]]
print(x[2][0])

Answer: A) 3

57. How do you get the current working directory in Python?

Answer: A) os.getcwd()

58. What is the output of the following code?

pythonCopy codex = [1, 2, 3]
x.append([4, 5])
print(x)

Answer: A) [1, 2, 3, [4, 5]]

59. How do you check the type of an object in Python?

Answer: A) type(object)

60. What will this code output?

pythonCopy codex = "Python"
print(x[1:5:2])

Answer: C) ytn

61. What does the sorted() function do?

Answer: A) Sorts a list and returns a new list

62. Which method returns the number of occurrences of a specified element in a list?

Answer: A) count()

63. What will the following code output?

pythonCopy codex = "Python"
print(x.replace("o", "a"))

Answer: A) Pythan

64. What is the result of the expression 5 % 2 in Python?

Answer: A) 1

65. How do you create a class in Python?

Answer: A) class MyClass:

66. What will this code output?

pythonCopy codex = [5, 10, 15]
print(x.pop(1))

Answer: A) 10

67. How do you get the number of items in a list?

Answer: B) len(list)

68. What does the append() method do to a list?

Answer: A) Adds an item to the end of the list

69. What is the output of this code?

pythonCopy codex = "hello world"
print(x.title())

Answer: A) Hello World

70. How do you remove a specific item from a list?

Answer: A) list.remove(item)

71. What will this code output?

pythonCopy codex = 4
y = 2
print(x ** y)

Answer: A) 16

72. Which keyword is used to indicate a function is to be used for a specific method of a class?

Answer: D) self

73. What is the output of this code?

pythonCopy codex = "Python"
print(x[::2])

Answer: A) Pto

74. Which method is used to remove and return an arbitrary element from a set?

Answer: B) set.pop()

75. What will this code output?

pythonCopy codex = "Python"
print(x.split("o"))

Answer: A) ['Pyth', 'n']

76. How do you check if a value exists in a dictionary?

Answer: A) value in dict

77. What will this code print?

pythonCopy codex = [1, 2, [3, 4]]
print(x[2][1])

Answer: A) 4

78. What does the int() function do?

Answer: A) Converts a value to an integer

79. What is the output of this code?

pythonCopy codex = {1, 2, 3}
y = {3, 4, 5}
print(x & y)

Answer: A) {3}

80. How do you access elements of a dictionary?

Answer: D) Both A and B

81. What will this code output?

pythonCopy codex = "Python"
print(x[::-1])

Answer: A) nohtyP

82. Which method returns the lowest value in a list?

Answer: B) min(list)

83. What will this code print?

pythonCopy codex = 5
y = 10
print(x + y)

Answer: A) 15

84. How do you define an empty set in Python?

Answer: C) set = set()

85. Which of the following is not a valid Python keyword?

Answer: C) foreach

86. What will this code output?

pythonCopy codex = "Python"
print(x.index("t"))

Answer: B) 3

87. How do you open a file in Python for reading?

Answer: A) open("file.txt", "r")

88. What does break do in a loop?

Answer: A) Exits the loop

89. What is the output of the following code?

pythonCopy codex = "Python"
print(x[1:4])

Answer: A) yth

90. Which of the following is used to comment out multiple lines in Python?

Answer: C) ''' comment '''

91. What does the dict.keys() method return?

Answer: B) A list of dictionary keys

92. How do you handle missing keys in a dictionary?

Answer: A) Use dict.get(key, default)

93. What is the output of the following code?

pythonCopy codex = [1, 2, 3]
y = x.copy()
y.append(4)
print(x)

Answer: A) [1, 2, 3]

94. What is the output of the following code?

pythonCopy codex = {1, 2, 3}
x.add(4)
print(x)

Answer: A) {1, 2, 3, 4}

95. How do you get the maximum value from a list?

Answer: B) max(list)

96. What will this code print?

pythonCopy codex = [1, 2, 3]
x.insert(1, [4, 5])
print(x)

Answer: A) [1, [4, 5], 2, 3]

97. How do you concatenate two strings in Python?

Answer: A) str1 + str2

98. What will this code output?

pythonCopy codex = "Python"
print(x[2:5])

Answer: A) tho

99. What is the output of the following code?

pythonCopy codex = "Python"
print(x[:4])

Answer: A) Pyth

100. How do you create a list in Python?

Answer: B) list = []

Exit mobile version