You are on page 1of 5

Python Interview questions

1. What are the different data types in python?


 [List, Tuple, String, Dictionary, Set, int, float]
2. What is difference between List and Tuple?
 [List are Mutable whereas Tuples are immutable]
3. In which scenario you will use list and tuple? Explain.
4. What is Dictionary?
 [Dictionary is a data type with key value pair where keys are unique]
5. Can None be dictionary key?
 [Yes, as keys can be immutable]
6. How to access elements of Dictionary? Tell all ways which you know.
 d.get(‘key’, ‘’)
 d[‘key’]
7. How to merge two dictionaries?
 [by using update]
8. Give all possible ways to create a dictionary?
9. What is list comprehension? Have you used it in your projects? Give example.
10. What are function decorators? Have you used it in your projects? Give example.
11. What is Generators? How to create Generators? Give example.[using yield]
12. What is Iterators? How to create Iterator? Give example.[define __iter__ and next method in class]
13. What is difference between sort and sorted?[sort is implicit i.e. sorts content in place where as sorted is explicit
i.e. returns new sorted list]
14. What are Higher-order functions?
15. What is lambda function? Give example.
16. Have you used OOPS in your projects? What all OOPS features supported by python?
 Inheritance, Polymorphism, Data Hiding, Abstraction(using ABC module)
17. What is significance of self?
 It represents Object.
18. How to call parent class method from child class?
 Using super()
19. What is significance of Super?
 Used to refer base class
20. What is method overriding? Give example?
 Re-defining method in Child Cass which is already present in Parent/Base Class.
21. What is method overloading? Give example?
 Defining method with same name but with different signature.[in python can be achieved with the help
of *args and **kwargs]
22. Have you used threading? If yes which module u have used for it? Explain in brief.
23. Does python supports multiple inheritance?
 Yes, Python supports multiple inheritance.
24. What is Diamond problem? How it is resolved in python?
25. What is Method Resolution Order?
 C3 Linearization
26. How to reverse string and list using slicing?
 l = [1,2,3,4,5]; l[::-1]
27. Some questions on list slicing.
28. How to order dictionary?
 By using OrderedDict
29. What all data types can act as key for dictionary?
 [All immutable objects can be used as key]
30. Few questions on database beginning from connecting to database to joins in database.
31. What all operations you have used on list, dictionary, string? Give Examples.
32. How to create a function which takes input parameters dynamically? [*args, **kwargs]
33. What is the significance of ** in **kargs?
 [Keyword Arguments]
34. What is the significance of * in *args?
 [Tuple as Argument]
35. What is package? Have you used it? Give example.
36. Have you ever implemented switch case in python? Explain with help of example.
37. What are pyc and pyo files in python? When they are created?
38. l = [‘k1’, ‘v1’, ‘k2’, ‘v2’, ‘k3’, ‘v3’] create dictionary from this list in one line?
 [ dict([(l[i], l[i+1]) for i in range(0, len(l), 2)])]
39. l = [1, 3, 5, 7, 9, 11, 13, 15] Write a program statement to generate list of square nos. from this list.
 [i**2 for i in l]
 map(math.pow, range(1,11), [2]*10)
40. What is static method? What is class method? There usage?
41. What is difference between range & xrange?
 range() creates list where as xrange() is a sequence object that evaluate lazily.
42. range(10), range(1, 10), range(1,10,2) tell output & explain
 range(10)  [0,1,2,3,4,5,6,7,8,9]
 range(1,10)  [1,2,3,4,5,6,7,8,9]
 range(1,10,2)  [1,3,5,7,9]
43. l = [1,2,3]; l1 = l; l.remove(3) will it affect on l1? if yes then why?
44. What are exceptions? What is base of Exception classes? How will generate exception classes?
45. What is inheritance & delegation?
46. What is the difference between Abstract class and Interface?
47. What is loose coupling, how can you achieve it?
48. What is the difference between aggregation and composition?
49. What is association?
50. What is the difference between pass by value and pass by reference, what is the default in Python?
51. What is the difference between Set, List and Map?
52. I have a nested function as follows,
def outerFn():
a = “ameet”
def innerFn():
print a

Will it give any error? If yes, which error and why? If not, then why?

53. I have a tuple t=(1,2,3) and a string s = “A”

I want another tuple t1 as ((1, “A”), (2, “A”), (3, “A”)). Give me all possible solutions. Which is much
better and why?

54. What will be the output of following statements,


I. t = (1, 2); a, b = t [a=1 and b = 2]
II. t = (1, 2, 3) ; a, b = t [it will give error]
55. What is significance of __repr__? Explain in brief.
56. I have a list “a” of numbers, how to get sum of all numbers? Give all possible solutions.
 sum(a)
 reduce(lambda x, y: x+y, a)
 s= 0

for num in a:

s += num
print s

57. I have a list “a” of objects of same type, where n is a property of that object which contains number. How to get
sum of all ‘n’ of objects? Give all possible solutions.
 sum([m.n for m in a])
 reduce(lambda x, y: x.n + y.n, a)
 s= 0

for num in a:

s += num.n

print s

58. What is difference between List and Array?[List can have data of different type where as Array can have same
type of data]
59. How to copy objects in python? What is difference between Shallow Copy and Deep Copy?
60. How to make a variable global? What is difference between local and global variable? Some more questions on
local and global variable.
61. Have you used map function in python? Explain with example.
62. How to create dictionary from two lists? Ans: dict(zip(l1,l2))
63. What is named tuple?

 namedtuple is a factory function for making a tuple class. With that class we can create tuples that are
callable by name also.

64. What is difference between tuple and named tuple?

Named tuples assign meaning to each position in a tuple and allow for more readable, self-documenting code.
They can be used wherever regular tuples are used, and they add the ability to access fields by name instead of
position index.

65. When to used named tuple?

 You should use named tuples instead of tuples anywhere you think object notation will make your code
more pythonic and more easily readable.
 You can also replace ordinary immutable classes that have no functions, only fields with them.

66. What is “Least Astonishment” in Python i.e The Mutable Default Argument? How to resolve it in pythonic
way?
JPMC or BOA Interview questions:

 What are different data types in python?


 What is difference between List and Tuples?
 What is list comprehension? Explain with example.
 Some questions on slicing.
 I have following list

L = [ [1, 2, 3],

[4, 5, 6],

[7, 8, 9] ]

I want to make it like

L = [ [1, 4, 7],

[2, 5, 8],

[2, 6, 9] ]

[Ans: zip(*L)]

 I have a list say “L” which contains numbers from 1 to 100, but one element is missing. How to find the
missing element? Ans: sum(xrange(1,101)) – sum(L)
 I have a list say “L” which contains n numbers or elements, only one element is duplicate. Remove that
duplicate element. Ans: list(set(L))
 Some questions on decorators.
 How memory management done in python? [Ans: http://foobarnbaz.com/2012/07/08/understanding-
python-variables/]
 What all design patters you have used?(Singleton, decorator, façade, proxy etc)
 How to implement Singleton in python?
 Some questions on magic methods?[In Face to Face round] [Ans:
http://www.rafekettler.com/magicmethods.html]
 Have you used Mixin in python?[JPMC] Ans: (Just google it. It is nothing but multiple inheritance.)

A mixin is a special kind of multiple inheritance. There are two main situations where mixins are used:

1. You want to provide a lot of optional features for a class.


2. You want to use one particular feature in a lot of different classes.

 How to get input from Shell? [Ans: raw_input()]


 Questions on generators.
 How to create dictionary from two lists? Ans: dict(zip(l1,l2))
 Can I inherit from one class 2 times? Eg. Class A(B, B): pass[Ans: No, it will give TypeError, duplicate
BaseClass]
 What is lambda function in python? OR What is anonymous functions in python? Give example.
 I have two strings stored in a and b. Both a and b can contain None or ‘’ or string. Print a + b without error.
If a is None or ‘’ it should print only b and vice versa else it should print a + b.
[Ans: a + b if a and b else a or b]

You might also like