0% found this document useful (0 votes)
338 views32 pages

Python Interview Questions With Answers

ABC stands for Abstract Base Class. It is a predefined class located in the abc module that can be used to create abstract base classes and abstract methods in Python. Some key points about ABC: - ABC is used to define abstract base classes. Any class inheriting from ABC can contain abstract methods. - Abstract methods are defined using the @abstractmethod decorator and don't have an implementation. They must be overridden in child classes. - Abstract classes cannot be instantiated and are meant only to be subclassed. - The abc module provides features like registering classes as abstract, checking if a class implements abstract methods etc. So in summary, ABC helps in implementing abstract base classes and abstract methods as per the abstract

Uploaded by

Krishnq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
338 views32 pages

Python Interview Questions With Answers

ABC stands for Abstract Base Class. It is a predefined class located in the abc module that can be used to create abstract base classes and abstract methods in Python. Some key points about ABC: - ABC is used to define abstract base classes. Any class inheriting from ABC can contain abstract methods. - Abstract methods are defined using the @abstractmethod decorator and don't have an implementation. They must be overridden in child classes. - Abstract classes cannot be instantiated and are meant only to be subclassed. - The abc module provides features like registering classes as abstract, checking if a class implements abstract methods etc. So in summary, ABC helps in implementing abstract base classes and abstract methods as per the abstract

Uploaded by

Krishnq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 32

Python interview questions

What is the difference between list and tuple?


List Tuple
1. List is created by using [ ] symbol. 1. Tuple is created by using ( ) symbol.
2. List elements are mutable 2. Tuple elements are immutable

What is set?
Set is created by using { } symbol.
Set stores elements in random order fashion.
Set doesn’t allow duplicate elements.
What is a dictionary?
Dictionary is used to store key-value pairs.
Keys should be unique, and values can be duplicate.

Eg:

x={
‘email’ : ‘enquiry@techpalle.com’,
‘pw’ : ‘abcd123’,
‘course’ : ‘python’,
‘duration’ : 90
}
How will you create array in python?
Python does not have built-in support for Arrays, but Python
Lists can be used instead.
However we can create array in python by importing array
module.
Eg:
Import array
arr = array.array( ‘i’ , [10,20,30,40] )

What is the difference between array and list?


array list
In array we can store only In list we can store
homogeneous elements. heterogeneous elements also.
What is list comprehension?
List comprehension is a concise way to create a new list
from values of existing list.

What is default parameter?


A function parameter can have a default value. If the function is
called without passing value to that parameter, then that
parameter will get that default value.

Eg:

Def fun(x,y,z=10):
print(x,y,z)

fun(1,2)

In above example z is default parameter.


what is kwargs (key word arguments) ?
Keyword arguments is represented with **
By using kwargs programmer can pass variable
number of key worded arguments.

Eg:
Def f(**x):
print(x)

f(name=‘palle’, course=‘python’, duration=90)


What is the difference between variable number
of arguments and keyword variable number of
arguments? (args vs kwargs)

Variable no of arguments (args) Keyword variable number of


arguments (kwargs)
Variable no of arguments is represented Keyword variable no of arguments is
with * symbol before parameter represented with ** symbol before
parameter.
User can pass any number of values to a User can pass any number of pair of
function which has * parameter elements to a function which has **
parameter
What is lambda or lambda expression?
lambda expression is an un-named function which
contains single expression code
lambda expressions are used for passing a call back
function as parameter.

Eg:

fun = lambda x, y : x + y
print( fun(10,20) )
What is a regex or regular expression?
Regular experession is a string pattern that we want to find
in a given text.

what is re?
re stands for regular expression. re is a predefined
module in python which contains several
predefined regular expression functions which
programmer can use.

What is a generator?
Generator is used create an iterator (of values) which we
can iterate and read elements one by one using a loop.
What is split?
By using split function we can split a string into words
and store into a list.
Split() function expects a separator as parameter,
based on which you want to split the string. A
separator can be a space or any character.

Eg:

s = ‘palle technologies python bangalore’


mylist = s.split(‘ ‘)
print(mylist)  will display output as [‘palle’ , ’technologies’ , ’python’ , ’bangalore’]
What is decorator?
Decorator is a function which wraps other function to
change its behavior, without explicitly modifying it.

Eg:
def wrapper(fun):
def inner():
print(‘hello’)
fun()
print(‘bye’)
return inner

@wrapper
def display():
print(‘display function’)

In the above code wrapper is the decorator for display() function


What is Object oriented programming
language?
• Any Programming language which supports
below 6 features is considered as OOP
language

1. Class
2. Object
3. Encapsulation
4. Abstraction
5. Inheritance
6. Polymorphism
What is a class
class is a virtual entity or a model which is used to create object.
class eg: class Student:
print(‘this is student class’)

What is an object
Object is a real entity.
object eg:
s1 = Student()
What is constructor or init() ?
by using constructor we can assign data into objects or we can
initialize objects

Eg for constructor::
class Bank
{
def __init__(self )
{

}
}
What is encapsulation?
binding logically related data and functions together in
one class is known as encapsulation.

class Doctor:
def suggestMedicine(self):
print(‘doctor will suggest medicine to patient’)
def doSurgery(self):
print(‘doctor will do surgery’)

In the above example we are keeping all the doctor


related functions in one class. So we can say Doctor
class is an example for encapsulated unit.
What is inheritance?
By using inheritance we can re-use the variables and
methods of parent class in child class.
Inheritance is used to reduce code duplication.

Eg:
class Bank:
def addaccount(self):
//code for adding new account in the bank

class HdfcBank(Bank):
def rateofinterest(self):
//code for calculating rate of interest
Does python support method overloading?
No

Can we achieve method overloading in python?


Yes, by using * parameter we can achieve method overloading indirectly

What is method overriding?


Having same method name in parent class and child class with same
number of parameters, is known as method overriding.

class Animal:
def run(self):
print(‘animal runs on 4 legs’)

Class Human(Animal):
def run(self):
print(‘human runs on 2 legs’)
What is polymorphism?
when an entity is appearing with the same name in different
forms then that entity is said to exhibit polymorphism

method over riding is an example of polymorphism, because


parent class and child class will have same method name but
code will be different.

What is final method?


We can’t over ride final method in child class.
What is final class?
We can’t inherit final class.
What is self keyword

self keyword is used to refer same class instance


variables and same class instance methods.

What is super keyword?


By using super keyword we can access parent
class variables and methods from child class.
What is class variable?
If we declare a variable at class level it is considered
as a class variable. we have to use class name to
access class variable.

What is the dif between instance variable and class


variable?
Instance variable Class variable

instance variables will be declared Class variables are declared at the


with self keyword as prefix. class level without self keyword.
For accessing instance variables we For accessing class variables we
need to use object. need to use class name.
What is dif between @staticmethod and @classmethod?

Static method Class method

If we use @staticmethod decorator If we use @classmethod decorator


above a method name, then that above a method name, then that
method is considered as static method is considered as static method.
method.
for calling static method we use class For calling class method we use class
name. name.
Static method will not have any Every class method will have cls as
parameters by default. parameter by default, using which we
can access class variables.
• What is the dif between instance method, and
class method?
Instance method Class method

Instance methods will have self as Class method will cls as parameter,
parameter, with which we can access with which we can access class
instance variables of the class. variables.
For accessing instance methods we For accessing class methods we need
need to use object. to use class name.
What is the parent most class for all classes?
object class
How will you access, members of other modules?
By using import keyword we can import one module into
other module and access its members.

What all the access specifiers supported in python?


Python explicitly doesn’t have any access specifiers. But we
can prefix a variable with 2 underscores or 1 underscore to
mark it as private or protected respectively.

How will you create public variable in python?


Public keyword is not available in python.
By default all the variables, methods, and classes are
considered public in python, which we can access from any
where.
How will you create private variable in python?
If we use 2 underscores before a variable or method, it is
considered as private member of that class. We can
access that member only with in the same class.
Eg: __x = 10 #here x is considered as private

How will you create protected variable in python?


If we use 1 underscore before a variable or method, it is
considered as protected member which can be
accessible only with in the same class as well as in the
child class.
Eg: _y = 20 #here y is considered as protected

How to create a default variable in python?


It is not possible.
What is an abstract method
def:- by specifying @abstractmethod decorator above
the method name we can mark a method as abstract
method.
Generally abstract methods will not have method bodies.

eg:

@abstractmethod
public abstract void fun(self):
pass
What is an abstract class?
def: if a class contains one or more abstract methods
then it is called as abstract class.
An abstract class must inherit predefined class ABC.

Eg:

class xyz(ABC):
@abstractmethod
def f1(self):
pass
What is is ABC?
ABC stands for abstract base class.
ABC is a predefined class in abc module of
python. We need to inherit ABC class to make
a class as abstract class.

What is abc?
It is a predefined module in python which
contains ABC class.
we need to import abc module to use ABC class.
When to use abstract method?
If you know only method name but you don’t know
the logic, then we make that method as abstract
method.
Can we create object for an abstract class?
No
How do you use an abstract class?
We use an abstract class by inheriting into another
class, and by over riding all abstract methods of
parent class in the child class.
Does python support multiple inheritance of
classes?
yes
What is abstraction?

def: hiding implementation details and exposing the


required details to the users is known as abstraction.
How to achieve abstraction in python?
By using abstract class and abstract methods we
can achieve abstraction in python.

class Bank(ABC):
@abstractmethod
def deposit(self, amount):
pass
@abstractmethod
@withdraw(self, amount):
pass

In the above class we are only showing what all the methods bank class is
having, but we are hiding the actual code how we can deposit or withdraw
money. Hence above class is an example for abstraction.
What is an exception?
Exception is a run time error which terminates
the program abruptly.
How do you handle exceptions?
By using try-except blocks
Can we have multiple except blocks for one try
block?
Yes
What is finally block?
Finally block code will be executed in all the
scenarios no matter whether exception occur or
does not occur.
What is the use of finally block?
Finally block is mainly used for closing files, and
closing database connections.
One try block can have how many finally blocks?
Maximum one
Is it possible to have a try block without except
block?
Yes we can have try without except blocks in case if
finally block is available.

You might also like