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

python irerview question and answers

Uploaded by

hotchat8686
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

python irerview question and answers

Uploaded by

hotchat8686
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Mostly Asked Questions (1-10 Ques-Ans)

what ORM in python?

- Object-relational mapping (ORM) is a design pattern that helps streamline


communication between relational databases and object-oriented
programming languages like Java or Python.

Why do we need ORM Python?

- Handles the logic required to interact with databases. Improves security.


ORM tools are built to eliminate the possibility of SQL injection attacks. You
write less code when using ORM tools than with SQL.

what is MRO(method resolution order)in python?

- MRO is a concept used in inheritance. It is the order in which a method is


searched for in a classes hierarchy and is especially useful in Python
because Python supports multiple inheritance. In Python, the MRO is from
bottom to top and left to right.

What is the use of MRO in Python?

- Method Resolution Order (MRO) is the order in which Python


looks for a method in a hierarchy of classes. Especially it plays
vital role in the context of multiple inheritance as single method
may be found in multiple super classes.

What MRO is followed in Python by default?

- Python follows a depth-first lookup order and hence ends up calling the
method from class A. By following the method resolution order, the lookup
order as follows. Python follows depth-first order to resolve the methods
and attributes.

- what is shallow copy and deep copy?

ans: shallow copy:will take a copy of the original object and create a
new compound object but if the object we are copying is a compound
object the inner

objects will be the same as the ones found in the original object.

deep copy: A deep copy will take a copy of the original object and
will then recursively take copy of the inner objects which are found (if
any)
- What is 'init' keyword in python?

ans:"__init__" is a reseved method in python classes. It is known as a


constructor in object oriented concepts.

This method called when an object is created from the class and it
allow the class to initialize the attributes of a class

What is 'self' keyword in python?

ans: the self parameter is a reference to current instance of class,and is


used to access varibles that belongs to

class it does not have to be named self you can call whatever you
like but it has to be the first parameter of any function in class
What is polymorphism? Polymorphism refers to having
multiple forms. Polymorphism is a programming term
that refers to the use of the same function name, but with
different signatures, for multiple types.

-what is polymorphism in python?

ans: A child class inherits all the methods from the parent class.
However, in some situations

the method inherited from the parent class doesn’t quite fit into the
child class. In such cases, you will

have to re-implement method in the child class.

-what is method overloading in python?

ans: Two methods cannot have the same name in Python; hence
method overloading is a feature that allows the same operator to have
different meanings

Overloading is a method or operator that can do different


functionalities with the same name.

-what is method overriding in python?


ans: The method overriding in Python means creating two methods
with the same name but differ in the programming logic.

The concept of Method overriding allows us to change or override


the Parent Class function in the Child Class.

-What is Encapsulation in Python?

ans: Encapsulation is nothing but process of wrapping code and data


together into single unit and also prevents accidental modification of
the data

what is multithreading in python?

ans: Multithreading in Python is a popular technique that enables


multiple tasks to be executed simultaneously. In simple words, the
ability of a processor to execute multiple threads simultaneously is
known as multithreading.

what is regular expressions in python?


ans: A regular expression is a special sequence of characters that helps
you match or find other strings or sets of strings, using a specialized syntax
held in a pattern. Regular expressions are widely used in UNIX world.

-what is function in python?

ans: A function is a block of code which only runs when it is called.You


can pass data, known as parameters, into a function.

A function can return data as a result.

-what is default arguments in function in python?

ans: the default arguments is nothing but when you use parameter
inside the function but you didn't call parameter

outside of the function by default it takesvalue of the parameter


given inside function is called default arguments

-what is keyword arguments in python?


ans: If you have some functions with many parameters and you want to
specify only some of them, then you can give values

for such parameters by naming them - this is called keyword


arguments

we use the name (keyword) instead of the position (which we have


been using all along) to specify the arguments to the function.

what is break, continue, pass statment in python?

ans: continue: The continue keyword is used to end the current


iteration in a for loop (or a while loop), and continues to the next
iteration.

pass: The pass statement is used as a placeholder for future code.


When the pass statement is

executed, nothing happens, but you avoid getting an error when


empty code is not allowed.

break:'Break' in Python is a loop control statement. It is used to


control the sequence of the loop.

Suppose you want to terminate a loop and skip to the next code
after the loop; break will help you do that

-map(), reduce(), filter()


Python's map() is a built-in function that allows you to process and
transform all the items in an iterable without using an explicit for loop

filter(): The filter() function is used to generate an output list of


values that return true when the function is called. It

This function like map(), can take user-defined functions and


lambda functions as parameters.

reduce(): The reduce() function applies a provided function to


‘iterables’ and returns a single value, as the name implies.

The function specifies which expression should be applied to


the ‘iterables’ in this case. The function tools

module must be used to import this function.(from functools


import reduce)

- Difference Between List and Tuple

ans:list and tuple are heterogeneous ordered data type means we can
assign any type of values like int,string,flot,complex

but lists are mutable datatype and tuple are immutable datatype we
cannot update once we assign the value
- What is dictnorys in python?

Ans: Dictionaries are used to store data values in key:value


pairs.A dictionary is a collection which is ordered*, changeable
and do not allow duplicates. As of Python version 3.7, dictionaries
are ordered. In Python 3.6 and earlier, dictionaries are unordered.

-What is sets in python?

Ans: Sets are used to store multiple items in a single variable.


Set is one of 4 built-in data types in Python used to store
collections of data, the other 3 are List, Tuple, and Dictionary,
all with different qualities and usage. A set is a collection which
is unordered, unchangeable*, and unindexed.also sets dosn't
allows duplicates

- Q How is Exception Handling done in Python?


Answer: There are 3 main keywords:
 Try: Try is the block of a code that is monitored for errors.
 Except: This block gets executed when an error occurs.
 Catch: The beauty of the final block is to execute the code after
trying for error. This block gets executed irrespective of whether
an error occurred or not. Finally block is used to do the required
cleanup activities of objects/variables.
- What is Decorator? Explain it with example.

ans: a decorator is a design pattern that allows a user to add new


functionality to existing object without modifying its strecture-

Difference Between List Comprehension and Dict Comprehension

ans:list comprehension: list comprehension is the short form of the


syntax in between square brackets[] using for loop and conditions to
achive the given functionlity in single line and the return type is in
square brakets[]

even_nums = [x for x in range(21) if x%2 == 0]

print(even_nums)

output: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]


dictionary comprehension:Dictionary comprehension is one way to
create a dictionary in Python.

It creates a dictionary by merging two sets of data which are in the


form of either lists or arrays.

values=[1,2,3,4,5,6,7,8,9,10]

NewDictionary = {x: x**2 for x in values if x**2 % 4 == 0}

print(NewDictionary)

output:{8: 64, 2: 4, 4: 16, 10: 100, 6: 36}

- Difference Between Anonymous and Lambda function

ans: In Python, an anonymous function is a function that is defined without


a name. While normal functions are defined using the def keyword in
Python, anonymous functions are defined using the lambda keyword.
Hence, anonymous functions are also called lambda functions.

-- How is Memory Managed In Python?

ans: Memory management in Python involves a private heap containing


all Python objects and data structures. The management of this private
heap is ensured internally by the Python memory manager
- Difference Between Generators and Iterators

ans: iterators:

An iterator in Python refers to an object that we can iterate upon. The


iterator consists of countable values, and it is possible to traverse through
these values, one by one.

The iterator simply implements the Python's iterator protocol. The iterator
protocol is a Python class which comes with two special methods,
namely __iter__() and __next__() . With these two methods, the iterator is
able to compute the next value in the iteration.

Generators:

Generators are basically functions that return traversable


objects or items. These functions do not produce all the items
at once, rather they produce them one at a time and only
when required.
- Difference Between Modules and Packages in Python

ans:a module code contain python file with.py extension where A


package, however, is like a directory that holds sub-packages and
modules.

A package must hold the file __init__.py. This does not apply to
modules.

To import everything from a module, we use the wildcard *. But this


does not work with packages.

- Difference Between Range and Xrange?

ans: the diffference resides in memory when using range() the entire
list will be allocated in memory where as xrange() returns a
genarator(actually it returns an xrange object

which acts as genarator)means it returns last value in the given


range of values .python2 has range() and xrange() functions but in
python3 xrange() has removed range acts as xrange
- What are Generators. Explain it with example.

ans: Generators are basically functions that return traversable objects


or items. These functions do not produce all the items at once, rather
they produce them one at a time and only when required.

Whenever the for statement is included to iterate over a set of


items, a generator function is run. Generators have a number of
advantages as well.

__iter__() and __next__() are generators in python

-What is Polymorphism?

ans:Polymorphism can be defined as a condition that occurs in many


different forms.

It is a concept in Python programming wherein an object defined in


Python can be used in different ways.

It allows the programmer to define multiple methods in a derived


class, and it has the same name as present in the parent class.
- What are inbuild data types available in Python -OR Explain Mutable
and Immutable data Types

ans: numirical(int,flot,complex)

sequence(list,tuple dictinories)

boolen(true,false)

sets

dictionary

Commonly Asked Questions (11-30 Ques-Ans)

- Explain Ternary Operator in Python?

- What is Inheritance In Python


- Difference Between Local Variable and Global Variable in Python

ans: global varibles are declared outside of the function and local are
declared inside the function while global varibles cannot be directly
changed in the function

you can use global keyword to creat a function that will change the
value of global varible

- Explain Break, Continue and Pass Statement

- What is 'self' keyword in python?


ans: the self parameter is a reference to current instance of class,and is
used to access varibles that belongs to

class it does not have to be named self you can call whatever you
like but it has to be the first parameter of any function in class

- Difference Between Pickling and Unpickling?

- Explain Function of List, Set, Tuple or Dictionary?

- What are Python Iterators?

- Explain type conversion in python. (int(), float(), ord(), oct(), str(),


hex(), eval() etc.)

ans:

- What does *args and **kwargs mean? expain

- What is "Open" and "With" statement in python?

Ans: In Python, you can access a file by using the open() method. However,
using the open() method directly requires you to use the close() method to
close the file explicitly. Instead, you can create a context using the with Open
statement in python. It returns a file object, which has methods and attributes
for getting information about and manipulating the opened file

- Different ways to read and write in a file in python?


1. Ans: Read Only (‘r’) : Open text file for reading. The handle is positioned at
the beginning of the file. If the file does not exists, raises the I/O error. This is
also the default mode in which a file is opened.
2. Read and Write (‘r+’): Open the file for reading and writing. The handle is
positioned at the beginning of the file. Raises I/O error if the file does not
exist.
3. Write Only (‘w’) : Open the file for writing. For the existing files, the data is
truncated and over-written. The handle is positioned at the beginning of the
file. Creates the file if the file does not exist.
4. Write and Read (‘w+’) : Open the file for reading and writing. For an existing
file, data is truncated and over-written. The handle is positioned at the
beginning of the file.
5. Append Only (‘a’): Open the file for writing. The file is created if it does not
exist. The handle is positioned at the end of the file. The data being written
will be inserted at the end, after the existing data.
6. Append and Read (‘a+’) : Open the file for reading and writing. The file is
created if it does not exist. The handle is positioned at the end of the file.
The data being written will be inserted at the end, after the existing data.

- What is Pythonpath?

- How exception handled in Python. [Try, Except, Else And Finally Block]

- Difference Between Python 2.0 & Python 3.0

- What is PIP in python

- Where is Python used?

- How to use F string and format or replacement operator in Python?

- How to get list of all keys in a dictionary?


Ans: The methods dict. keys() and dict. values() return lists of
the keys or values explicitly. There's also an items() which
returns a list of (key, value) tuples, which is the most efficient
way to examine all the key value data in the dictionary

- Difference Between Abstration and Encapsulation.

Ans: Definition. Abstraction is hiding the details and


implementation of the code. Encapsulation is hiding the data
and controlling the visibility of the code

Tricky Questions (31-40 Ques-Ans)

- Does Python support multiple inheritence. (Diamond Problem)

- How to initialize empty List, Tuple, Dict or Set?

- What are .py and .pyc files in Python

- How slicing works in string manupulation. Explain [::-1]

- Can you concatenate two tuples? How is it even possible to


concatenate two tuples, since Tuple is Immutable data type.

- Difference Between Python Arrays and Lists

- Difference Between _a, __a, __a__ in python?


- How to read multiple value from single input? --- By Split()

- How to copy and delete a dictionary

Advanced/Rarely Asked Questions (41-52 Ques-Ans)

- How to achieve Multiprocessing and Multithreading in python?

- What is GIL. Explain

- How class and object created in python?

- Explain Namespace and its types in python

- Explain Recursion by Reversing a list.

- What are Unittests in Python

- How to use Map, filter and reduce functions in python?

- Difference Between Shallow copy and deep copy

- How an object be copied in python

- What does term MONKEY PATCHING refer to in python?

- What is operator overloading & Dunder Method.

- Draw Some Pattern/Graphs.

- Bonus Tip

You might also like