0% found this document useful (0 votes)
20 views5 pages

Python Practice Questions & Answers

The document contains a series of Python practice questions divided into sections, including True/False statements, fill-in-the-blanks, output predictions, code completion and debugging, functions, sets and dictionaries, lambda functions, and scope and loops. Each section provides questions along with their corresponding answers and explanations. It serves as a comprehensive resource for testing and reinforcing Python programming knowledge.

Uploaded by

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

Python Practice Questions & Answers

The document contains a series of Python practice questions divided into sections, including True/False statements, fill-in-the-blanks, output predictions, code completion and debugging, functions, sets and dictionaries, lambda functions, and scope and loops. Each section provides questions along with their corresponding answers and explanations. It serves as a comprehensive resource for testing and reinforcing Python programming knowledge.

Uploaded by

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

PYTHON PRACTICE QUESTIONS WITH ANSWERS (SIDE-BY-SIDE)

SECTION A — True/False + Explanation

1. Default arguments in Python must always come after non-default arguments.


→ True. Non-defaults must come first.

2. A function in Python can return multiple values separated by commas.


→ True. They return a tuple.

3. Tuples in Python are mutable.


→ False. They are immutable.

4. open('[Link]', 'r') will create the file if it does not exist.


→ False. 'r' does not create files.

5. In Python, == checks identity and is checks equality.


→ False. == checks equality, is checks identity.

6. Sets maintain insertion order.


→ False. They are unordered.

7. Dictionary keys must always be strings.


→ False. Keys can be any hashable type.

8. Using [Link]() changes the length of the list by one.


→ True.

9. Strings in Python are immutable.


→ True.

10. Lambda functions can contain multiple statements.


→ False. Only expressions allowed.

11. break exits only the inner-most loop.


→ True.

12. A continue statement skips the execution of the current iteration.


→ True.

13. range(5) generates the numbers 1 to 5.


→ False. It generates 0–4.

14. Two lists with the same values in the same order are always identical (is).
→ False. Identical means same memory address.
15. A Python file must contain a main() function.
→ False.

16. The expression None == 0 evaluates to True.


→ False.

17. A dictionary cannot contain duplicate keys.


→ True. Duplicates overwrite.

18. [Link](5) adds an element only if it doesn't exist already.


→ True.

19. Python automatically garbage-collects unused objects.


→ True.

20. You must close a file after reading it.


→ False technically (auto-GC), but recommended.

21. A module can be imported inside a function.


→ True.

22. Using global keyword inside a function allows modifying global variables.
→ True.

23. List slicing creates a new list.


→ True.

24. try-except blocks prevent all runtime errors.


→ False. Only caught exceptions.

25. Indentation defines code blocks in Python.


→ True.

SECTION B — Fill in the Blanks

26. Keyword to define a function → def


27. A Python set is written using → curly brackets
28. set1 | set2 performs → union
29. set1 & set2 performs → intersection
30. set1 ^ set2 gives elements present in → either set but not both
31. Default argument values are evaluated at → function definition time
32. open('[Link]','w') opens for → writing
33. A function that returns nothing returns → None
34. lambda x: x*3 is a → anonymous function
35. {x for x in range(10) if x%2==0} is → set comprehension
36. 'Python'.lower() returns → python
37. list1 = [1,2,3], list2 = list1 refer to same → object
38. The 'is' operator compares → identity
39. A dictionary is enclosed using → curly brackets
40. To handle exceptions, we use the → except clause

SECTION C — Output Prediction

41. x=5; y=x; y=10 → prints 5


42. a=[1,2,3]; b=a; [Link](4) → [1,2,3,4]
43. {3,1,2,3,2} → length = 3
44. f(5) with default b=2 → 7
45. type(lambda x:x) →
46. for loop with else → 0 1 2 done
47. [Link]("c") → None
48. x[1:] = [2,3]
49. {1,2,3} == {3,2,1} → True
50. (1,2,3)+(4,) → (1,2,3,4)
51. "abc"*3 → abcabcabc
52. {x:x*x for x in range(3)} → {0:0,1:1,2:4}
53. sorted(["anil","amit","bob"]) → ['amit','anil','bob']
54. bool("") → False
55. [i for i in range(5) if i%2==0] → [0,2,4]

SECTION D — Code Completion & Debugging

56. inter(a,b): return a & b


57. filter odd numbers → x % 2 != 0
58. Fix default argument → def f(x, z, y=2):
59. with open("[Link]") as f:
60. Add colon → for i in range(5):
61. fun(b=5, a=10)
62. Sets cannot contain sets → use frozenset({4,5})
63. {1:2,1:3} → length 1 (key overwritten)
64. Indent print
65. square = lambda x: x*x

SECTION E — Functions

66. def greet(name): print("Hello,", name)


67. def f(country="India"): print(country)
68. fun(a=5, b=3)
69. fun(5,3)
70. def f(*args): return sum(args)
71. def f(**kwargs): return kwargs
72. def max3(a,b,c): return max(a,b,c)
73. Reverse string manually: loop from end
74. Count vowels: loop & increment
75. Factorial using loop

SECTION F — Sets, Dicts, Comprehensions

76. a ^ b
77. {x for x in s if 'a' not in x}
78. {i:i*i for i in range(1,11)}
79. set(fruits)
80. x in s
81. d1 | d2
82. d[key] = new_value
83. [w for w in words if len(w)>3]
84. Count chars using dict
85. {x for x in s if x%2!=0}

SECTION G — Lambda, Map, Filter, Reduce

86. lambda x: x*3


87. list(map(lambda x: x+5, lst))
88. filter(lambda x: x%3==0, lst)
89. map([Link], lst)
90. lambda s: [Link]('a')
91. filter(lambda s: len(s)%2==0, lst)
92. [x*x for x in nums]
93. lambda x,y: x*y ; f(3,4)=12
94. sorted(lst, key=lambda x: len(x))
95. reduce(lambda x,y: x*y, lst)

SECTION H — Scope & Loops

96. global variable example


97. local shadowing global
98. print 1–10 except multiples of 3
99. return inside loop exits function
100. function modifies global list

You might also like