Python Interview Questions

Python


Python Interview Question

1. What do you know about Python?

Python is an interpreted, high-level, and object-oriented programming language and easy to understand and develop.

2. Who developed Python?

Python was developed by Guido Van Rossum.

3. Why Python was named as “Python”?

G.V Rossum picked the name “Python” from the famous TV show, Monty Python’s Flying Circus and the rest is history.

4. Python is “dynamically typed”. Explain. 

In Python, unlike C/C++, no need to declare anything. Assignment statements bind a name to an object, and the object can be of any type. If a name is assigned to an object of one type, then later, it may be assigned to an object of another type. It means that Python is dynamically typed.

5. Is Python a scripting language? 

The scripting language is a programming language that does not use a compiler for executing the source code. Rather, it uses an interpreter to translate source code to machine code on the go. Python is capable of scripting but, is considered to be a general-purpose language. The general-purpose programming language has a wide range of application domains.

6. What is Byte code? 

When a python code is compiled, a file (set of instructions ) is generated. this is referred to as Byte code. It is Byte code only, which makes python is platform-independent. Further, this byte code is converted to machine code using Interpreter. 

7. What are the flavors of Python? 

Python flavors refer to the different types of Python compilers. Some flavors are CPython, Jython, IronPython, PyPy. 

8. What is CPython?

This is a standard python compiler implemented in C language. 

9. Do you what is Jython?

This is the implementation of Python programming language which is designed to run on the Java platform. 

10. What is a built-in datatype in Python? 

None, int, float, complex, bool, str, bytes, list, tuple, range, etc. 

11. You are given a mathematical expression in the form of the string, write a python code to solve it. 

Do it with the help of the eval() function. 

12. Give a brief explanation about the list and tuple?

Lists are mutable(can be changed) whereas Tuples are immutable(can’t be changed). Tuples are faster than lists. 

General format for tuple : (4,5,6,9) 

General format for list : [4,5,6,9]

13. Differentiate between list and tuple?

Tuple uses less memory whereas lists use more memory. We can’t modify tuples but lists can be modified. Tuples are faster than lists.

14. What is PEP 8?

It is a Python Enhancement Proposal. It refers to the formatting of python code for maximum readability and understandability. 

15. Explain memory management in Python?

Memory Management in Python is managed by the Python Private heap space. All Python objects and data structures are stored in the heap area. Programmers do not have any direct access to the private heap area, the interpreter takes care of it. 

16. Explain the Garbage Collection in Python?

Python has the concept of an inbuilt garbage collector. It recycles all the unused memory so that they get available for future use. 

17. What is PYTHON PATH? 

It is an environment variable which we can use to set the additional directories, where modules and packages are looked for.

For MAC : 

1. Open Terminal.app; 

2. Open the file ~/.bash_profile in your text editor – e.g. atom ~/.bash_profile; 

3. Add the following line : 

export PYTHONPATH=”/Users/my/code” and save it.

For LINUX : 

1. Open your terminal; 

2. Open the file ~/.bashrc in your text editor – e.g. atom ~/.bashrc; 

3. Add the following line to the end: export PYTHONPATH=/home/my/code and save it.

18. What are python modules? 

Python modules are .py files python executable code. Some builtin modules are math, random, sys, JSON, itertools. 

19. What do you mean by JSON? 

Python inbuilt module JSON is basically used to work with text, written with JavaScript Object Notation. 

20. Explain the role of loads() & dumps() methods of JSON module.

loads() -> for parsing json text/string.

dumps() -> for converting python code to json string.

21. What is the role of the ord() method?

It is used to change a character to an integer. 

22. What the basic difference between list & array?

Arrays have similar types of elements whereas lists have dynamic type elements. 

23. Explain the role of the __init()__ method? 

It is a predefined method in python classes. This method is called when an object is created from a class, it basically initializes the attributes of a class.

24. What are anonymous functions?

Lambda functions are popularly called as anonymous functions. Such methods generally have multiple parameters but a single statement. 

For example : 

a = lambda x,y : x+y 

print(a(6,8)) will result in 14. 

25. What is the self?

It is an object or an instance of a class, which is explicitly included as the first argument. 

26. What is the pass keyword?

Pass is a non-operational statement. It actually does nothing. 

27. What is the continue keyword?

The continue statement skips all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop. 

28. How we can generate randomize elements of lists?

We can do this with the help of the shuffle() method, present in the random module.

29. Differentiate between range and xrange?

range()
  1. it returns lists containing values.
  2. takes more memory.
  3. supports sliding.
xrange()
  1. returns generator objects.
  2. takes less memory.
  3. doesn't support slicing.

30. How pickling works?

Pick an object, convert it to string and then dump it into the file, this defines the process of pickling.

31. What are Generator functions?

It is just like a normal function that generates a value using the yield keyword.
Example:
def simgen():
    yield 1    yield 4    yield 9x=simgen()
print(x._next_())
Output:1
Generator object is a python iterator.

32. What are decorators?

Decorators provide a simple and easy syntax for calling higher-order functions. In other words, it is a function that takes another function and extends the behavior of the latter function without explicitly modifying it. It is referred to as meta programing.

33. Tell me the role of 'is', 'not', 'in' keyword?

is: return true if both operands are equal else false.
not: returns inversion of boolean value.
in: checks for the presence of elements in a list or any other datatype.

34. Analyze the role of help()?

It is used to display the documentation of modules, functions, classes, keywords, etc.

35. Analyze the role of dir()?

It returns the list of all valid attributes.

36. Give general syntax for a ternary operator?

[on_true] if [expression] else [on_false]
Example : “odd” if n%2==1 else “even”

37. What are *args & **kargs?

*args: It is used when we need to pass an unspecified number of parameters within a method.

**kargs: It is used when we need to pass an unspecified number of parameters within a method but usually used in dictionaries.

38. How we can delete files in Python?

import os
os.remove(filename)

39. Expand docstring?

A String wrote in triple quotes, which generally gives a complete introduction of class.

40. What are the different types of variables in python?

Instance variable, Static variable, Class variable.

41. Explain the Instance variable?

Variables whose separate copy is created in evert instances are instance variables. Ex: if c is an instance variable and we create 2 instances, them there will 2 different copies of z in these 2 instances. Modification of one variable will not other copies.

42. How we can access the instance variable?

with the help of a dot(.) operator.

43. What are instance methods? 

Methods that act on the instances of a class are known as instance methods. They generally use self as their first parameter, which refers to the location of that instance in memory.

44. Explain Static methods? 

They are simple methods with no self argument. They work on class attributes not on instance attributes. They can be called through both class and instance.

45. Explain Abstract methods? 

Abstract methods are the methods, that does not have anybody, and are further refined in the subclasses. Abstract methods are generally written with decorator @abstractmethod above them. 

46. Abstract class contains only abstract methods. True?

No, it can also contain concrete methods.

47. In Python, is it possible to write abstract methods with the body also?

Yes.

48. Can we create instances of Abstract class?

No

49. Can we have a nested class in Python?

Yes, we can have a nested class in Python. 

50. What do know about MRO? 

It is Method Resolution Order. It is the order in which Python looks for a method in a hierarchy of classes. Starting from the current class, searching in parent class in depth-first, left to right fashion, without searching the same class twice.