Python Interview
Python Interview
QUESTIONS
GENERAL CONCEPTS
2. What are the benefits of using Python language as a tool in the present
scenario?
The following are the benefits of using Python language:
Object-Oriented Language
High-Level Language
Dynamically Typed language
Extensive support Libraries
Presence of third-party modules
Open source and community development
Portable and Interactive
Portable across Operating systems
28. What is the difference between a shallow copy and a deep copy?
Shallow copy is used when a new instance type gets created and it keeps values
that are copied whereas deep copy stores values that are already copied.
A shallow copy has faster program execution whereas a deep copy makes it slow.
import m
def monkey_function(self):
print "monkey_function()"
m.GeeksClass.function = monkey_function
obj = m.GeeksClass()
obj.function()
.py files contain the source code of a program. Whereas, .pyc file contains the
bytecode of your program. We get bytecode after compilation of .py file (source
code). .pyc files are not created for all the files that you run. It is only created for
the files that you import.
Before executing a python program python interpreter checks for the compiled
files. If the file is present, the virtual machine executes it. If not found, it checks
for .py file. If found, compiles it to .pyc file and then python virtual machine
executes it.
Having .pyc file saves you the compilation time.
numbers = [1, 2, 3, 4, 5]
print(numbers.pop())
oops
1. What is Object Oriented Programming (OOPs)?
Object Oriented Programming (also known as OOPs) is a programming
paradigm where the complete software operates as a bunch of objects
talking to each other. An object is a collection of data and the methods which
operate on that data.
2. Why OOPs?
The main advantage of OOP is better manageable code that covers the
following:
1. The overall understanding of the software is increased as the distance
between the language spoken by developers and that spoken by users.
2. Object orientation eases maintenance by the use of encapsulation. One
can easily change the underlying representation by keeping the methods
the same.
3. The OOPs paradigm is mainly useful for relatively big software.
3. What is a Class?
A class is a building block of Object Oriented Programs. It is a user-defined
data type that contains the data members and member functions that
operate on the data members. It is like a blueprint or template of objects
having common properties and methods.
4. What is an Object?
An object is an instance of a class. Data members and methods of a class
cannot be used directly. We need to create an object (or instance) of the
class to use them. In simple terms, they are the actual world entities that
have a state and behavior.
6. What is Encapsulation?
Encapsulation is the binding of data and methods that manipulate them into
a single unit such that the sensitive data is hidden from the users
It is implemented as the processes mentioned below:
1. Data hiding: A language feature to restrict access to members of an
object. For example, private and protected members in C++.
2. Bundling of data and methods together: Data and methods that
operate on that data are bundled together. For example, the data
members and member methods that operate on them are wrapped into a
single unit known as a class.
7. What is Abstraction?
Abstraction is similar to data encapsulation and is very important in OOP. It
means showing only the necessary information and hiding the other
irrelevant information from the user. Abstraction is implemented using
classes and interfaces.
8. What is Polymorphism?
The word “Polymorphism” means having many forms. It is the property of
some code to behave differently for different contexts. For example, in C++
language, we can define multiple functions having the same name but
different working depending on the context.
Polymorphism can be classified into two types based on the time when the
call to the object or function is resolved. They are as follows:
Compile Time Polymorphism
Runtime Polymorphism
A) Compile-Time Polymorphism
Compile time polymorphism, also known as static polymorphism or early
binding is the type of polymorphism where the binding of the call to its code
is done at the compile time. Method overloading or operator overloading are
examples of compile-time polymorphism.
B) Runtime Polymorphism
Also known as dynamic polymorphism or late binding, runtime polymorphism
is the type of polymorphism where the actual implementation of the function
is determined during the runtime or execution. Method overriding is an
example of this method.
9. What is Inheritance? What is its purpose?
The idea of inheritance is simple, a class is derived from another class and
uses data and implementation of that other class. The class which is derived
is called child or derived or subclass and the class from which the child class
is derived is called parent or base or superclass.
The main purpose of Inheritance is to increase code reusability. It is also
used to achieve Runtime Polymorphism.
It follows a bottom-to-top
It follows a Top-to-Down approach.
approach.
Modifying and updating the code is Modifying the code is difficult as compared
easier. to OOPs.
A class that is abstract can have both An interface can only have
abstract and non-abstract methods. abstract methods.
An abstract class can have final, non-final, The interface has only static
static and non-static variables. and final variables.