DDDDD

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

### Operators in Python

1. **Arithmetic Operators:**
- **Question:** What will be the output of the following code?
```python
a = 10
b=3
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** b)
```
- **Answer:**
```
13
7
30
3.3333333333333335
3
1
1000
```

2. **Relational Operators:**
- **Question:** What will be the output of the following code?
```python
a=5
b = 10
print(a > b)
print(a < b)
print(a == b)
print(a != b)
print(a >= b)
print(a <= b)
```
- **Answer:**
```
False
True
False
True
False
True
```

3. **Logical Operators:**
- **Question:** What will be the output of the following code?
```python
a = True
b = False
print(a and b)
print(a or b)
print(not a)
```
- **Answer:**
```
False
True
False
```

4. **Bitwise Operators:**
- **Question:** What will be the output of the following code?
```python
a = 6 # 110 in binary
b = 2 # 010 in binary
print(a & b)
print(a | b)
print(a ^ b)
print(~a)
print(a << 1)
print(a >> 1)
```
- **Answer:**
```
2
6
4
-7
12
3
```

### Data Types in Python

1. **String Operations:**
- **Question:** What will be the output of the following code?
```python
s = "CBSE Class 12"
print(s[0])
print(s[-1])
print(s[1:5])
print(s[:])
print(s[::2])
```
- **Answer:**
```
C
2
BSE
CBSE Class 12
CS la 2
```

2. **List Operations:**
- **Question:** What will be the output of the following code?
```python
lst = [10, 20, 30, 40, 50]
print(lst[2])
print(lst[-1])
print(lst[1:4])
lst.append(60)
print(lst)
lst.pop(2)
print(lst)
lst.insert(1, 15)
print(lst)
```
- **Answer:**
```
30
50
[20, 30, 40]
[10, 20, 30, 40, 50, 60]
[10, 20, 40, 50, 60]
[10, 15, 20, 40, 50, 60]
```

3. **Tuple Operations:**
- **Question:** What will be the output of the following code?
```python
t = (1, 2, 3, 4, 5)
print(t[1])
print(t[-1])
print(t[1:3])
```
- **Answer:**
```
2
5
(2, 3)
```

4. **Dictionary Operations:**
- **Question:** What will be the output of the following code?
```python
d = {'a': 1, 'b': 2, 'c': 3}
print(d['a'])
print(d.get('b'))
d['d'] = 4
print(d)
del d['a']
print(d)
```
- **Answer:**
```
1
2
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
{'b': 2, 'c': 3, 'd': 4}
```

5. **Set Operations:**
- **Question:** What will be the output of the following code?
```python
s = {1, 2, 3, 4, 5}
s.add(6)
print(s)
s.remove(3)
print(s)
s1 = {4, 5, 6, 7}
print(s.union(s1))
print(s.intersection(s1))
print(s.difference(s1))
```
- **Answer:**
```
{1, 2, 3, 4, 5, 6}
{1, 2, 4, 5, 6}
{1, 2, 4, 5, 6, 7}
{4, 5, 6}
{1, 2}
```
Certainly! Here are some examples of previous year questions from the CBSE Class 12
Computer Science exam focusing on the topic "Exception Handling in Python":

### Exception Handling in Python

1. **Basic Try-Except Block:**


- **Question:** What will be the output of the following code?
```python
try:
x = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
```
- **Answer:**
```
Cannot divide by zero
```

2. **Multiple Exceptions:**
- **Question:** What will be the output of the following code?
```python
try:
a = [1, 2, 3]
print(a[3])
except IndexError:
print("Index out of range")
except:
print("Some error occurred")
```
- **Answer:**
```
Index out of range
```

3. **Else Block in Exception Handling:**


- **Question:** What will be the output of the following code?
```python
try:
num = int(input("Enter a number: "))
print(f"Entered number is {num}")
except ValueError:
print("Invalid input")
else:
print("No exception occurred")
```
- **Answer:**
- If a valid integer is entered, for example, `5`:
```
Entered number is 5
No exception occurred
```
- If an invalid input, such as a string, is entered:
```
Invalid input
```

4. **Finally Block in Exception Handling:**


- **Question:** Explain the purpose of the `finally` block in exception handling with an
example.
- **Answer:**
- **Explanation:** The `finally` block is used to specify a block of code that will be
executed regardless of whether an exception is raised or not. It is typically used for cleanup
actions, such as closing files or releasing resources.
- **Example:**
```python
try:
file = open("sample.txt", "r")
content = file.read()
except FileNotFoundError:
print("File not found")
finally:
file.close()
print("File closed")
```

5. **Raising Exceptions:**
- **Question:** What will be the output of the following code?
```python
try:
age = int(input("Enter age: "))
if age < 18:
raise ValueError("Age must be at least 18")
except ValueError as ve:
print(ve)
```
- **Answer:**
- If the input age is less than 18, for example, `16`:
```
Age must be at least 18
```
- If the input age is 18 or more, for example, `20`:
```
(No output, the program runs without exception)
```

6. **Custom Exceptions:**
- **Question:** Write a Python program that defines a custom exception called
`NegativeNumberError` which is raised when a negative number is entered. Handle this
exception in the program.
- **Answer:**
```python
class NegativeNumberError(Exception):
pass

try:
num = int(input("Enter a number: "))
if num < 0:
raise NegativeNumberError("Negative number entered")
except NegativeNumberError as nne:
print(nne)
```
- If a negative number is entered, for example, `-5`:
```
Negative number entered
```

7. **Nested Try-Except Blocks:**


- **Question:** Explain nested try-except blocks with an example.
- **Answer:**
- **Explanation:** Nested try-except blocks are used when you need to handle
exceptions at multiple levels, allowing finer control over exception handling in different parts
of the code.
- **Example:**
```python
try:
num = int(input("Enter a number: "))
try:
result = 10 / num
print(result)
except ZeroDivisionError:
print("Cannot divide by zero")
except ValueError:
print("Invalid input")
```

You might also like