0% found this document useful (0 votes)
16 views

Python Interview Questions

Python interview questions

Uploaded by

janhavi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Python Interview Questions

Python interview questions

Uploaded by

janhavi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

1) What is the difference between a module and a package in Python?

Each Python program file is a module that imports other modules like
objects. The folder of a Python programs is called a package of
modules.

2) What is lambda function in Python?

Lambda function is also called as anonymous function,it may have any


no of arguments but single expression
Obj=lambda no.of.arguments:single expression
a = lambda x,y : x+y

3) What is meant by namespace?

A namespace is a system that has a unique name for each and every
object in Python. An object might be a variable or a method

4) Explain the difference between a list and a tuple?

list is a mutable and tuple is not mutable


Search by position is fast in both case
List is represented by [] and tuple is represented by ()

5) Difference between pickling and unpickling

Pickling : The Python pickle is defined as a module which accepts any


Python object and converts it into a string representation. It dumps the
Python object into a file using the dump function; this process is
called pickling.

unpickling : The process of retrieving the original Python objects from


the stored string representation is called as Unpickling.

6) What are decorators in Python?where we can use decorators?

functions are passed as an argument into another function and then


called inside the wrapper function.
Decorators is used to modify or behaviour of a function
Adding additional functionality to the existing function is called
decorator

7) Difference between generators and iterators?

Iterators: In Python, iterators are used to iterate a group of elements,


containers like list.
Generators : generator is a function that returns an yield object which
we can iterate over value of time.
(or)
generator function returns an Yield object that is iterable over value of
time

8) What is the range() function and what are its parameters?

The range() function is used to generate a list of numbers. Only integer


numbers are allowed, and hence, parameters can be both negative and
positive.

9) What are the key features of Python?

 Interpreted
 Dynamically-typed
 Object-oriented
 English-like syntax

10) What are python modules? Name some commonly used built-in
modules in Python?

Python modules are files containing Python code that can be either
function classes or variables.
 os
 sys
 math
 random
 data time
 JSON

11) What is PEP 8?


PEP or Python Enhancement Proposal is provide guidelines for python
how to write code in python

12) Explain the difference between Python arrays and lists.

In python both arrays and lists are used to store data , main difference is
arrays can store of same data type but list can store any type of datatype

13) What is __init__?

In Python,__init__ is a method or constructor. It is automatically called


to allocate memory when a new object or instance of a class is created.
All classes have the __init__ method.

14) Explain the functionality of “break,” “continue,” and “pass.”

Break :Break is used to terminate the loop when specific condition met.
Continue: It is used to terminate the current loop and skip the code of
current loop and continue to next loop
Pass:

15) What is slicing?

slicing means pieces of character which is enclosed in double quotes or


single quotes

16) How does a function return values?

A function uses the ‘return’ keyword to return a value

17) Pass by references

all the parameters (arguments) are passed "by reference" to the


functions. Thus, if you change the value of the parameter within a
function, the change is reflected in the calling function as well.

18) Pass by value

The pass by value is that whenever we pass the arguments to the


function only values pass to the function, no reference passes to the
function. It makes it immutable that means not changeable. Both
variables hold the different values, and original value persists even after
modifying in the function

19) Why do we use join() function in Python?

For concatenating two strings we use joint function in python

20) How is memory managed in Python?

o The Python memory is managed by a Python private heap space.


All the objects and data structures are located in a private heap.
The programmer does not have permission to access this private
heap.
o We can easily allocate heap space for Python objects by the
Python memory manager

o Python also has an inbuilt garbage collector, which recycle all the
unused memory and frees the memory for the heap space.

21) What is the Python decorator?

Decorator is used to modify the behaviour of any class or function

22) What is local and global variable in Python?

Local variable: The variable which is declared inside the function is


called local variable
Global variable : The variable which is declared outside of the function is
called global variable

23) How to send an email in Python Language?

Python provides smtplib and email modules


import smtplib

24) How do you debug a Python program?

In python we will do debug by using modulepdb


25) How you will check bugs in python ?

In python we use pychecker to find out bugs

26) What is the difference between Xrange and+ range?

Xrange returns the xrange object while range returns the list, and uses
the same memory and no matter what the range size
is.However, xrange() is used only in Python 2. x whereas range() is used
in Python 3. x.

27) Which python version you are using ?

Python version : 3.6 to 3.8 we used in our project and latest version is
3.9.2

28) Explain Python List Comprehension?

List comprehensions are used for transforming on list into another list.
Or
Using the list,we need to create another list by applying some filters or
manipulation
Syntax: output = [ <expression> for <variable> in <Reference>]
for ex: list = [i for i in range(1000)]
Print(list)

29) What is inheritance? Types of inheritance

All the properties and attributes of one class derived or used in another
class is called inheritance.
It provides the re-usability of the code.

1. single inheritance
2. Multilevel inheritance - one parent has multiple childs
3. Multiple inheritance - one child has multiple parents

30) What is Abstraction

To hide inner functionality of a function from the user


(or)
Declaring method in a class but not implementing,that declare method
used in sub-classes . for declare method we want to use decorate
function i.e,(@abstractclassmethod)
From abc import ABC,abstractclassmethod ,this is pass in declaring
method class

31) what are the test cases you used in python?

Pytest and unittest

32) Which library is used for file operation

Import os

33) How file operation take place

a file operation takes place in the following order:


1. Open a file
Python has a built-in open() function to open a file.

f = open("test.txt") # open file in current directory


f = open("C:/Python38/README.txt") # specifying full path

2. Read or write (perform operation)

f = open("test.txt", mode='r', encoding='utf-8')

3. Close the file

f = open("test.txt", encoding = 'utf-8')# perform file operations


f.close()

34) What is Multi Threading?

multithreading is to run multiple tasks and function at the same


time ,when one task is not depended on another task
35) How multi processing?

The ability of a system to support more than one processor at the same
time.Applications in a multiprocessing system are broken to smaller
routines that run independently. The operating system allocates these
threads to the processors improving performance of the system.

36) What is the difference between Abstraction and encapsulation?

Abstraction is the method of hiding the unwanted information.


encapsulation is a method to hide the data in a single entity or unit
along with a method to protect information from outside
(or)
The process of protecting the attributes and method from outside of the
class
Encapsulation:-- Information hiding.
Abstraction:-- Implementation hiding.

37) What is polymorphism?

Changing a behaviour of method at a run time

38) We have folder1,folder2 in root directory.How do you get methods


of one folder to another folder?

From folder1 import folder1_name

39) Difference between method overloading and method overriding

method overloading method overriding

In the method overloading, Whereas in the method overriding,


methods or functions must have methods or functions must have
the same name and different the same name and same
signatures. signatures.

Method overloading is performed Whereas method overriding is


between methods within the class. done between parent class and
child class methods.

n method overloading, there is no Whereas in method overriding,


need of more than one class. there is need of at least of two
classes.

40) What is self in Python?

The self is used to represent the instance of the class. With this keyword,
you can access the attributes and methods of the class in python

41) How to do imports for pdf?

Import reportlab
or
Import xhtml2pdf and pisa

42) What is instance variable?

The value of variable is varied from object to object ,then such type of
variable is called instance variable.
For every object a separate copy of instance variable will be
created.
Where we can declare instance variable
A. inside constructor by using self variable
B. Inside instance method by using self variable
C. Outside of the class by using object reference variable

43) What is instance method?

Inside method, if we are using instance variable then such type of


method are called instance methods.In instant method we have to pass
self variable.
By using self variable inside method we can able to access instance
variable

44) What is static variable?

The value of variable not varies from object to object then such type of
variable is called static variab le or class variable
For total class only one copy of static variable will be created and shared
by all objects of that class.
We can access static variable either class name or by object reference
Where we can declare static or class variable
a. we can declare inside the class
b. Inside constructor by using class name
c. inside instance method by using class name
d. Inside method by using class name or cls variable I,e @class method
e. Inside static method by using class name

45) What is class method?

Inside method ,if we are using any class variable or static variable then
such type of method we call as class method
We can declare class method explicitly by using @class method
decorator
For class method we should provide class variable at the time of
declaration
We can call class method by using class name or object reference
variable

46) What is local variable?

The variable which is declared inside method directly,such type of


variable are called local variable or temporary variable
Local variable will be created at the time of execution and destroyed
once the method is completed
Local variable cannot access from outside of method

47) What is static method?

Static method belongs to the class.It is just like a static variable that
bounds to the class rather than the class's object. A static method can be
called without creating an object for the class. It means we can directly
call the static method with the reference of the class name.
(or)
A static method is a method which is bound to the class and not the
object of the class. It can't access or modify class state. It is present in a
class because it makes sense for the method to be present in class.
A static method does not receive an implicit first argument.
48) What are *args and **kwargs ?

There are two types of arguments in a function which are positional


arguments (declared by a name only) and keyword arguments (declared
by a name and a default value).

 When a function is called, values for positional arguments must be


given. Keywords arguments are optional (they take the default value if
not specified).
 *args collects the positional arguments that are not explicitly defined
and store them in a tuple
 **kwargs does the same as *args but for keyword arguments. They
are stored in a dictionary because keyword arguments are stored as
name-value pairs.
 Python does not allow positional arguments to follow keyword
arguments. Thus, we first declare positional arguments and then
keyword arguments.

49) Difference between Deep copy and Shallow copy

Deep copy stores copies of an object's values, whereas shallow


copy stores references to the original memory address. Deep
copy doesn't reflect changes made to the new/copied object in
the original object; whereas, shallow copy does.

50) What is monkey patching

Dynamic modification of a class or module at runtime is called monkey


patching

51) Difference between sorted and sort

sorted() method sorts the given sequence either in ascending order or


in descending order and always return the a sorted list. This method
doesn't effect the original sequence.
sort() function is very similar to sorted() but unlike sorted it returns
nothing and makes changes to the original sequence. Moreover, sort()
is a method of list class and can only be used with lists.

You might also like