Class 12 Python – Board Exam Practice Questions with Solutions
1. Difference between append() and extend()
Question / Program:
L1 = [1, 2, 3]
[Link]([4, 5])
L2 = [1, 2, 3]
[Link]([4, 5])
print(L1)
print(L2)
Answer / Output:
[1, 2, 3, [4, 5]]
[1, 2, 3, 4, 5]
Explanation:
append() adds the entire object as one element, whereas extend() adds elements
individually.
--------------------------------------------------------------------------------
2. Program to Count Vowels
Question / Program:
s = input("Enter a string: ")
count = 0
for ch in [Link]():
if ch in "aeiou":
count += 1
print("Number of vowels =", count)
Answer / Output:
The program counts the number of vowels in the entered string.
Explanation:
The loop checks every character and increases the counter whenever a vowel is found.
--------------------------------------------------------------------------------
3. Dictionary get() Function
Question / Program:
d = {"A":10, "B":20, "C":30}
print([Link]("B"))
print([Link]("D", 0))
Answer / Output:
20
0
Explanation:
get() returns the value of a key. If the key does not exist, the default value is returned.
--------------------------------------------------------------------------------
4. Use of break Statement
Question / Program:
for i in range(1, 11):
if i == 5:
break
print(i)
Answer / Output:
1
2
3
4
Explanation:
The break statement immediately terminates the loop.
--------------------------------------------------------------------------------
5. Difference between List and Tuple
Question / Program:
Differentiate between List and Tuple.
Answer / Output:
List:
- Mutable
- Uses square brackets []
- Can be modified
Tuple:
- Immutable
- Uses parentheses ()
- Cannot be modified
Explanation:
Lists can be changed after creation, while tuples cannot.
--------------------------------------------------------------------------------
6. Largest Number in a List
Question / Program:
L = [10, 45, 78, 23, 99]
largest = L[0]
for i in L:
if i > largest:
largest = i
print("Largest number =", largest)
Answer / Output:
Largest number = 99
Explanation:
The program compares every element and updates the largest value.
--------------------------------------------------------------------------------
7. Built-in Functions
Question / Program:
print(len("Python"))
print(type(12.5))
for i in range(1,5):
print(i)
Answer / Output:
6
<class 'float'>
1
2
3
4
Explanation:
len() gives length, type() gives data type, and range() generates a sequence.
--------------------------------------------------------------------------------
8. Prime Number Program
Question / Program:
n = int(input("Enter a number: "))
flag = True
for i in range(2, n):
if n % i == 0:
flag = False
break
if flag and n > 1:
print("Prime Number")
else:
print("Not Prime")
Answer / Output:
The program checks whether the number is prime or not.
Explanation:
If the number is divisible by any value other than 1 and itself, it is not prime.
--------------------------------------------------------------------------------
9. String Slicing
Question / Program:
x = "Computer"
print(x[2:6])
print(x[-3:])
Answer / Output:
mput
ter
Explanation:
String slicing extracts characters using index positions.
--------------------------------------------------------------------------------
10. Factorial using Function
Question / Program:
def factorial(n):
fact = 1
for i in range(1, n + 1):
fact = fact * i
return fact
num = int(input("Enter number: "))
print("Factorial =", factorial(num))
Answer / Output:
Factorial = 120 (for input 5)
Explanation:
Factorial is the product of all positive integers from 1 to n.
--------------------------------------------------------------------------------