CodeX
Published in

CodeX

Top 10 Most Asked Python Interview Questions With Answers Part 03

By Muhammad Umair

Top 10 Most Asked Python Interview Questions With Answers Part 03 By Muhammad Umair

Q.21 What is the statement that can be used in Python if a statement is required syntactically but the program requires no action?

pass keyword is used to do nothing but it fulfill the syntactical requirements.

try x[10]:   
print(x)
except:
pass

Use pass keyword over there like:

if a > 0:    
print("Hello")
else:
pass

Q.22 Does Python support strongly for regular expressions?

Yes, Python Supports Regular Expressions Well. re is an in-built library for the same. There is a lot of other languages that have good support for RegEx- Perl, Awk, Sed, Java etc.

Regular expressions (called REs, or regexes, or regex patterns) are essentially a tiny, highly specialized programming language embedded inside Python and made available through the re module. Using this little language, you specify the rules for the set of possible strings that you want to match; this set might contain English sentences, or e-mail addresses, or TeX commands, or anything you like. You can then ask questions such as “Does this string match the pattern?”, or “Is there a match for the pattern anywhere in this string?”. You can also use REs to modify a string or to split it apart in various ways.

Regular expression patterns are compiled into a series of bytecodes which are then executed by a matching engine written in C. For advanced use, it may be necessary to pay careful attention to how the engine will execute a given RE, and write the RE in a certain way in order to produce bytecode that runs faster. Optimization isn’t covered in this document, because it requires that you have a good understanding of the matching engine’s internals.

Q.23 How do you perform pattern matching in Python? Explain.

Regular Expressions/REs/ regexes enable us to specify expressions that can match specific “parts” of a given string. For instance, we can define a regular expression to match a single character or a digit, a telephone number, or an email address, etc. The Python’s “re” module provides regular expression patterns and was introduce from later versions of Python 2.5. “re” module is providing methods for search text strings, or replacing text strings along with methods for splitting text strings based on the pattern defined.

Q.24 Write a regular expression that will accept an email id. Use the re module.

Ans.

import ree = re.search(r'[0-9a-zA-Z.]+@[a-zA-Z]+.(com|co.in)$' 'JaiRameshwar@gmail.com')e.group()

‘Ramayanwashere@gmail.com’

To brush up on regular expressions, check Regular Expressions in Python.

Garbage Collector & Memory Manager

Q.25 What is Garbage Collection?

The concept of removing unused or unreferenced objects from the memory location is known as a Garbage Collection. While executing the program, if garbage collection takes place then more memory space is available for the program and rest of the program execution becomes faster.

Garbage collector is a predefined program, which removes unused or unreferenced objects from the memory location.

Any object reference count becomes zero then we call that object an unused or unreferenced object Then no.of reference variables which are pointing to the object is known as a reference count of the object.

While executing the python program if any object reference count becomes zero, then internally python interpreter calls the garbage collector and the garbage collector will remove that object from the memory location.

Q.26 How is memory managed in Python?

Python memory is managed by Python private heap space. All Python objects and data structures are located in a private heap. The programmer does not have an access to this private heap and interpreter. Like other programming language python also has garbage collector which will take care of memory management in python.Python also have an inbuilt garbage collector, which recycle all the unused memory and frees the memory and makes it available to the heap space. The allocation of Python heap space for Python objects is done by Python memory manager. The core API gives access to some tools for the programmer to code.

Python has a private heap space to hold all objects and data structures. Being programmers, we cannot access it; it is the interpreter that manages it. But with the core API, we can access some tools. The Python memory manager controls the allocation.

Q.27 Why isn’t all memory freed when Python exits?

Objects referenced from the global namespaces of Python modules are not always deallocated when Python exits. This may happen if there are circular references. There are also certain bits of memory …

Q.28 Whenever you exit Python, is all memory de-allocated? State why is it so.

The answer here is no. The modules with circular references to other objects, or to objects referenced from global namespaces, aren’t always freed on exiting Python.Plus, it is impossible to de-allocate portions of memory reserved by the C library.

Whenever Python exits, especially those Python modules which are having circular references to other objects or the objects that are referenced from the global namespaces are not always de-allocated or freed.It is impossible to de-allocate those portions of memory that are reserved by the C library.On exit, because of having its own efficient clean up mechanism, Python would try to de-allocate/destroy every other object.

Q.29 Is it possible to assign multiple var to values in list?

The multiple assignment trick is a shortcut that lets you assign multiple variables with the values in a list in one line of code. So instead of doing this:

cat = ['fat', 'orange', 'loud']
size = cat[0]
color = cat[1]disposition = cat[2]

Do this:

cat = ['fat', 'orange', 'loud']
size, color, disposition = cat

Q.30 What is __slots__ and when is it useful?

In Python, every class can have instance attributes. By default Python uses a dict to store an object's instance attributes. This is really helpful as it allows setting arbitrary new attributes at runtime.

However, for small classes with known attributes it might be a bottleneck. The dict wastes a lot of RAM. Python can't just allocate a static amount of memory at object creation to store all the attributes. Therefore it sucks a lot of RAM if you create a lot of objects. The usage of __slots__ to tell Python not to use a dict, and only allocate space for a fixed set of attributes.

Example:

1. Object without slots

class MyClass(object):      
def __init__(self, *args, **kwargs):
self.a = 1
self.b = 2if __name__ == "__main__":
instance = MyClass()
print(instance.__dict__)

2. Object with slots

class MyClass(object):      __slots__=['a', 'b']      
def __init__(self, *args, **kwargs):
self.a = 1
self.b = 2if __name__ == "__main__":
instance = MyClass()
print(instance.__slots__)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Muhammad Umair

Muhammad Umair

MERN Stack Developer | Software Engineer| Frontend & Backend Developer | Javascript, React JS, Express JS, Node JS, MongoDB, SQL, and Python