Class 12 ICSE Python Questions and Answers
1. Data Types and Variables
Q1. What are the basic data types in Python?
A1. The basic data types in Python include:
- int: Represents integers
- float: Represents decimal numbers
- str: Represents strings
- bool: Represents boolean values True and False
- NoneType: Represents the absence of value (None)
2. Operators and Expressions
Q2. What is the difference between ‘==’ and ‘=’ operators in Python?
A2. ‘==’ is the equality operator, used to compare two values for equality.
‘=’ is the assignment operator, used to assign a value to a variable.
Q3. Write a Python expression for calculating the area of a circle.
A3. The area of a circle can be calculated as:
area = 3.1416 * radius ** 2
3. Control Statements (if, else, loops)
Q4. Write a Python program to check if a number is even or odd.
A4. Program:
```python
num = int(input('Enter a number: '))
if num % 2 == 0:
print('Even')
else:
print('Odd')
```
Q5. Write a Python program to calculate the factorial of a number using a loop.
A5. Program:
```python
num = int(input('Enter a number: '))
factorial = 1
for i in range(1, num + 1):
factorial *= i
print('Factorial:', factorial)
```
4. Functions and Scope
Q6. Define a function in Python.
A6. A function is a block of code that performs a specific task. It is defined using the 'def'
keyword.
Q7. Write a Python function to calculate the square of a number.
A7. Function:
```python
def square(num):
return num ** 2
```
5. String Manipulation
Q8. How do you concatenate two strings in Python?
A8. Two strings can be concatenated using the '+' operator.
Q9. Write a Python program to count the occurrences of a character in a string.
A9. Program:
```python
text = input('Enter a string: ')
char = input('Enter a character: ')
count = text.count(char)
print('Occurrences:', count)
```
6. Lists, Tuples, and Dictionaries
Q10. What is the difference between a list and a tuple in Python?
A10. A list is mutable (can be changed), while a tuple is immutable (cannot be changed).
Q11. Write a Python program to add an item to a dictionary.
A11. Program:
```python
dict = {'a': 1, 'b': 2}
dict['c'] = 3
print(dict)
```
7. File Handling
Q12. Explain how to open a file in Python.
A12. A file can be opened using the open() function. Example: file = open('filename.txt', 'r')
Q13. Write a Python program to read the contents of a file.
A13. Program:
```python
with open('filename.txt', 'r') as file:
contents = file.read()
print(contents)
```
8. Exception Handling
Q14. What is exception handling in Python?
A14. Exception handling is a mechanism in Python to handle errors gracefully using try-
except blocks.
Q15. Write a Python program to handle division by zero.
A15. Program:
```python
try:
num = int(input('Enter a number: '))
result = 10 / num
except ZeroDivisionError:
print('Cannot divide by zero')
```
9. Basic Object-Oriented Programming Concepts
Q16. What is a class in Python?
A16. A class is a blueprint for creating objects. It defines attributes and methods.
Q17. Write a simple class representing a Car with attributes and a method to display details.
A17. Program:
```python
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
def display_info(self):
print(f'Car make: {self.make}, Model: {self.model}')
```