15. Program for NumPy array arithmetic operations.

import numpy as np
a = np.array([1,2,3])
b = np.array([4,5,6])

print(a + b)
print(a * b)
print(a / b)

---

16. Difference between copy() and view() in NumPy.

copy() view()

Creates new array Shares same data
Changes don’t affect original Changes affect original

Example:

x = np.array([1,2,3])
y = x.copy()
z = x.view()

---

------------------------------------------

📌 PART – B (6 MARKS EACH)

------------------------------------------

---

1. Explain in detail the basic concepts of OOP. (6 marks)

1. Class → Blueprint for objects

2. Object → Instance of a class

3. Encapsulation → Protecting data

4. Abstraction → Hiding details

5. Inheritance → Code reusability

6. Polymorphism → Many forms of a function/method

Example:

class Student:
    def show(self):
        print("Student details")

---

2. Explain the life cycle of an object.

1. Creation → using class constructor (__init__())

2. Use → methods operate on object

3. Destruction → done using __del__() automatically

Example:

class Demo:
    def __init__(self):
        print("Created")
    def __del__(self):
        print("Destroyed")

---

3. Types of Inheritance with diagrams. (6 marks)

1. Single Inheritance → One parent, one child

2. Multiple Inheritance → Child has multiple parents

3. Multilevel Inheritance → Parent → Child → Grandchild

4. Hierarchical Inheritance → One parent → many children

5. Hybrid → Combination of multiple types


---

4. Instance attributes & instance methods program.

class Car:
    def __init__(self, brand, color):
        self.brand = brand      # instance attribute
        self.color = color

    def show(self):            # instance method
        print(self.brand, self.color)

c = Car("BMW", "Black")
c.show()

---

5. Class attributes & class methods program.

class Student:
    college = "ABC College"     # class attribute

    @classmethod
    def show_college(cls):      # class method
        print(cls.college)

Student.show_college()

---

6. How data hiding can be achieved? Explain with example.

Use private attributes using __ prefix.

class Bank:
    def __init__(self, bal):
        self.__balance = bal     # private

    def show(self):
        print(self.__balance)

obj = Bank(5000)
obj.show()

---

------------------------------------------

📌 PART – C (8 MARKS EACH)

------------------------------------------

---

1. Demonstrate method overriding with example. (8 marks)

Method overriding happens when a child class provides its own version of a parent method.

class Animal:
    def sound(self):
        print("Animal sound")

class Dog(Animal):
    def sound(self):          # overriding
        print("Bark")

d = Dog()
d.sound()

---

2. Explain single level & multilevel inheritance with example code.

1️⃣ Single Inheritance

class A:
    def showA(self):
        print("A")

class B(A):
    def showB(self):
        print("B")

b = B()
b.showA()
b.showB()

---

2️⃣ Multilevel Inheritance

class A:
    def showA(self):
        print("A")

class B(A):
    def showB(self):
        print("B")

class C(B):
    def showC(self):
        print("C")

c = C()
c.showA()
c.showB()
c.showC()

---

3. Write a note on __init__, __new__, __del__. (8 marks)

1️⃣ __init__

Constructor

Used to initialize object variables

def __init__(self):
    print("Initializing")

2️⃣ __new__

Creates the actual object in memory

Called before __init__

def __new__(cls):
    return super().__new__(cls)

3️⃣ __del__

Destructor

Called when object is destroyed

def __del__(self):
    print("Destroyed")

---

4. Explain hierarchical inheritance with example.

One parent → multiple children.

class A:
    def display(self):
        print("Class A")

class B(A):
    def showB(self):
        print("Class B")

class C(A):
    def showC(self):
        print("Class C")

b = B()
c = C()

b.display()
c.display()

python Where stories live. Discover now