Python Advance – Interview Questions – 2


Python advance level questions are included below. Following includes Interview Questions for Experienced Programmer:

Expand All Questions   Collapse All Questions
Question 1: How will you check if a class is subclass of another class?

Answer: You can check if a class is a subclass of another class in Python using the issubclass() function. For example:

issubclass(DerivedClass, BaseClass)

This will return True if DerivedClass is a subclass of BaseClass and False otherwise.

Question 2: How will you check if an object (python variable) is an instance of a class?

Answer: You can check if an object is an instance of a class in Python using the ininstance() function. For example:

isinstance(obj, MyClass)

This will return Ture if obj is an instance of MyClass, and False otherwise.

Question 3: What is finalize in python and how it is used?

Answer: In Python, finalize is a function from the weakref module used to register a cleanup function that will be called when an object is about to be destroyed. It helps manage resources like file handles or network connections. Here’s a simple usage example:

import weakref

class MyClass:
    def __init__(self, name):
        self.name = name

def cleanup(obj):
    print(f"Cleaning up {obj.name}")

obj = MyClass("example")
weakref.finalize(obj, cleanup, obj)
del obj # Output: Cleaning up example
Question 4: What is empty class in Python?

Answer: An empty class in Python is a class that does not contain any attributes or methods. It is defined using the class keyword followed by the class name and a colon. You can use the pass statement to indicate that the class body is intentionally left blank. Following code creates an empty class with no attribute or method.

class EmptyClass:
    pass
Question 5: Is it possible to call parent class without its instance creation?

Answer: Yes, it is possible to call a parent class method without creating an instance by using the class name to directly call the method. This is typically done with class methods or static methods.

class Parent:
    @classmethod
    def class_method(cls):
        return "Class method called"
    
    @staticmethod
    def add(x, y):
        return x + y

class Child(Parent):
    pass

print(Child.class_method())
print(Child.add(1,2))
Question 6: How can you access parent class members in child class?

Answer: You can access parent class members in a child class using the super() function or by directly referencing the parent class. For example:

class Parent:
    def greet(self):
        print("Hello from Parent")

class Child(Parent):
    def greet(self):
        super().greet()  # Using super()
        Parent.greet(self)  # Directly referencing the parent class

c=Child()
c.greet()
Question 7: What is inheritance and its types in Python?

Answer: Inheritance in Python allows a class to inherit attributes and methods from another class. Types include:

Single Inheritance: One parent class, one child class.

Multilevel Inheritance: A class derived from another derived class.

Multiple Inheritance: One child class, multiple parent classes.

Hierarchical Inheritance: Multiple classes derived from a single parent class.

Hybrid Inheritance: A combination of two or more types of inheritance. Like the diamond inheritance.

Question 8: What is the __main__ function in python and how do we invoke it?

Answer: The __main__ function in Python is used to execute code only when the script is run directly, not when imported as a module. You invoke it using:

if __name__ == "__main__":
    print("place your python code here to run")
Question 9: List some static analysis tools for python.

Answer: Several tools can be used for static analysis of Python code, including:

Prospector: Aggregates multiple linters into a single interface

Pylint: Checks for coding standards and errors.

Mypy: Performs static type checking.

Pyflakes: Detects syntax errors and undefined names.

Question 10: What is GIL in python?

Answer: The Global Interpreter Lock (GIL) in Python is a mutex that protects access to Python objects, preventing multiple native threads from executing Python bytecodes simultaneously. 

Question 11: What is pickling and unpickling in python?

Answer: In Python, pickling is the process of converting a Python object into a byte stream, which can be stored in a file or transmitted over a network. Unpickling is the reverse process, where the byte stream is converted back into a Python object. This is useful for saving program state or data persistence.

Example:

import pickle

# Example data
data = {'name': 'Alice', 'age': 30, 'city': 'Wonderland'}

# Pickling the data
with open('data.pkl', 'wb') as file:
    pickle.dump(data, file)

# Unpickling the data
with open('data.pkl', 'rb') as file:
    loaded_data = pickle.load(file)

print(loaded_data)
Question 13: What are the most common built-in modules of python?

Answer: Python comes with a rich set of built-in modules. Here are some of the most commonly used ones:

  1. os: Provides functions for interacting with the operating system.
  2. sys: Provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter.
  3. math: Offers mathematical functions like trigonometry, logarithms, etc.
  4. datetime: Supplies classes for manipulating dates and times.
  5. json: Enables parsing and creating JSON data.
  6. re: Provides support for regular expressions.
  7. random: Implements pseudo-random number generators for various distributions.

These modules help streamline various tasks and enhance the functionality of your Python programs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post