Python Question Paper 1
Python Question Paper 1
Python Question Paper 1
Python
1 Marks Questions
a) What is dry run in Python?
Answer:
Selection statements in Python, such as if , elif (else if), and else , serve
the purpose of controlling the flow of a program based on certain conditions.
They allow the program to make decisions and execute different blocks of
code depending on whether a given condition is true or false. Selection
statements enable developers to implement conditional logic, making
programs more dynamic and responsive to different scenarios.
Python 1
d) What is the use of the pass statement?
Answer:
Python 2
2. : Suspends the execution of the current thread for the
time.sleep(seconds)
The seek(offset, whence) function is used to change the current file position
in a file, and the tell() function returns the current file position.
2 Marks Questions
a) How to handle exception in Python?
Answer:
Exception handling in Python is done using the try , except , else , and
finally blocks.
try:
# Code that may raise an exception
result = 10 / 0
except ZeroDivisionError as e:
# Handling specific exception
print(f"Error: {e}")
except Exception as e:
# Handling other exceptions
print(f"An error occurred: {e}")
else:
# Code to execute if no exception occurred
Python 3
print("No exception occurred.")
finally:
# Code that will be executed in any case
print("Finally block.")
my_list = [1, 2, 3, 4, 5]
length = len(my_list) # Result: 5
my_list = [1, 2, 3]
my_list.append(4) # Result: [1, 2, 3, 4]
Backward indexing in strings allows accessing characters from the end of the
string using negative indices. The last character has index 1 , the second-to-
last has index 2 , and so on.
Python 4
my_string = "Hello"
last_char = my_string[-1] # Result: 'o'
second_last_char = my_string[-2] # Result: 'l'
e) Define identifiers.
Answer:
4 Marks Questions
a) Write a Python program to check if a given number is
Armstrong.
def is_armstrong(number):
num_str = str(number)
order = len(num_str)
sum_of_digits = sum(int(digit) ** order for digit in num_
return sum_of_digits == number
# Example usage:
num = 153
if is_armstrong(num):
print(f"{num} is an Armstrong number.")
else:
print(f"{num} is not an Armstrong number.")
Python 5
b) Write a Python program to display the power of 2 using an
anonymous function.
power_of_two = lambda x: 2 ** x
# Example usage:
exponent = 4
result = power_of_two(exponent)
print(f"2^{exponent} is {result}")
def even_length_words(input_string):
words = input_string.split()
even_length_words_list = [word for word in words if len(wo
return even_length_words_list
# Example usage:
sentence = "This is a sample sentence for demonstration"
result = even_length_words(sentence)
print("Even-length words:", result)
4 Marks Questions
a) Write a Python program to check for Zero Division Error
Exception.
Python 6
# Example usage:
divide_numbers(10, 2) # Normal division
divide_numbers(5, 0) # Division by zero error
# Example usage:
num1 = 48
num2 = 18
result = gcd_recursive(num1, num2)
print(f"The GCD of {num1} and {num2} is: {result}")
# Example usage:
sample_dict = {'a': 1, 'b': 2, 'c': 3}
search_key = 'b'
if key_exists(sample_dict, search_key):
print(f"The key '{search_key}' exists in the dictionary."
else:
print(f"The key '{search_key}' does not exist in the dicti
Python 7
3 Marks Questions
a) Trace the output of the following code:
pythonCopy code
sum = 0
for i in range(12, 2, -2):
sum += i
print(sum)
Explanation:
The loop iterates over these numbers, and each value is added to the sum .
pythonCopy code
count = 1
def doThis():
global count
for i in (1, 2, 3):
count += 1
doThis()
Python 8
print(count)
Explanation:
The function doThis is defined, which contains a for loop iterating over the
values (1, 2, 3).
The global variable count is incremented by 1 for each iteration in the loop.
After calling the function, the print(count) statement outputs the final value of
count .
Inside doThis :
Python 9