Python Programming
Sem: 7th
Question Bank
1. What is the difference between a Python keyword and an identifier?
2. How do you write a single-line comment in Python?
3. Name any two immutable data types in Python.
4. What is the purpose of the print() function in Python?
5. Which function is used to read input from the user in Python?
6. What is the purpose of the break statement in Python loops?
7. How would you write a basic if-else statement to check if a number is positive or
negative?
8. What is the default condition for a while loop to execute in Python?
9. What does the continue statement do in a loop?
10. What is the Python equivalent of a switch-case statement?
11. What is the main difference between a list and a tuple in Python?
12. Can you modify a string in Python? Why or why not?
13. What type of object is a dictionary in Python?
14. What will happen if you try to add an element to a tuple in Python?
15. How do you access the value of a dictionary using a key?
1. Which of the following is a valid Python keyword?
a)function
b)return
c)defintion
d) classify
Answer: b) return
2. Which of the following is used to comment a single line in Python?
a)<!--comment-->
b)#comment
c)//comment
d) /* comment */
Answer: b) # comment
3. Which of the following data types is immutable in Python?
a)List
b)Dictionary
c)String
d) Set
Answer: c) String
4. What is the correct way to install a package using pip in Python?
a)pythoninstall<package_name>
b)ipinstall<package_name>
c)pipadd<package_name>
d) install <package_name>
Answer: b) pip install <package_name>
5. Which of the following will execute if the condition in an if statement is False?
a)else
b)elif
c)continue
d) break
Answer: a) else
6. What will be the output of the following code?
python
Copy code
x = 10
if x > 5:
print("Hello")
else:
print("Hi")
a)Hello
b)Hi
c)Error
d) No output
Answer: a) Hello
7. Which statement is used to exit a loop prematurely in Python?
a)exit
b)stop
c)break
d) continue
Answer: c) break
8. Which of the following is used to skip an iteration in a loop?
a)skip
b)continue
c)stop
d) exit
Answer: b) continue
9. What is the equivalent of a switch statement in Python?
a)if-elsechain
b)whileloop
c)forloop
d) try-except block
Answer: a) if-else chain
10. Which of the following is a mutable object in Python?
a)Tuple
b)String
c)List
d) Set
Answer: c) List
11. Which of the following will raise an error in Python?
a)tuple=(1,2,3)
b)list=[1,2,3]
c)tuple[1]=4
d) list[1] = 4
Answer: c) tuple[1] = 4
12. What type of data structure is a dictionary in Python?
a)List
b)Tuple
c)Set
d) Key-Value pair
Answer: d) Key-Value pair
13. What happens if you try to modify a string in Python?
a)The string gets modified
b)Python raises an error
c) A new string is created
d) Nothing happens
Answer: c) A new string is created
14. Which function is used to convert a string to an integer in Python?
a)str()
b)int()
c)float()
d)convert()
Answer: b) int()
15. Which of the following is used to define a function in Python?
a)define
b)function
c)def
d) func
Answer: c) def
1. Explain the difference between a Python list and a tuple. Provide an example of each.
2. Write a Python program that uses an if-else statement to check whether a given
number is even or odd.
3. What is a recursive function in Python? Write a recursive function to calculate the
factorial of a given number.
4. What are mutable and immutable objects in Python? Provide examples and explain
their differences.
5. What is encapsulation in Object-Oriented Programming? How can you implement it
in Python? Provide an example.
6. Write a Python program that takes a number as input and prints whether it is divisible
by both 3 and 5 using an if-elif-else statement.
7. What is the purpose of the lambda function in Python? Write a lambda function that
takes two numbers as arguments and returns their product.
8. Explain what will happen if you try to add an element to a tuple in Python. Write a
code snippet to demonstrate this.
9. What is the difference between a class method and a static method in Python? Provide
an example of each.
10. Write a Python program to count the number of occurrences of a specific character in
a string using a function
11. Explain how to install Python on your machine. What steps are involved in setting up
the Python environment for the first time?
12. What are the different built-in data types in Python? Explain with examples.
13. Write a Python program that checks whether a given year is a leap year or not. Use an
if-else statement.
14. What is the difference between function parameters and function arguments? Explain
with an example.
15. Write a Python program that prints the first 10 Fibonacci numbers using a for loop.
16. Explain the difference between mutable and immutable objects with examples. What
happens when you try to modify an immutable object?
17. What is inheritance in Python? Write an example to demonstrate how inheritance
works.
18. Explain what a lambda function is in Python. Write a Python program that:
Creates a list of integers.
Uses a lambda function with filter() to extract only the even numbers from the list.
Uses a lambda function with map() to square each number in the list.
19. Provide explanations for how filter() and map() work in conjunction with the lambda
function.
20. Explain how sets work in Python. Write a Python program that:
Takes two lists as input.
Converts them into sets.
Finds the union, intersection, and difference between the two sets.
Handles edge cases like empty sets or non-overlapping sets.
21. Explain the concept of modules and packages in Python. Create a module named
math_operations.py that contains two functions: add() and subtract(). Then, import
and use these functions in another Python file. Discuss the use of import and from ...
import statements.
22. What is multithreading in Python? Write a Python program that uses the threading
module to:
23. Create two threads that each print numbers from 1 to 5 with a delay of 1 second.
24. Explain how threads are executed concurrently and how the Global Interpreter Lock
(GIL) affects multithreading in Python.
25. Explain the importance of debugging and testing in Python. Write a Python program
and intentionally introduce an error. Then, use Python's built-in pdb (Python
Debugger) to find and fix the error. Additionally, write a test function using unittest to
test a specific functionality of your program.
26. Explain list comprehension and generators in Python. Write a Python program that:
Uses list comprehension to create a list of squares from 1 to 10.
Implements a generator function to yield the squares one at a time. Compare the
efficiency of list comprehension and generators in terms of memory usage.
27. What is a decorator in Python? Write a Python program that:
Defines a decorator function that measures the execution time of any function.
Applies this decorator to a function that computes the Fibonacci sequence. Explain
how decorators work and the benefits they provide.
28. Discuss the differences between lists, tuples, sets, and dictionaries in Python. Write a
program that:
Creates a list of numbers and a tuple of strings.
Creates a set of numbers and a dictionary with string keys and integer values.
Performs basic operations like adding/removing elements and iterating over these data
structures.