Class 12 Computer Science
Question Paper and Solution - Python Revision Tour & Functions
Class 12 Computer Science - Chapter 1 & 2 Test
Max Marks: 35 Time: 1½ hours
Chapters Covered: Python Revision Tour & Functions
Section A: Objective (1 mark each) - [5 × 1 = 5 marks]
1. Which of the following is a valid identifier in Python?
a) 123abc b) _my_var c) def d) for
2. What is the output of:
print(type(3.0 + 4))
3. Which of the following is an immutable data type?
a) list b) set c) dict d) tuple
4. Which function returns the length of a string?
a) size() b) len() c) length() d) count()
5. A function defined inside another function is called:
a) Recursion b) Nested Function c) Lambda d) Built-in function
Section B: Short Answer (2 marks each) - [5 × 2 = 10 marks]
6. Differentiate between `is` and `==` in Python with an example.
7. What is the purpose of the `return` statement in a function?
8. Write a Python function to check if a number is even or odd.
9. Rewrite the following using while loop:
for i in range(1, 6):
print(i)
10. Write any two differences between global and local variables.
Section C: Application-Based Questions (3 marks each) - [4 × 3 = 12 marks]
11. Predict the output:
def test(a, b=3):
return a + b
print(test(2), test(2, 4))
12. Write a function `count_vowels(s)` that returns the number of vowels in a string.
13. What are default and keyword arguments? Give suitable code examples for each.
14. Write a Python program using recursion to find the sum of digits of a number.
Section D: Long Answer (4 marks each) - [2 × 4 = 8 marks]
15. Write a program to:
- Accept a list of numbers
- Define a function to return the second largest number
- Print the result
16. Write a recursive function to generate and print the Fibonacci series up to n terms.
Solutions
Solutions to Class 12 Computer Science - Python Revision Tour & Functions
Section A Answers:
1. b) _my_var
2. <class 'float'>
3. d) tuple
4. b) len()
5. b) Nested Function
Section B Answers:
6. `is` checks identity; `==` checks value.
Example:
a = [1, 2]; b = [1, 2]
a == b -> True; a is b -> False
7. `return` gives output from function to caller.
8. def check_even_odd(n):
if n % 2 == 0:
print("Even")
else:
print("Odd")
9. i = 1
while i <= 5:
print(i)
i += 1
10. Global: Declared outside, accessible everywhere
Local: Declared inside function
Section C Answers:
11. Output: 5 6
12. def count_vowels(s):
vowels = "aeiouAEIOU"
return sum(1 for ch in s if ch in vowels)
13. Default: def greet(name, msg="Hi")
Keyword: greet(name="Ravi", msg="Hello")
14. def sum_digits(n):
if n == 0: return 0
return (n % 10) + sum_digits(n // 10)
Section D Answers:
15. def second_largest(lst):
lst = list(set(lst))
lst.sort()
return lst[-2] if len(lst) >= 2 else None
nums = [4, 7, 2, 7, 1, 9]
print("Second Largest:", second_largest(nums))
16. def fib(n):
if n <= 1: return n
return fib(n-1) + fib(n-2)
for i in range(5):
print(fib(i), end=" ")