0% found this document useful (0 votes)
23 views7 pages

Here

Uploaded by

Tarun Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views7 pages

Here

Uploaded by

Tarun Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Here's a set of questions for Class XII CBSE Computer Science, focusing on Python fundamentals,

looping, and functions:

Multiple Choice Questions (MCQs)

1. Which of the following is a valid Python data type?


A) Integer
B) String
C) List
D) All of the above

Answer: D) All of the above

2. What is the output of the following code?


python
x = [1, 2, 3, 4]
print(x[1:3])

A) [1, 2]
B) [2, 3]
C) [2, 3, 4]
D) [1, 2, 3]

Answer: B) [2, 3]

3. Which of the following statements is used to create a function in Python?


A) def
B) function
C) create
D) lambda

Answer: A) def

4. What does the following loop do?


python
for i in range(5):
print(i)

A) Prints numbers from 1 to 5


B) Prints numbers from 0 to 4
C) Prints numbers from 0 to 5
D) Prints an error

Answer: B) Prints numbers from 0 to 4


5. Which function can be used to convert a string into a list of characters?
A) list()
B) split()
C) join()
D) map()

Answer: A) list()

6. In Python, how can you catch multiple exceptions in a single block?


A) Using multiple `except` blocks
B) Using a single `except` block with a tuple
C) Using nested `try` blocks
D) Using `finally` block

Answer: B) Using a single `except` block with a tuple

7. What will be the output of the following code?


python
def add(a, b=2, c=3):
return a + b + c
print(add(1))

A) 3
B) 6
C) 7
D) Error

Answer: B) 6

8. Which of the following keywords is used to define an anonymous function in Python?


A) lambda
B) def
C) fun
D) anon

Answer: A) lambda

9. What is the purpose of the `pass` statement in Python?


A) To exit a loop
B) To define a placeholder for future code
C) To skip an iteration in a loop
D) To raise an exception
Answer: B) To define a placeholder for future code

10. What does the following code do?


python
x = [i 2 for i in range(5)]

A) Generates a list of squares from 1 to 5


B) Generates a list of squares from 0 to 4
C) Generates a list of even numbers from 1 to 5
D) Generates a list of odd numbers from 0 to 4

Answer: B) Generates a list of squares from 0 to 4

Two Mark Questions

1. Write a Python function to check if a number is even or odd.

Answer:
python
def check_even_odd(number):
if number % 2 == 0:
return "Even"
else:
return "Odd"

2. Explain the difference between `break` and `continue` statements in Python.

Answer:
`break`: Exits the nearest enclosing loop completely.
`continue`: Skips the rest of the code inside the current loop iteration and moves to the next iteration
of the loop.

3. How do you create a dictionary in Python and access its values?

Answer:
python
my_dict = {'name': 'Alice', 'age': 25}
print(my_dict['name']) Access value using key

4. What will be the output of the following code? Explain why.


python
def func(a, b=[]):
b.append(a)
return b
print(func(1))
print(func(2))

Answer:

[1]
[1, 2]

The default argument `b` retains its value between calls because default arguments are evaluated only
once.

5. Write a Python function to calculate the factorial of a number using recursion.

Answer:
python
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n 1)

Four Mark Questions

1. Describe how you would implement a Python function to check if a string is a palindrome. Provide
the code.

Answer:
python
def is_palindrome(s):
s = s.lower().replace(" ", "")
return s == s[:: 1]

Explanation: Convert the string to lowercase, remove spaces, and check if it reads the same
backward.

2. Explain the concept of list comprehensions with an example to create a list of squares of even
numbers from 1 to 10.

Answer:
python
even_squares = [x 2 for x in range(1, 11) if x % 2 == 0]
Explanation: List comprehensions provide a concise way to create lists.
This example squares each even number in the range 1 to 10.

3. Compare and contrast `for` and `while` loops in Python with examples.

Answer:
python
for loop example
for i in range(5):
print(i)

while loop example


i=0
while i < 5:
print(i)
i += 1

`for` loop: Iterates over a sequence (list, tuple, range).


`while` loop: Repeats as long as a condition is true.

Comparison: Use `for` loop for a known range or sequence and `while` loop for indefinite iteration
until a condition is met.

4. Explain the concept of lambda functions in Python with an example to add two numbers.

Answer:
python
add = lambda x, y: x + y
print(add(5, 3)) Output: 8

Explanation: Lambda functions are anonymous, single expression functions defined using the
`lambda` keyword. They are useful for small, throwaway functions used without needing a full `def`
statement.

5. Describe how to handle exceptions in Python with a try except block. Provide an example that
catches a division by zero error.

Answer:
python
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero.")

Explanation:
Use `try` to wrap code that might cause an exception.
`except` catches the exception and allows handling it gracefully without stopping the program.

Five Mark Questions

1. Implement a Python function that takes a list of numbers and returns a list with only the prime
numbers. Include helper function for prime check.

Answer:
python
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n 0.5) + 1):
if n % i == 0:
return False
return True

def filter_primes(numbers):
return [num for num in numbers if is_prime(num)]

Example usage:
numbers = [2, 3, 4, 5, 6, 7, 8, 9, 10]
print(filter_primes(numbers)) Output: [2, 3, 5, 7]

2. Explain with examples the use of `*args` and ` kwargs` in Python functions. Write a function that
demonstrates both.

Answer:
python
def example_function(*args, kwargs):
print("args:", args)
print("kwargs:", kwargs)

Example usage:
example_function(1, 2, 3, a=4, b=5)

Explanation:
`*args` allows passing a variable number of positional arguments.
` kwargs` allows passing a variable number of keyword arguments.

Output:

args: (1, 2, 3)
kwargs: {'a': 4, 'b': 5}

3. Write a Python program that defines a function to calculate the Fibonacci sequence up to `n` terms.
Provide both iterative and recursive approaches.

Answer:
python
Iter

You might also like