Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1
1.
What is the difference between class variables and instance
variables in Python?
o Answer: Class variables are shared across all instances of a
class, while instance variables are unique to each instance. Class variables are defined within the class but outside any methods, whereas instance variables are usually defined within methods and prefixed with self.
2. Explain the concept of inheritance in Python.
o Answer: Inheritance allows a class (child class) to inherit
attributes and methods from another class (parent class). This promotes code reuse and establishes a relationship between classes. Python supports single, multiple, multilevel, and hierarchical inheritance.
3. What is polymorphism in Python, and how is it
implemented?
o Answer: Polymorphism allows objects of different classes to
be treated as objects of a common superclass. It is implemented through method overriding (where a child class provides a specific implementation of a method already defined in its parent class) and method overloading (achieved through default arguments).
4. How does encapsulation work in Python?
o Answer: Encapsulation restricts access to certain components
of an object, which is achieved using private and protected access modifiers. Private members are prefixed with double underscores (__), and protected members are prefixed with a single underscore (_).
5. What are metaclasses in Python, and why are they used?
o Answer: Metaclasses are classes of classes that define how
classes behave. They allow customization of class creation and can be used to enforce certain constraints or modify class attributes and methods dynamically. Metaclasses are defined by inheriting from the type class.