---

15. Give some examples for common in-built exceptions.

ZeroDivisionError

TypeError

ValueError

IndexError

---

16. What is the difference between else and finally block in exception handling?

else finally

Runs only if no exception occurs Runs always
Optional Optional

---

17. What is assertion?

assert is used to check a condition. If the condition is false, an AssertionError occurs.

---

18. What are the different modes in which a file can be opened?

"r" – read

"w" – write

"a" – append

"rb" – read binary

"wb" – write binary

---

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

📌 PART – B (6 MARKS EACH)

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

---

1. Advantages and disadvantages of recursion.

Advantages

Code becomes short and clean

Solutions closer to mathematical logic

Useful for tree/graph problems

Disadvantages

Uses more memory

Slower because of repeated function calls

Risk of infinite recursion

Example:

def fact(n):
    if n == 1:
        return 1
    return n * fact(n-1)

---

2. Explain random module with example.

random module is used to generate random values.

Common functions:

random.randint(a,b) → random integer

random.random() → value between 0 and 1

random.choice(list) → pick random element

Example:

import random
print(random.randint(1,10))
print(random.choice([10,20,30]))

---

3. Explain math module with example.

The math module provides mathematical functions.

Examples:

math.sqrt(x)

math.pow(x,y)

math.ceil(x)

math.floor(x)

Example code:

import math
print(math.sqrt(25))
print(math.ceil(4.2))

---

4. How to define a function? Explain with example.

python Where stories live. Discover now