PYTHON SECOND INTERNAL EXAM(20-07-2024)

SUJALADITZ

PYTHON  QUESTION AND ANSWERS:-

### 2 Marks ###


1. **Define file. Mention types of file in Python and also modes of file?**


   A file is a named location on a disk to store related information. It is used to store data permanently. When we open a file, we can read from it or write to it.
   Types of files in Python:
   - **Text Files**: Stores data in a readable format.
   - **Binary Files**: Stores data in a binary format (0s and 1s).


   Modes of file:
   - **'r'**: Read (default mode)
   - **'w'**: Write (creates a new file if it doesn't exist or truncates if it does)
   - **'a'**: Append (writes data to the end of the file)
   - **'b'**: Binary mode (used with other modes, e.g., 'rb', 'wb')
   - **'+'**: Update mode (reading and writing), e.g., 'r+', 'w+', 'a+'

2. **What are types of operations on file?**


   Types of file operations:
   - **Opening a file**
   - **Reading from a file**
   - **Writing to a file**
   - **Appending to a file**
   - **Closing a file**


3. **Define class and object. Give syntax?**


   A class is a blueprint for creating objects. It defines a set of attributes and methods that the objects created from the class can use.
   An object is an instance of a class. It is created using the class and can use the attributes and methods defined by the class.
   **Syntax**:
   ```python
   class ClassName:
       # class attributes and methods
   object_name = ClassName()
   ```


4. **Write the syntax for creating class and object?**


   **Syntax**:
   ```python
   class MyClass:
       def __init__(self, attribute1, attribute2):
           self.attribute1 = attribute1
           self.attribute2 = attribute2
       def method1(self):
           # method implementation
   my_object = MyClass(attribute1_value, attribute2_value)
   ```


5. **Define inheritance. What are its types?**


   Inheritance is a mechanism in OOP that allows a class to inherit attributes and methods from another class. The class that inherits is called the child class or subclass, and the class being inherited from is called the parent class or superclass.
   Types of inheritance:
   - **Single Inheritance**: A subclass inherits from a single superclass.
   - **Multiple Inheritance**: A subclass inherits from multiple superclasses.
   - **Multilevel Inheritance**: A subclass inherits from another subclass.
   - **Hierarchical Inheritance**: Multiple subclasses inherit from a single superclass.
   - **Hybrid Inheritance**: A combination of two or more types of inheritance.


6. **Define encapsulation?**


   Encapsulation is the concept of bundling data (attributes) and methods that operate on that data into a single unit or class. It restricts direct access to some of the object's components, which is a means of preventing accidental or unauthorized data modification. Encapsulation is typically achieved using private and protected access specifiers.


7. **Define polymorphism?**


   Polymorphism means "many forms," and it allows objects of different classes to be treated as objects of a common superclass. It provides a way to use a single interface to represent different underlying forms (data types). Polymorphism is achieved through method overriding and method overloading.


8. **What is a constructor? Name types of it?**


   A constructor is a special method in a class that is automatically called when an object of the class is created. It initializes the object's attributes.
   Types of constructors:
   - **Default Constructor**: Takes no parameters.
   - **Parameterized Constructor**: Takes parameters to initialize the attributes.


### 5 Marks ###


1. **Explain various operations on file with example?**


   Operations on files include:
   - **Opening a file**: Using `open()` function.
   - **Reading from a file**: Using methods like `read()`, `readline()`, or `readlines()`.
   - **Writing to a file**: Using `write()` or `writelines()` methods.
   - **Appending to a file**: Using `append()` method.
   - **Closing a file**: Using `close()` method.
   **Example**:
   ```python
   # Opening a file
   file = open('example.txt', 'w')
   # Writing to a file
   file.write('Hello, world!\n')
   file.write('This is an example.')
   # Closing the file
   file.close()
   # Reading from a file
   file = open('example.txt', 'r')
   content = file.read()
   print(content)
   # Closing the file
   file.close()
   ```


2. **Explain constructor and write a program using default constructor?**


   A constructor is a special method that initializes the objects of a class. It is called automatically when an object is created. 
   **Example**:
   ```python
   class MyClass:
       def __init__(self):
           self.attribute = "Default Constructor"
       def display(self):
           print(self.attribute)
   # Creating object using default constructor
   obj = MyClass()
   obj.display()  # Output: Default Constructor
   ```


3. **Explain types of inheritance with example?**


   Types of inheritance:
   - **Single Inheritance**: One class inherits from another class.
     ```python
     class Parent:
         def display(self):
             print("Parent Class")
     class Child(Parent):
         def show(self):
             print("Child Class")
     obj = Child()
     obj.display()  # Output: Parent Class
     obj.show()     # Output: Child Class
     ```
   - **Multiple Inheritance**: A class inherits from more than one class.
     ```python
     class Parent1:
         def display1(self):
             print("Parent1 Class")
     class Parent2:
         def display2(self):
             print("Parent2 Class")
     class Child(Parent1, Parent2):
         def show(self):
             print("Child Class")
     obj = Child()
     obj.display1()  # Output: Parent1 Class
     obj.display2()  # Output: Parent2 Class
     obj.show()      # Output: Child Class
     ```
   - **Multilevel Inheritance**: A class inherits from a class which inherits from another class.
     ```python
     class Grandparent:
         def display_grandparent(self):
             print("Grandparent Class")
     class Parent(Grandparent):
         def display_parent(self):
             print("Parent Class")
     class Child(Parent):
         def show(self):
             print("Child Class")
     obj = Child()
     obj.display_grandparent()  # Output: Grandparent Class
     obj.display_parent()       # Output: Parent Class
     obj.show()                 # Output: Child Class
     ```
   - **Hierarchical Inheritance**: Multiple classes inherit from a single class.
     ```python
     class Parent:
         def display(self):
             print("Parent Class")
     class Child1(Parent):
         def show1(self):
             print("Child1 Class")
     class Child2(Parent):
         def show2(self):
             print("Child2 Class")
     obj1 = Child1()
     obj2 = Child2()
     obj1.display()  # Output: Parent Class
     obj1.show1()    # Output: Child1 Class
     obj2.display()  # Output: Parent Class
     obj2.show2()    # Output: Child2 Class
     ```


4. **Write a program for protected member?**


   **Example**:-


 class MyClass:

    def __init__(self):

        self._protected_member = "Protected"


    def display(self):

        print(self._protected_member)


class ChildClass(MyClass):

    def show(self):

        print(self._protected_member)


# Create an instance of ChildClass

obj = ChildClass()

obj.display()  # Output: Protected

obj.show()     # Output: Protected


OUTPUT:-

Protected
Protected



5. **Write a program for private member?**


   **Example**:
   
   class MyClass:

    def __init__(self):

        self.__private_member = "Private"


    def display(self):

        print(self.__private_member)


class ChildClass(MyClass):

    def show(self):

        try:

            print(self.__private_member)

        except AttributeError:

            print("Cannot access private member directly")


# Create an instance of ChildClass

obj = ChildClass()

obj.display()  # Output: Private

obj.show()     # Output: Cannot access private member directly


 OUTPUT:-


Private
Cannot access private member directly


EXAM OVER....

BEST OF LUCK GUYS.


CREATED AND PUBLISHED BY:-SUJAL.S.RAJAMANI.

 

Best BSNL plans that are cheaper than Jio, Airtel, and Vi:- LINK  
Jio, Airtel, Vi, vs BSNL: Tariffs comparison:-
LINK

Post a Comment

Previous Post Next Post

Contact Form