In this article, the tech learn has listed out Python Interview Questions and Answers asked by Every Interviewer. After going through each and every question, you will be fully prepared to go for Python interview
Python Interview Questions For Freshers
Q. 1. What is Python Programming language?
Python is a most demanding programming language with threads, objects, automatic memory management, modules and exceptions. Python is very easy, extensible, portable and an open source programming language.
Q. 2. What do you mean by PEP 8?
PEP 8 is a coding agreement, a set of reference about how to make a python code more readable.
Q. 3. Differentiate between list and tuple?
The major difference between list and tuple is as follow:
• List is mutable whereas tuple is not mutable.
• We can hash the Tuple but list cannot be hashed.
Q. 4. What are the supported data types in Python?
There are standard data types in Python −
Q. 5. What is lambda in Python?
Lambada in Python is a single expression unidentified function which is usually used as an inline function.
Q. 6. What do you mean by namespace in Python?
Namespace in Python is a naming system which is especially used for making the names unique for avoiding the naming problems.
Q. 7. Write some usually used built-in modules in Python?
Some of the usually used built-in modules are listed below:
• data time
Q. 8. What are Dict and List comprehensions?
Dict and list comprehensions are basically syntax constructions for making the formation of dictionary easier.
Q. 9. What are the key features of Python?
Main features of Python are:
• Has a large community
• Short and simple
Q. 10. Explain how to delete a file in Python?
For deleting a file in Python we use a command: os.remove (filename) or os.unlink(filename).
Python Coding Interview Questions For Freshers
Q. 11. What is the following function inserts an object at given index in a list?
A – list.index(obj)
B – list.insert(index, obj)
C – list.pop(obj=list[-1])
D – list.remove(obj)
Correct – B
Q. 12. Which of the following variable is invalid?
(D) None of these
Q. 13. What gets printed?
A) new line then the string: c:newtodayabc.txt
B) the text exactly like this: r”c:\new\today\abc.txt”
c) the text like exactly like this: c:\new\today\abc.txt
D) the letter r and then newline then the text: c:\new\today\abc.txt
Q. 14. What gets printed?
def f(): pass print(type(f())) A) <class 'function'> B) <class 'tuple'> C) <class 'NoneType'> D) <class 'str'> Correct A
Q. 15. Code Solve
x = 4.5 y = 2 print(x//y)
Q. 16. What is output of the program?
value = 770 try: value = value except ZeroDivisionError: print('Cannot divide by 0 ', end = '') else: print('Division successful ', end = '') try: value = value /5.5 except: print('Inside except block ', end = '') else: print('Cannot execute simmultaneously', end = '')
a) Cannot divide by 0 Cannot execute simmultaneously
b) Cannot divide by 0
c) Division successful Cannot execute simmultaneouslyInside except block Cannot execute simmultaneously
d) Cannot divide by 0 Inside except block
Correct – C
Q. 17. what is output of the program?
def calculateSq(n): return n*n+n numbers = (2, 3, 4, 5) result = list(map( calculateSq, numbers)) print(result)
c) [4, 9, 16, 25]
d) [6, 12, 20, 30]
18. what is output of the program?
x = [‘ab’, ‘cd’] print(len(list(map(list, x))))
d) None of these
Q. 19. What is output of the program?
def fun(string): s1 = string s = string[::-1] if(s1 == s): return True else: return False print(fun("madam"))
Q. 20. What is output of the program?
def sum(num): if len(num) == 1: return num else: return num + sum(num[1:]) print(sum([2, 4, 5, 6, 7]))
Python Interview Questions for Experienced
Q. 21. What are functions in Python?
Function in Python is a block of code which is performed when we call it to execute. For defining a function in Python, we use def keyword.
Q. 22. List of commonly used Immutable built-in datatypes of Python?
Usually used Immutable built-in datatypes of Python are:
Q. 23. When do we used the Python decorator?
Usually used Mutable built-in datatypes of Python are:
Q. 24. When do we used the Python decorator?
A relative change which we perform in the Python syntax for adjusting the functions rapidly.
Q. 25. What are some basic types of functions available in Python?
Two types of basic functions available in Python which are listed below:
• Built-in function
• User-defined function
Q. 26. What is pickling?
Any Python objects are accepted and converted into string representation by the Pickle module which uses dump function for dumping it into a file and the whole process is called pickling.
Q. 27. What are Python decorators?
A Python decorator is a particular change which we apply in Python syntax for altering the functions simply.
Q. 28. What are the ways for applying reverse string?
There are five ways for applying the reverse string –
• Extended Slice Syntax
Q. 29. What is monkey patching?
Dynamic modifications of a module or a class during the run-time is known as monkey patch.
Q. 30. Does Python supports multiple inheritances?
Yes, Python supports multiple inheritances. A class can be originated from many parent classes.
Python Coding Interview Questions for Experienced
Q. 31. Import re
p = re.compile('\d') print(p.findall("I went to him at 11 A.M. on 4th July 1886"))
a) [‘1’, ‘1’, ‘4’, ‘1’, ‘8’, ‘8’, ‘6’]
d) None of the above
Q. 32. Import re
print(re.split('[a-f]+', 'Aey, Boy oh boy, come here'))
1) [‘On ‘, ‘th Jan 2016, at 11:02 AM’]
2) [”, ‘y, ‘, ‘oy oh ‘, ‘oy, ‘, ‘om’, ‘ h’, ‘r’, ”]
3) [‘A’, ‘y, Boy oh ‘, ‘oy, ‘, ‘om’, ‘ h’, ‘r’, ”]
4) none of these
Q. 33. _ represents an entity in the real world with its identity and behaviour.
a) A method
b) An object
c) A class
d) An operator
Q. 34. What is the output?
y= 6 z = lambda x: x * y print z(8)
d) None of the mentioned
Q. 35. To open a file c:\scores.txt for reading, we use
a) infile = open(“c:\scores.txt”, “r”)
b) infile = open(“c:\scores.txt”, “r”)
c) infile = open(file = “c:\scores.txt”, “r”)
d) infile = open(file = “c:\scores.txt”, “r”)
Q. 36. Class Test:
def __init__(self): print('no-args passed') def __init__(self,x): print('no-args') print(x) t1=Test() t2=Test(10)
a) no-args passed
d) no-args 10
Q. 37. When will the else part of try-except-else be executed?
b) when an exception occurs
c) when no exception occurs
d) when an exception occurs in to except block
Q. 38. Which is the correct operator for power(x^y)?
d) None of the mentioned
Q. 39. What is the return type of function id?
Correct – D
Q. 40. Which of the following is an invalid statement?
(A) abc = 1,000,000
(B) a b c = 1000 2000 3000
(C) a,b,c = 1000, 2000, 3000
(D) a_b_c = 1,000,000
Correct – C