Python Interview Questions
Python Interview Questions
Introduction
1. Python 2 vs Python 3
2. Compiled and interpreted
3. Python distributions
4. Can you run python on JVM and .Net CLR
5. What is .pyc and .pyb files ?
6. Default Character encoding in python 2 and python 3
7. What is slicing?
8. output ?
x = raw_input()
y = raw_input()
print x + y
Strings
9. Reverse a string in Python
10. #output
“Hello” * 3
11. #output
s = “Hello world! 123$”
s.upper()
12. x = int(“2345.6”) ?
13. Explain indexing and slicing?
14. S = ”Hello world!“
Find the outputs of
S[1:9:2]?
S[::2]?
S[-4:-1:1]?
S[::-1]?
S[3::-1] ?
15. #output
date = ‘12/02/1984’
date [ : 5: -1][ : : -1]
date.rsplit(‘/’, 1)
16. How do you convert string to list?
Operators
17. #output
x = raw_input(“Enter x value”)
y = raw_input(“enter y value”)
print x+y
18. Difference between “ / ” and “ // “ ?
19. #output
21. #output
s1 = “Hello“
s2 =”Polo”
s3 =”Hello”
s1 is s3 ?
22. x = 99
y =100
x = 200
Print “{1}, {0}, {2}, {0}”.format(x,y,z)
23. Is there switch in Python?
58. Program: Find the word with maximum number of occurrences in a large text file.
68. Mention what are the rules for local and global variables in Python?
81. What is the purpose of ZIP, enumerate()? How to unzip list of tuples to multiple lists?
82. How can you implement functional programming and why would you?
Files
90. How do you read line by line from a file in python ?
91. with open(‘abc.tx’) as f:
s = f.read()
What happens if ‘abc.txt’ doesn’t exist?
Oops
92. Is Python object oriented? what is object oriented programming?
93. What is a Class? How do you create it in Python?
96. How to create private variable in Python? Can you access private variables?
Multi-threading
116. What is the difference between concurrency and parallelism?
117. Why threading is not recommended in Python?
118. Difference between threading and multiprocessing?.
119. What is Global interpreter lock(GIL)?
120. How do you prevent dead-lock?
121. What is the benefit of python multi-threading in jython?
122. Explain below terms,
a. Critical section?
b. What is dead-lock ?
c. What is live-lock?
Modules –Packages
123. What is a Python module?
124. How do you import all functions/classes in a module?
125. How do u implement import*
126. When do you get recursive import error, and how do you resolve it?
127. Explain how can you access a module written in Python from C?
128. Name few Python modules for Statistical, Numerical and scientific
computations?
129. What is package in Python?
130. What is the difference between package and directory?
131. What is the purpose of __init__.py in Python?
132. What is __all__?
133. What is the purpose of help, dir functions?
134. What is the purpose of __name__?
135. Private variables/ functions in a module ?
Exception Handling
136. What is the purpose of Exception Handling? How do you achieve it in Python?
137. What are the different types of exceptions generated in python?
138. Why does the exception handling have a finally block?
139. How finally and else works ?
140. Can you have multiple except blocks? In which order we have to arrange
except blocks?
Unit test
141. What is the purpose of unit testing?
142. What you should not test?
143. What is mocking?
144. How do you mock exceptions?
145. How do you write an unit test for a function which has infinity loop?
146. What are the python modules required for unit testing?
147. How do you mock a function?
Miscellaneous
148. What are the various logging levels in python ?
149. How do you create rotating file handler in python?
150. What is ORM?
151. What is virtualenv?
152. What is serialization ? difference between pickle.dumps() vs pickle.dump?
153. Difference between search(), match(), findall() in re module
154. Explain greedy-matching in re module?
155. Explain, broadcasting in numpy arrays ?