Dating from year 1991, the Python programming language was considered a gap-filler, a way to write scripts that “automate the boring stuff” or to rapidly prototype applications that will be implemented in other languages.
Python is one of the most popular and widely used programming language. Python runs on every major operating system and platform, and most minor ones too. Many major libraries and API-powered services have Python bindings or wrappers, letting Python interface freely with those services or directly use those libraries.
In this article, we will see the most commonly asked python related interview questions and answers which will help you excel and bag amazing job offers with handsome packages.
Let us start by taking a look at some of the most frequently asked interview questions on Python.
1Q. What do you know about Python ?
Python is an interpreted, high level and object oriented programming language. Python is easy to understand and develop.
2Q. Who developed Python ?
Python was developed by Guido Van Rossum.
3Q. Why Python was named as “Python” ?
G.V Rossum picked the name “Python” from the famous TV show, Monty Python’s Flying Circus and rest is history.
4Q. Python is “dynamically typed”. Explain.
In Python, unlike C/C++, we do not need to declare anything. Assignment statements binds a name to an object, and 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.
5Q. Is Python a scripting language ?
A 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. General purpose programming language has wide range of application domains.
6Q. 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 platform independent. Further, this byte code is converted to machine code using Interpreter.
7Q. What are flavors of Python?
Python flavors refers to the different types of Python compilers. Some flavors are cpython, jython, ironpython, PyPy.
8Q. What is CPython ?
This is the standard python compiler implemented in C language.
9Q. Do you what is Jython ?
This is the implementation of Python programming language which is designed to run on Java platform.
10Q. What are built-in datatype in Python ?
None, int, float, complex, bool, str, bytes, list, tuple, range etc.
11Q. You are given a mathematical expression in the form of string, write a python code to solve it.
Do it with the help of eval() function.
12Q. Give a brief explanation about 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]
13Q. Why tuples are faster than lists ?
Tuples are stored in single block of memory whereas Lists are stored in two blocks, first block for object related information and second block for the value. Hence, tuples are faster than list.
14Q. Differentiate between list and tuple ?
Tuple uses less memory whereas lists uses more memory. We can’t modify tuples but lists can be modified. Tuples are faster than lists.
15Q. What is PEP 8 ?
It is Python Enhancement Proposal. It refers to the formatting of python code for maximum readability and understandability.
16Q. Explain memory management in Python ?
Memory Management in Python is managed by Python Private heap space. All python objects and data structures are stored in heap area. Programmers do not have any direct access to private heap area, interpreter takes care of it.
17Q. Explain Garbage Collection in Python ?
Python has the concept of inbuilt garbage collector. It recycles all the unused memory so that they get available for future use. Q. What is PYTHON PATH ? A. It is an environment variable which we can use to set the additional directories, where modules and packages are looked for.
For MAC :
- Open Terminal.app;
- Open the file ~/.bash_profile in your text editor – e.g. atom ~/.bash_profile;
- Add the following line :
export PYTHONPATH=”/Users/my/code” and save it.
For LINUX :
- Open your terminal;
- Open the file ~/.bashrc in your text editor – e.g. atom ~/.bashrc;
- Add the following line to the end:
export PYTHONPATH=/home/my/code and save it.
18Q. What are python modules ?
19Q. Explain the role of loads() & dumps() methods of JSON module.
- loads() -> for parsing json text/string.
- dumps() -> for converting python code to json string.
20Q. What is the role of ord() method ?
It is used to change a character to integer.
21Q. What the basic difference between list & array ?
Arrays have similar type of elements whereas list have dynamic type elements.
22Q. Explain the role of __init()__ method ?
It is predefined method in python classes. This method is called when any object is created from a class, it basically initializes the attributes of a class.
23Q. What are anonymous functions ?
Lambda functions are popularly called as anonymous functions. Such methods generally have multiple parameters but single statement.
For example :
a = lambda x,y : x+y
print(a(6,8)) will result in 14.
24Q. What is self ?
It is an object or an instance of a class, which is explicitly included as first argument.
25Q. What is pass keyword ?
Pass is a non operational statement. It actually does nothing.
26Q. What is 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.
27Q. How we can generate randomize elements of lists ?
We can do this with the help of shuffle() method, present in random module.
28Q. How pickling works ?
Pick an object, convert it to string and then dump it into file, this defines the process of pickling.
29Q. What are Generator functions ?
It is just like a normal function which generates a value using yield keyword. Generator object is a python iterator.
30Q. What are decorators ?
Decorators provide a simple and easy syntax for calling higher-order functions. In other words, a decorator 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 programming.
31Q. 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 an element in a list or any other datatype.
32Q. Analyze the role of help() ?
It is used to display the documentation of modules, functions, classes, keywords etc.
33Q. Analyze the role of dir() ?
It returns the list of all valid attributes.
34Q. Give general syntax for ternary operator ?
[ on true ] if [ expression ] else [ on false ]
Example : “odd” if n%2==1 else “even”
35Q. What are *args & **kargs ?
- *args -> It is used when we need to pass unspecified number of parameters within a method.
- **kargs -> It is used when we need to pass unspecified number of parameters within a method but usually used in dictionaries.
36Q. How we can delete files in Python ?
37Q. Explain docstring ?
A String written in triple quotes, which generally gives a complete introduction of class.
38Q. What are different types of variables in Python ?
Instance variable, Static variable, Class variable.
39Q. Explain Instance variable ?
Variables whose separate copy is created in every instances, are instance variable. Ex. If z is an instance variable, and we create 2 instances, then there will 2 different copies of z in these 2 instances. Modification of one variable will not effect other copies.
40Q. How we can access instance variable ?
With the help of dot (.) operator. Q. What are instance methods ? A. Methods that acts 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.
41Q. Explain Static variable ?
They are also referred to as Class variables. In these type of variables, only single copy is available.
42Q. What is namespace ?
A memory block, where names are mapped to objects.
43Q. Explain me the role of Constructor ?
It is defined as the special method for initializing instance variable. Constructor is called at the time of creating objects.
44Q. 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.
45Q. Explain Abstract methods ?
Abstract methods are the methods, that does not have any body, and are further refined in the subclasses. Abstract methods are generally written with decorator @abstractmethod above them.
46Q. Abstract class contains only abstract methods. True ?
No, it can also contain concrete methods.
47Q. In Python, is it possible to write abstract method with body also ? A
48Q. Can we create instances of Abstract class ?
49Q. Can we have nested class in Python ?
Yes, we can have nested class in Python.
50Q. 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 current class, searching in parent class in depth first, left to right fashion, without searching same class twice.
51Q. What is method overloading ?
It refers to the two or more methods having same method name but different parameters as well as body.
52Q. What is method overriding w.r.t classes ?
Whenever a method in a subclass is defined with same definition as that of a method in base class, then this method is said to be overrided. This explains overriding mechanism.
53Q. What is an Interface ?
Interface can be defined as the abstract class containing no concrete methods.
54Q. What is Duck Duck Typing ?
In this, object type is not checked while invoking method or object. Python uses duck duck typing.
55Q. What are Exceptions ?
Exceptions are run time errors that can be handled by a programmer. All these exceptions are represented as classes.
56Q. Can you name some Exceptions that may occur during execution of a program ?
Arithmetic Exception, Type Error, Syntax Error, Assertion Error, EOF Error.
57Q. How can we handle exceptions in Python ?
Using try, except, and finally.
58Q. How a code behaves inside a finally block ?
Finally block is executed every time.
59Q. Tell me the situation when finally block will not be executed ?
When we use sys.exit().
60Q. What do you mean by monkey patching ?
Whenever a class is dynamically modified, then, it is known as monkey patching.
61Q. What do you know about Numpy Package ?
It is a general purpose array processing package. It provides a high performance multidimensional array object and tools for working with these arrays. It is a fundamental package for scientific computing with python. It is used for fast searching, statistics, algebra, histogram etc.
62Q. What are axes ?
In Numpy, dimensions are called axes. The number of axes, are referred to as rank.
63Q. What is the role of strip() ?
strip() is an inbuilt function in Python programming language that returns a copy of the string with both leading and trailing characters removed.
64Q. What is the role of split() ?
It splits a string into a list where each word is a list item.
65Q. What is the concept of dictionary in python ?
Dictionary is an unordered collection of data items, it is used to store data values like a map. It usually stores the data in the form of key-value pairs.
66Q. How to run a python program on Linux ?
- Python 2 : python <filename>.py
- Python 3 : python3 <filename>.py
67Q. Explain the role of setTrace() method ?
It is present in sys module, responsible for monitoring the code flow of a program.
68Q. Explain the concept of enumerate() ?
It is used to iterate through a sequence, it retrieves index as well as value at same time.
69Q. Why do we use with statement in file handling ?
It will auto close, a file it opens.
70Q. Explain the purpose of tell() & seek() ?
- tell() : It is knowing the position of file pointer.
- seek() : It sets the new position of pointer.
71Q. What is the role of encode() ?
It is to convert a string to byte.
72Q. Explain regex ?
Python has a built-in package called re , which can be used to work with Regular Expressions.
73Q. Write a python code for retrieving all words starting with ‘a’ from a string ” mystring “.
res = re.findall(r ‘ a[\w]* ‘, mystring)
74Q. What is a Thread ?
Thread is a smallest executable unit. Q. How we can create threads in Python ? A.
75Q. What is GIL ?
It is Global Interpreter Lock. It does not allow more than one thread to execute at a time.
76Q. What are Daemon Threads ?
Daemon Threads are the threads that run continously. They are used for big processing system.
77Q. Explain OOPS methodology ?
The entire OOPS methodology has been derived from a single root called object. In OOPS, all programs involve creation of classes and object.
Five important features of OOPS are :
- Classes & Objects
78Q. What is Procedural oriented programming ?
It is the methodology where programming is done using procedures & functions.
79Q. Is Python an object oriented or object based programming language ?
80Q. Explain Encapsulation with example ?
Encapsulation is a mechanism where the data and the code that act on the data will bind together. Example : Class is an example of encapsulation. As class is the collection of data members and member functions, hence class binds them together.
81Q. Explain Abstraction with example ?
Abstraction refers to the act of hiding the background details and providing only necessary details to the user.
Example : Let us the example of a car. While driving car, driver(user) only knows basic operations like gearing system, brakes, but is totally unaware about the internal happenings and processes. This is abstraction.
82Q. Explain Polymorphism with example ?
Polymorphism represents the ability to assume several different forms of a program.
Example : Overloading of methods is an example of Polymorphism. Other examples include, abstract classes and interfaces.
83Q. Explain Inheritance with example ?
Inheritance refers to the process in which child class inherits the properties of its parent class.
Example : Suppose we are having two classes A and B, so
class B(A) -> Child class B, is inheriting the property of its parent class A.
84Q. What is Flask ?
Flask is a light weighted web micro framework, It is used to build web applications with Python.
85Q. What is Django ?
It is an open source framework, to ease up the creation of complex database oriented websites. Django contains prewritten code, unlike flask.
86Q. What is Pyramid ?
It is a web framework for larger applications, they are heavily configurable.
87Q. Explain SciPy ?
All numerical code resides inside SciPy. It is used for large and complex algebraic functions.
88Q. What is use of MatplotLib ?
It provides basic 3D plotting.
89Q. What is the role of PyChecker ?
PyChecker is the tool that help us find bugs or perform static analysis of code.
- Mostly Asked DBMS Interview Questions
- 150 Important Network Security Multiple Choice Questions and Answers
- 200+ Wireless Multiple Choice Questions and Answers
- SCRUMVersity Approved Agile Coach (SAAC) Questions with Answers
- Important Terms Related to Computer Technology
- OOPS Interview Questions With Answers
- Computer Security Multiple Choice Questions with Answers – Part 4
- Computer Security Multiple Choice Questions with Answers – Part 3
- Computer Security Multiple Choice Questions with Answers – Part 2
- Computer Security Multiple Choice Questions with Answers – Part 1