0% found this document useful (0 votes)
5 views8 pages

Python MCQ Answer Key

The document contains a comprehensive multiple-choice question (MCQ) answer key for a Python programming test, divided into six sections covering basics, decision structures, loops, functions, lists & strings, and dictionaries, tuples, sets & files. Each question is followed by multiple-choice answers, with the correct answers highlighted in green. The document serves as a study guide or reference for Python programming concepts and syntax.

Uploaded by

oho576398
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

Python MCQ Answer Key

The document contains a comprehensive multiple-choice question (MCQ) answer key for a Python programming test, divided into six sections covering basics, decision structures, loops, functions, lists & strings, and dictionaries, tuples, sets & files. Each question is followed by multiple-choice answers, with the correct answers highlighted in green. The document serves as a study guide or reference for Python programming concepts and syntax.

Uploaded by

oho576398
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python Programming – MCQ Answer Key

Correct answers are highlighted in green

Section A: Basics (Questions 1–20)


1. Identify the correct way to declare a variable with the value 5 in Python.
A. int x = 5 B. x = 5
C. var x = 5 D. declare x = 5

2. Which of the following is a valid multi-line comment in Python?


A. /* This is\na comment */ B. // This is\na comment
C. """This is\na comment""" D. ## This is\na comment

3. Predict the output of: print(type(3.14))


A. <class 'int'> B. <class 'float'>
C. <class 'double'> D. <class 'number'>

4. Which operator is used to check if two values are equal in Python?


A. = B. ===
C. == D. :=

5. Determine the result of: 17 % 5


A. 3 B. 2
C. 3.4 D. 1

6. Select the correct way to print 'Hello, World!' in Python.


A. print("Hello, World!") B. echo("Hello, World!")
C. [Link]("Hello, World!") D. printf("Hello, World!")

7. What is the result of: 2 ** 4


A. 8 B. 16
C. 6 D. 12

8. Identify the data type of: x = True


A. str B. int
C. bool D. NoneType

9. Which of the following variable names is valid in Python?


A. 1name B. my-var
C. _score D. class

10. Calculate the result of: int(7.9)


A. 8 B. 7
C. 7.9 D. Error

11. What does the len() function return when called on the string 'Python'?
A. 5 B. 6
C. 7 D. Error

12. Predict the output of: print(10 / 4)


A. 2 B. 2.5
C. 2.0 D. 3

13. Which keyword is used to check for membership in a list?


A. has B. contains
C. in D. exists

14. Determine the output of: print(bool(0))


A. True B. False
C. 0 D. Error

15. Which function converts a string to an integer?


A. str() B. float()
C. int() D. num()

16. What is the result of: 'Hello' + ' ' + 'World'


A. HelloWorld B. Hello World
C. Error D. Hello+World

17. Identify the escape character used for a newline in Python strings.
A. \t B. \n
C. \r D. \s

18. What will print(round(3.567, 2)) output?


A. 3.56 B. 3.57
C. 3.6 D. 4.0

19. Which of the following correctly converts an integer to a string?


A. int('5') B. str(5)
C. string(5) D. toString(5)

20. Predict the output of: print(abs(-15))


A. -15 B. 15
C. 0 D. Error

Section B: Decision Structures (Questions 21–35)


21. Evaluate: x = 3; print('positive' if x > 0 else 'negative')
A. negative B. positive
C. Error D. None

22. Which keyword handles the case when no if/elif condition is true?
A. default B. else
C. finally D. otherwise

23. Predict: x=5; if x>10: print('A') elif x>3: print('B') else: print('C')
A. A B. B
C. C D. AB

24. Which logical operator returns True only if both conditions are True?
A. or B. not
C. and D. xor

25. Determine: a=10; b=20; if a>b: print(a) else: print(b)


A. 10 B. 20
C. Error D. None

26. What is the result of: not (5 > 3)


A. True B. False
C. None D. Error

27. Identify the correct syntax for a nested if statement in Python.


A. if x>0: { if x<10: print('yes') } B. if x>0:\n if x<10:\n print('yes')
C. if x>0 && x<10: print('yes') D. if (x>0) if (x<10) print('yes')

28. What will print? x=0; if x: print('True') else: print('False')


A. True B. False
C. 0 D. Error

29. Which comparison operator checks if two values are NOT equal?
A. <> B. !==
C. != D. =/=

30. Predict: x=15; if x%2==0: print('Even') else: print('Odd')


A. Even B. Odd
C. Error D. 15

31. What does 'pass' do inside an if block?


A. Exits the if block B. Does nothing (placeholder)
C. Raises an error D. Skips to else

32. Evaluate: (5 > 3) and (10 < 20)


A. False B. True
C. None D. Error

33. Output? marks=75; if marks>=90: print('A') elif marks>=75: print('B') else: print('C')
A. A B. B
C. C D. Error

34. Which is the correct ternary expression syntax?


A. x = 1 if condition else 0 B. x = condition ? 1 : 0
C. x = if condition then 1 else 0 D. x = (condition) -> 1 | 0

35. Predict: x=5; y=10; print(x if x>y else y)


A. 5 B. 10
C. True D. Error

Section C: Loops (Questions 36–50)


36. Output of: for i in range(5): pass; print(i)
A. 0 B. 4
C. 5 D. Error

37. How many times? for i in range(2, 8, 2): print(i)


A. 2 B. 3
C. 4 D. 6
38. Predict: x=1; while x<4: x+=1; print(x)
A. 3 B. 4
C. 5 D. 1

39. Which statement immediately exits a loop?


A. exit B. stop
C. break D. end

40. What does 'continue' do in a loop?


A. Exits the loop immediately B. Skips rest of current iteration
C. Restarts the loop D. Pauses the loop

41. Sum after: total=0; for i in range(1,5): total+=i; print(total)


A. 10 B. 15
C. 6 D. 4

42. Determine: for i in range(3): print(i, end=' ')


A. 1 2 3 B. 0 1 2
C. 0 1 2 3 D. 1 2

43. What is the purpose of the else clause in a for loop?


A. Executes if loop had an error B. Executes if loop completes without break
C. Executes on every iteration D. Executes only if break was called

44. Predict: count=0; for i in range(10): if i%2==0: count+=1; print(count)


A. 4 B. 5
C. 6 D. 10

45. Which function iterates over both index and value of a list?
A. zip() B. range()
C. enumerate() D. index()

46. What will cause an infinite loop?


A. for i in range(10): B. while True: break
C. while True: pass D. for i in []:

47. Final value: x=10; while x>0: x-=3; print(x)


A. -2 B. 1
C. 0 D. -1

48. Predict: for i in range(3): for j in range(2): print('*',end=''); print()


A. ***\n***\n*** B. **\n**\n**
C. ***\n*** D. **\n**

49. What does range(5, 0, -1) produce?


A. [5,4,3,2,1] B. [5,4,3,2,1,0]
C. [4,3,2,1,0] D. [1,2,3,4,5]

50. Determine: result=[x**2 for x in range(4)]; print(result)


A. [0,1,4,9] B. [1,4,9,16]
C. [0,1,2,3] D. [1,2,3,4]
Section D: Functions (Questions 51–65)
51. Which keyword is used to define a function in Python?
A. function B. def
C. func D. define

52. Return of: def greet(name='World'): return 'Hello, '+name; print(greet())


A. Hello, B. Hello, World
C. Error D. None

53. Output: def add(a,b): return a+b; print(add(3,4))


A. 7 B. 34
C. Error D. None

54. What is a function that calls itself called?


A. Iterative B. Lambda
C. Recursive D. Generator

55. Predict: def square(x): return x*x; result=square(square(2)); print(result)


A. 4 B. 8
C. 16 D. 64

56. What does a function return if there is no return statement?


A. 0 B. False
C. None D. Error

57. Which correctly defines a lambda that adds two numbers?


A. lambda a,b: a+b B. def lambda(a,b): return a+b
C. function(a,b) => a+b D. lambda(a,b) { return a+b }

58. result=10; def show(): print(result); show() — what happens?


A. Error: undefined B. 10
C. None D. 0

59. What does *args allow a function to accept?


A. Keyword arguments only B. A variable number of positional arguments
C. Dictionary arguments D. Default arguments only

60. Predict: def count_args(*args): return len(args); print(count_args(1,2,3,4))


A. 3 B. 4
C. 5 D. Error

61. Output: add = lambda x,y: x+y; print(add(10,5))


A. 15 B. 105
C. Error D. None

62. What happens when you pass a mutable list to a function and modify it?
A. A copy is modified; original unchanged B. The original object is modified
C. An error is raised D. The function creates a new list

63. Determine: def f(x, y=5): return x*y; print(f(3))


A. 3 B. 5
C. 15 D. Error
64. What keyword modifies a global variable inside a function?
A. nonlocal B. extern
C. global D. public

65. Predict: def f(**kwargs): return len(kwargs); print(f(a=1,b=2,c=3))


A. 2 B. 3
C. 6 D. Error

Section E: Lists & Strings (Questions 66–80)


66. Result of: [1,2,3] + [4,5]
A. [1,2,3,4,5] B. [5,7]
C. Error D. [[1,2,3],[4,5]]

67. Predict: fruits=['apple','banana','cherry']; print(fruits[-1])


A. apple B. banana
C. cherry D. Error

68. What does [Link]() do without an argument?


A. Removes first element B. Removes last element
C. Removes all elements D. Returns the length

69. Determine: num=[5,3,1,4,2]; [Link](); print(num[0])


A. 5 B. 1
C. 2 D. 3

70. What does 'hello'.split('l') return?


A. ['he','o'] B. ['he','','o']
C. ['h','e','l','l','o'] D. Error

71. Predict: text='Python Programming'; print(text[7:14])


A. Program B. Programm
C. rogrammi D. Error

72. Which method removes leading and trailing whitespace from a string?
A. .clean() B. .trim()
C. .strip() D. .remove()

73. Output of: ','.join(['a','b','c'])


A. a,b,c B. ['a','b','c']
C. abc D. a b c

74. Length: data=[1,[2,3],[4,5,6]]; print(len(data))


A. 3 B. 6
C. 5 D. Error

75. Result of: 'Hello World'.replace('World','Python')


A. Hello Python B. Hello World
C. Python World D. Error

76. Predict: nums=[10,20,30]; [Link](1,15); print(nums)


A. [10,15,20,30] B. [15,10,20,30]
C. [10,20,15,30] D. Error

77. Which string method checks if a string starts with a given prefix?
A. .beginswith() B. .startswith()
C. .prefix() D. .first()

78. Result of: [0]*4


A. [0,0,0,0] B. [0,4]
C. 0000 D. Error

79. Determine: words=['cat','dog','bird']; print('dog' in words)


A. True B. False
C. dog D. Error

80. Index from: 'abcabc'.index('c')


A. 1 B. 2
C. 4 D. 5

Section F: Dictionaries, Tuples, Sets & Files (Questions 81–100)


81. Output? d={'a':1,'b':2}; print([Link]('c',0))
A. None B. Error
C. 0 D. c

82. Which creates an empty set?


A. {} B. set()
C. [] D. ()

83. Predict: t=(1,2,3); print(t[1])


A. 1 B. 2
C. 3 D. Error

84. What happens when you try to modify a tuple?


A. The change is applied B. TypeError is raised
C. A new tuple is returned D. Warning is shown

85. Result: s1={1,2,3}; s2={2,3,4}; print(s1|s2)


A. {2,3} B. {1,2,3,4}
C. {1,4} D. Error

86. What does .keys() return on a dictionary?


A. A list of all values B. A list of all keys
C. A list of key-value tuples D. The number of keys

87. Predict: d={1:'one',2:'two',3:'three'}; print(len(d))


A. 2 B. 3
C. 6 D. Error

88. Which file mode appends without deleting existing content?


A. 'r' B. 'w'
C. 'a' D. 'x'

89. What is the difference between a list and a tuple?


A. Lists hold mixed types; tuples cannot B. Tuples are immutable; lists are mutable
C. Lists are ordered; tuples are unordered D. Tuples allow duplicates; lists do not

90. Determine: s={3,1,4,1,5,9,2,6}; print(len(s))


A. 8 B. 7
C. 9 D. 6

91. Which method removes a key-value pair from a dictionary?


A. .remove(key) B. .delete(key)
C. .pop(key) D. .discard(key)

92. Predict: coords=(10,20); x,y=coords; print(x+y)


A. 10 B. 20
C. 30 D. Error

93. What does [Link](element) do when element is not in the set?


A. Raises KeyError B. Raises ValueError
C. Does nothing D. Returns False

94. Correct way to read all lines of a file into a list?


A. [Link]() B. [Link]()
C. [Link]() D. [Link]()

95. Predict: d={'x':10}; d['y']=20; d['x']=30; print(sum([Link]()))


A. 30 B. 50
C. 60 D. Error

96. Which method returns all key-value pairs as tuples?


A. .pairs() B. .items()
C. .tuples() D. .entries()

97. Output? nums=(1,2,3,2,1); print([Link](2))


A. 1 B. 2
C. 3 D. Error

98. Which statement safely opens and auto-closes a file?


A. file=open('[Link]') B. with open('[Link]') as f:
C. open('[Link]').safe() D. auto open('[Link]')

99. Result: s1={1,2,3}; s2={3,4,5}; print(s1-s2)


A. {1,2} B. {4,5}
C. {1,2,4,5} D. {3}

100. Predict: data={'name':'Alice','score':95}; for k,v in [Link](): print(k,end=' ')


A. name score B. Alice 95
C. name Alice D. Error

You might also like