0% found this document useful (0 votes)
2 views3 pages

Question Bank Python II Midterm

The document is a question bank for a midterm exam in Python, divided into three parts. Part I consists of theoretical questions covering Python concepts such as data structures, functions, and file handling. Part II includes practical programming tasks, while Part III focuses on advanced topics like memory efficiency, exception handling, and object-oriented programming.

Uploaded by

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

Question Bank Python II Midterm

The document is a question bank for a midterm exam in Python, divided into three parts. Part I consists of theoretical questions covering Python concepts such as data structures, functions, and file handling. Part II includes practical programming tasks, while Part III focuses on advanced topics like memory efficiency, exception handling, and object-oriented programming.

Uploaded by

vinayavasthi007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Question Bank (Important Questions) for II MidTerm

Sub: Python

Part-I

1. What is the difference between 'break', 'continue' and 'pass' statements?


2. What is a dictionary in Python? Write any two dictionary methods.
3. What is the difference between a list and a tuple in Python?
4. Define string slicing with an example.
5. What happens when we try to add mutable elements to sets? Explain with examples.
6. List four built-in methods associated with Python Lists that modify the list in-place.
7. What is a tuple? Mention two advantages of using tuples over lists.
8. List any two methods of strings and their uses.
9. Write the Output of the code:
numbers = [1, 2, 3]
[Link](2)
print(numbers)
10. Identify the specific keyword used to create a function that calls itself.
11. Recall the default mode in which a file is opened using the open() function if no mode is
specified.
12. Contrast "Local Scope" and "Global Scope" using the global keyword as a reference point.
13. Illustrate the difference between the append() and extend() methods when adding a list to an
existing list.
14. Summarize the benefit of using the with statement (context manager) when performing file I/O
operations.
15. Describe the purpose of the self parameter in Python classes?
16. What is the role of the __init__ method in a class?

Part-II

1. Write a Python program that checks whether a given string is a palindrome or not.
2. Develop a recursive function factorial(n) and demonstrate its execution flow for n = 5.
3. Explain the concepts of aliasing and cloning in python with example.
4. Explain any four dictionary functions with suitable examples.
5. Write a Python program to find the largest element in a list.
6. Explain recursion in detail. Write a recursive program for Fibonacci series.
7. Write a Python program to check whether a given number is an Armstrong number using
functions.
8. Explain the concept of user-defined functions with an example that uses parameters and returns
a value.
9. Explain control flow statements in Python. Write a program using loops and conditional
statements.
10. Write a Python program to count the number of strings where the string length is 2 or more and
the first and last characters are the same from a given list of strings.
Sample List: ['abc', 'xyz', 'aba', '1221']
Expected Result: 2
11. Explain file handling modes (r, w, a) with examples.
12. Write a Python program to print the numbers of a specified list after removing even numbers
from it.
13. Write a program to search an element in a list and display "Found" or "Not Found".
14. Write a Python function that returns both the quotient and remainder of two numbers using tuple
return.
15. Execute a file handling task: Write a program that reads [Link], identifies all lines containing
with the word "PCE", and writes them into a new file [Link].
16. Predict the output of program:
list1 = [5, 6, 7]
list2 = [Link]()
[Link](8)
print("list1:", list1)
print("list2:", list2)
17. Build a class Employee with attributes for name and salary. Include a method
apply_raise(percent) that modifies the salary attribute.
18. Demonstrate the use of a while loop to print the first 10 natural numbers.
19. Code Snippet Analysis: Given a list L = [10, 20, 30, 40, 50], write the code to extract the sub-
list [40, 30, 20] using a single slicing operation.
20. Explain the basic operations of file handling in Python. List and describe the different modes
used to open a file in Python.
21. Illustrate the concept of "Modular Programming" by explaining how a developer can use the
import statement to share functions between two different .py files.

Part-III

1. Compare and Contrast the memory efficiency and use cases of a List versus a Dictionary when
searching for an element among 1 million records.
2. Examine the behavior of "Variable Scope" inside nested functions. Explain the nonlocal
keyword with a practical code example.
3. Define a user-defined function calculate() that takes two numbers as input and returns their:

a) Sum
b) Difference
c) Product
d) Quotient
Write a complete program using this function, and display all returned values.
4. Write a Python program to perform the following operations:
a) Create a list of five numbers and display the maximum and minimum values.
b) Convert the list into a tuple.
c) Create a dictionary with keys as subject names and values as marks, and display all key–
value pairs.
d) Perform any three string operations (like upper(), find(), replace()) with examples.

5. Differentiate between read(), readline(), and readlines() in terms of memory consumption when
dealing with a 5GB text file. Which method is most robust?
6. Write a Python program to:
a) Create a text file named [Link]
b) Write five lines of text into the file
c) Read the file contents and display them
d) Count the total number of words in the file and print the output

7. Write a Python program that reads a file line by line and counts the number of lines, words, and
characters in the file.
8. Write a Python program to handle the following exceptions:

a) Division by zero
b) Invalid input type (when user enters text instead of a number)

Use try, except, else, and finally blocks and explain the purpose of each block with output.

9. Evaluate the output of the following dictionary operation and explain the "Key Collision" or
overwriting logic:
Python
my_dict = {1: "Alpha", 1.0: "Beta", True: "Gamma"}
print(my_dict[1])

10. Deconstruct the relationship between a Class and an Instance. Explain how changing a class-
level attribute differs from changing an instance-level attribute in terms of memory impact.
11. Original Code:
nums = [5, 10, 15, 20]
sub = nums[1:3]
print(sub)
Task:
1. Rewrite using nums → numbers_list and sub → sliced_list.
2. Predict the output.
3. Explain how slicing works.
12. Analyze the trade-offs between "Recursion" and "Iteration" in Python. Under what specific
memory conditions would a recursive solution be considered a liability compared to a while
loop?
13. Write a Python program to create a class BankAccountp with the following:
1. Attributes: account number, account holder name, balance
2. Methods: deposit(), withdraw(), display()
3. Create objects and demonstrate all methods
14. Write a short note on Object-Oriented Programming (OOP) concepts in Python. Explain its key
concepts with examples.
15. Write a Python program to create a class Student with attributes name and marks, and a method
to display student details.
16. Differentiate between the sort() method and the sorted() function. Analyze their behavior
regarding the "Original Data" and explain which one is safer to use when data integrity is a
priority.
17. Explain exception handling in Python. Write a simple example using try-except.
18. Write a Python program that:

a) Accepts a string from the user


b) Counts the number of vowels and consonants
c) Stores them in a dictionary as { "vowels": count, "consonants": count }
d) Writes this dictionary to a file named [Link]
e) Uses exception handling to manage file-related errors

You might also like