0% found this document useful (0 votes)
16 views4 pages

Python Programming Exam Solutions Detailed

Uploaded by

atharvsondkar840
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)
16 views4 pages

Python Programming Exam Solutions Detailed

Uploaded by

atharvsondkar840
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
You are on page 1/ 4

Programming with Python - Detailed Exam Solutions

Q.1 Attempt the following (Any FOUR)

(A) Purpose of Python's Garbage Collection Feature:


Garbage collection in Python is the process of automatically freeing memory by deleting
objects that are no longer referenced. Python mainly uses reference counting, where
each object keeps track of how many variables refer to it. When count becomes zero,
memory is released. In addition, Python has a cyclic garbage collector that removes
circular references. This ensures efficient memory management and prevents memory
leaks. For example, unused lists or objects are automatically cleared without manual
effort.

(B) Demonstrate range() in 'for loop':


The range() function generates a sequence of numbers and is mostly used in loops. It
takes up to three arguments: start, stop, and step. For example:
for i in range(1,6):
print(i)
This will print numbers 1,2,3,4,5. We can also use range(2,10,2) to print 2,4,6,8. Thus,
range() helps to control iteration in loops efficiently.

(C) Explain 'continue statement':


The continue statement is used inside loops to skip the current iteration and move directly
to the next cycle. It does not terminate the loop but just ignores the remaining code for that
iteration. For example:
for i in range(5):
if i == 2:
continue
print(i)
Output: 0,1,3,4. Here, value 2 is skipped. This is useful when some unwanted cases need
to be ignored during iteration.

(D) Behavior of int and float in arithmetic:


In Python, when int and float are mixed in an arithmetic operation, Python converts int into
float automatically (type promotion) to avoid loss of precision. Example:
5 + 2.0 → 7.0 (float)
5 // 2.0 → 2.0 (floor division returns float)
5 * 2.0 → 10.0
Thus, float dominates int in mixed expressions, ensuring accuracy.

(E) Compare if...else and if...elif:


- if...else allows only two-way decision making: one condition true, otherwise else
executes.
- if...elif allows multiple conditions and executes the first true condition.
Example:
if x > 0: print("Positive")
elif x == 0: print("Zero")
else: print("Negative")
Thus, elif increases flexibility by handling many cases.

(F) Algorithm for Median of Five Numbers:


Steps:
1. Input five numbers.
2. Store them in a list.
3. Sort the list in ascending order.
4. Pick the middle element (3rd element, index 2).
Dry run:
Numbers = [9,2,5,7,3]
Sorted = [2,3,5,7,9]
Median = 5
Thus, the algorithm ensures the middle value is found.

Q.2 Attempt the following (Any FOUR)

(A) What is a Module in Python?


A module is a file containing Python code (functions, classes, variables). It allows code
reusability and modular programming. To use a module, we import it using import
keyword.
Example:
import math
print(math.sqrt(16)) → 4.0
Thus, modules help organize code into separate files.

(B) Function returning multiple values:


In Python, a function can return multiple values packed into a tuple. These values can be
unpacked into variables.
Example:
def calc(a,b):
return a+b, a-b, a*b
x,y,z = calc(5,3)
Output: 8,2,15. This makes functions more flexible.

(C) Explain linspace() in array:


linspace() is a NumPy function that returns evenly spaced numbers within a range. It is
useful in scientific computing and plotting.
Example:
np.linspace(0,10,5) → [0,2.5,5,7.5,10]
This divides range 0-10 into 5 equal parts.

(D) '+=' operator:


The operator x += y is shorthand for x = x + y. It updates the variable directly.
Example:
x=5
x+=3 → 8
It is useful for compact code.

(E) Division Operators:


- '/' → true division (always float). Example: 7/2=3.5
- '//' → floor division (integer part). Example: 7//2=3
This helps in controlling precision.

(F) Set operations:


set_a={1,2,3}; set_b={3,4,5}
i) Unique: {1,2,4,5}
ii) Common: {3}
iii) Only in set_a: {1,2}
iv) 4 in set_b → True
v) Convert immutable → frozenset({1,2,3})

Q.3 Attempt the following (Any FOUR)

(A) Advantages and Disadvantages of Jupyter Notebook:


✔ Provides interactive environment for coding.
✔ Supports visualization, graphs, Markdown, and LaTeX.
✔ Easy to debug and share notebooks.
✘ Not suitable for very large applications.
✘ Dependency management can be difficult.
✘ Execution order may confuse beginners.

(B) get() method in dictionary:


The get() method retrieves value for a key. If key not found, returns None or default value.
Example:
d={"a":1}
d.get("b") → None
d.get("b",0) → 0
Thus, it prevents errors during key lookup.

(C) Operator for checking tuple membership:


Membership operators 'in' and 'not in' check if a value exists in tuple.
t=(1,2,3,4)
3 in t → True
5 in t → False

(D) str.split() vs str.join():


- split() breaks a string into a list based on delimiter.
- join() combines list elements into a string with separator.
Example:
s="a,b,c".split(",") → ['a','b','c']
",".join(s) → "a,b,c"

(E) Nested lists:


Nested lists are lists within lists. They are used for tables, matrices, and hierarchical
structures.
Example: matrix=[[1,2],[3,4]]
They provide flexibility but are complex to manage.

(F) Program on sentence operations:


sentence="Python programming is fun"
words=sentence.split()
First & last = words[0], words[-1] → Python fun
Middle character = sentence[len(sentence)//2]
Thus, indexing and slicing allow powerful text processing.

You might also like