Professional Documents
Culture Documents
DDDDD
DDDDD
DDDDD
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
```
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":
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
```
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
```