InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
How will you check if a class is a child of another class? |
|
Answer» This is DONE by using a method called issubclass() provided by PYTHON. The method tells us if any class is a child of another class by returning true or false accordingly.
|
|
| 2. |
What is init method in python? |
|
Answer» The init method WORKS similarly to the constructors in Java. The method is run as soon as an object is instantiated. It is useful for initializing any attributes or default behaviour of the object at the time of instantiation. |
|
| 3. |
Why is finalize used? |
|
Answer» Finalize method is USED for freeing up the unmanaged resources and CLEAN up before the GARBAGE collection method is invoked. This helps in performing memory MANAGEMENT TASKS. |
|
| 4. |
Differentiate between new and override modifiers. |
|
Answer» The new MODIFIER is USED to INSTRUCT the compiler to use the new implementation and not the base class FUNCTION. The Override modifier is useful for OVERRIDING a base class function inside the child class. |
|
| 5. |
How is an empty class created in python? |
|
Answer» An EMPTY class does not have any members defined in it. It is CREATED by using the pass KEYWORD (the pass command does nothing in PYTHON). We can CREATE objects for this class outside the class. Output: |
|
| 6. |
Is it possible to call parent class without its instance creation? |
|
Answer» Yes, it is possible if the BASE class is INSTANTIATED by other CHILD CLASSES or if the base class is a static METHOD. |
|
| 7. |
Are access specifiers used in python? |
|
Answer» Python does not make use of access specifiers specifically like PRIVATE, public, protected, etc. However, it does not derive this from any variables. It has the concept of IMITATING the behaviour of variables by making use of a single (protected) or DOUBLE underscore (private) as prefixed to the variable names. By default, the variables without prefixed underscores are public. Example: # to demonstrate access specifiersclass InterviewbitEmployee: # protected MEMBERS _emp_name = None _age = None # private members __branch = None # constructor def __init__(self, emp_name, age, branch): self._emp_name = emp_name self._age = age self.__branch = branch #public member def display(): print(self._emp_name +" "+self._age+" "+self.__branch) |
|
| 8. |
How do you access parent members in the child class? |
|
Answer» Following are the ways using which you can access parent class members within a CHILD class:
|
|
| 9. |
How does inheritance work in python? Explain it with an example. |
|
Answer» Inheritance gives the power to a class to access all attributes and methods of another class. It aids in CODE reusability and helps the developer to maintain applications without redundant code. The class INHERITING from another class is a child class or also called a derived class. The class from which a child class derives the members are called parent class or superclass. Python supports different kinds of inheritance, they are:
|
|
| 10. |
How do you create a class in Python? |
|
Answer» To create a class in python, we use the keyword “class” as shown in the example below: class InterviewbitEmployee: def __init__(SELF, emp_name): self.emp_name = emp_nameTo instantiate or create an object from the class created above, we do the following: emp_1=InterviewbitEmployee("Mr. Employee")To access the name attribute, we just call the attribute using the DOT operator as shown below: print(emp_1.emp_name)# Prints Mr. EmployeeTo create methods inside the class, we include the methods under the scope of the class as shown below: class InterviewbitEmployee: def __init__(self, emp_name): self.emp_name = emp_name def introduce(self): print("Hello I am " + self.emp_name)The self parameter in the init and introduce functions represent the reference to the CURRENT class instance which is used for accessing attributes and methods of that class. The self parameter has to be the first parameter of any method defined inside the class. The method of the class InterviewbitEmployee can be accessed as shown below: emp_1.introduce()class InterviewbitEmployee: def __init__(self, emp_name): self.emp_name = emp_name def introduce(self): print("Hello I am " + self.emp_name) # create an object of InterviewbitEmployee classemp_1 = InterviewbitEmployee("Mr Employee")print(emp_1.emp_name) #print employee nameemp_1.introduce() #introduce the employee |
|