Programming in Python · End Term · May 2026 · FN
Programming in Python · End Term · May 2026 FN
← Course papers · Start practice / exam
Questions and published explanations below are available without starting a test. Some questions may not have a published solution yet.
17 / 18 published solutions
Consider the following code snippet:
What does the code snippet print?
True -4 True 1
False -2 False 0
True -2 False 0
True 0 True 0
Evaluate carefully by type. a=bool(-1) and bool(0)=False. b=bool([]) or bool('False')=True, because a nonempty string is truthy regardless of its spelling. c=int(3.99)+int(-3.99)=3-3=0, since int truncates towards zero. d=int(True)+int(3.0//2)=1+1=2. e=True!=True is False. f=int(True & False)=0. Thus a|b is True and c-d is -2, giving `True -2 False 0`.
**Answer: C.**
A: **Incorrect:** Miscomputes truncation, equality and the final Boolean operation.
B: **Incorrect:** a|b is True because b is True.
C: **Correct:** Matches all four evaluated expressions.
D: **Incorrect:** c-d is -2 and e is False, rather than the displayed alternatives.
Question 3 MCQ · 3.0 marks
Consider the following code snippet.
What is the output?
4
10
16
100
Published solution
In Python 3 a list comprehension has its own scope for its iteration variable. Inside the comprehension, x takes 2,3,4 and ans becomes [4,9,16]. The outer x remains 10. Because the final statement prints x rather than ans, it prints 10.
**Answer: B.**
A: **Incorrect:** 4 is a list value, not the outer x.
B: **Correct:** The outer x remains 10.
C: **Incorrect:** 16 is the final squared list value, not the outer x.
D: **Incorrect:** 100 would be the square of the outer x, which is not what is printed.
Question 4 MCQ · 3.0 marks
Which of the following best describes what
does to the list
between indices
and
?
It reverses the entire sub-list from
to
, regardless of whether the
elements are multiples of 3.
It reverses only the elements that are **not** multiples of 3 in the range
, leaving multiples of 3 in their original positions.
It moves all multiples of 3 to the left and all other elements to the right (a
partition), without changing the relative order of the non-multiples themselves.
It sorts the sub-list from
to
in ascending order.
Published solution
The recursion skips an endpoint when its value is divisible by 3, leaving that position untouched. When neither endpoint is divisible by 3, it swaps those two values and recurses inward. Thus only the subsequence of nonmultiples is reversed. For the supplied tray, nonmultiples 4,7,5,8 become 8,5,7,4 in their original available positions; the result is [9,8,5,6,7,12,4,3].
**Answer: B.**
A: **Incorrect:** Multiples of 3 are skipped, so the entire sublist is not reversed.
B: **Correct:** Describes reversal of exactly the nonmultiple subsequence.
C: **Incorrect:** No partition moves all multiples to one side.
D: **Incorrect:** The procedure swaps endpoints, rather than sorting by magnitude.
Question 5 MCQ · 3.0 marks
Consider the following code snippet.
What will be the output?
Published solution
Python treats -0 as 0. Therefore s[-0::-1] means s[0::-1], not s[::-1]. Start at index 0 ('P') and move backwards; there is no earlier character to include. The resulting string contains only P. The quotes used to display a string in an option are not printed by print.
**Answer: C.**
A: **Incorrect:** Would be the full reversal from s[::-1], whose start is omitted.
B: **Incorrect:** The starting index 0 is included, so the result is not empty.
C: **Correct:** Only P at index 0 is included.
D: **Incorrect:** N is at the end, but the slice explicitly starts at 0.
Question 6 MCQ · 3.0 marks
Consider the following code snippet.
**Note:**
returns the largest integer less than or equal to
. For
example,
returns
, and
returns
.
What is the output of the above code?
1
16
1616
16
Published solution
x is the string '1081616'. int(x[3:]) converts '1616' to 1616; int(True)=1; floor(16.08)=16; int(x[5:]) also gives 16. A set removes the repeated 16, leaving {1,16,1616}. Sorting gives [1,16,1616], so r1[2]=1616. characters[::2] takes indices 0,2,4: Harry, Hermione, McGonagall.
**Answer: C.**
A: **Incorrect:** Uses the wrong sorted index and the wrong character slice.
B: **Incorrect:** 16 is at index 1, not index 2.
C: **Correct:** Correctly gives 1616 and the index-0,2,4 names.
D: **Incorrect:** Misreads both the numeric index and the slice start/step.
Question 7 MCQ · 3.0 marks
Consider the following class used at the school fruit stall to track volunteer hours.
What are the values of
and
, respectively?
True, 1.25
True, 7.00
False, 7.00
False, 1.25
Published solution
The score is sqrt(stall_counter_hours+2×free_meal_distribution_hours), rounded to two decimals. Hari's score is sqrt(30+2×26)=sqrt(82)≈9.06, meeting the threshold of 9, so x=True. Madhava's score is sqrt(49+2×0)=7.00, so y=7.0. Calling a method on one object uses that object's stored hours.
**Answer: B.**
A: **Incorrect:** Hari is eligible, but Madhava's score is 7.00, not 1.25.
B: **Correct:** Matches the eligibility result and Madhava's calculated score.
C: **Incorrect:** Hari's score exceeds 9, so x is not False.
D: **Incorrect:** Both the eligibility and the numeric score are wrong.
Question 8 MSQ · 3.0 marks
You are debugging a Python script that manages a local database file (
).
The script contains the following function:
Which of the following strings can be passed as the
argument so that the script successfully
creates the missing file, writes the data, and returns
without throwing an error?
Published solution
The file does not yet exist, and the function must both write and read. w+ creates it and enables both operations. a+ also creates it with reading and appending enabled; seek(0) moves back to read the newly written contents. r+ requires an existing file. Plain w creates and writes, but does not permit the final read.
**Answer: A, B.**
A: **Correct:** Creates the file with both writing and reading enabled.
B: **Correct:** Creates the file and permits reading after the seek.
C: **Incorrect:** Raises FileNotFoundError because the file is absent.
D: **Incorrect:** The final read fails because w is write-only.
Question 9 MSQ · 3.0 marks
Consider the following code snippet.
Select **all** correct statements.
Removing
changes the output by 10.
Removing the entire
block (i.e., removing both
and
) changes
by 1000.
The program prints
.
The program prints
.
Published solution
Starting at 0, the try block adds 1, then immediately raises ValueError. The later +10 is skipped. The except handler adds 100, and finally adds 1000 regardless of the exception, giving 1101. Removing finally removes exactly 1000. Removing raise instead permits +10 but prevents the +100 handler, giving 1011, a change of 90 rather than 10.
**Answer: B, D.**
A: **Incorrect:** Without raise the result becomes 1011, differing by 90.
B: **Correct:** The finally block contributes exactly 1000.
C: **Incorrect:** 1011 is the value only if the raise statement is removed.
D: **Correct:** The actual result is 1+100+1000=1101.
Question 10 MSQ · 3.0 marks
Consider the following snippet.
Select **all** that apply.
The first line of output is
.
The first line of output is
.
The second line of output is
.
The third line of output is
.
The fourth line of output is
.
Published solution
Tuple concatenation gives t3=(10,20,30,40,50,60). The slice t3[1:4] is (20,30,40). Set union of {10,20,30} and {20,30,40} has four elements. Both contain 30, so the Boolean expression is True. Assigning t1[0] raises TypeError because tuples are immutable. The handler rebinds t1 to t3[-1:], which is the one-element tuple (60,), not the integer 60.
**Answer: A, C, D.**
A: **Correct:** The slice includes indices 1,2,3.
B: **Incorrect:** Incorrectly includes index 0.
C: **Correct:** The union contains 10,20,30,40, giving length 4.
D: **Correct:** 30 belongs to both sets.
E: **Incorrect:** The final print is (60,), including its tuple structure, not plain 60.
Question 11 NAT · 3.0 marks
Consider the following code snippet.
What is the output of the above code?
Published solution
The loop's n values change 10→5→4→2→1→0. Even positive values are halved with integer division; odd values are reduced by 1. There are five transitions and count increases once for each, so the program prints 5. The final zero is the stopping state rather than an additional iteration.
**Answer: 5.**
Question 12 NAT · 3.0 marks
At Hari's yoga class, a running total is maintained while processing attendance codes.
What is the output of the above code?
Published solution
Start total=5,errors=0. Values 10 and '8' add 20//10=2 and 20//8=2, giving total=9. Zero raises ZeroDivisionError, adding 2 to errors and subtracting 1 from total (now 8). 'yoga' raises ValueError, adding another 1 to errors. Finally 2 adds 20//2=10. End values are total=18,errors=3, so it prints 21.
**Answer: 21.**
Question 13 NAT · 3.0 marks
Consider the following code snippet.
How many function calls (including the initial call) occur for
?
Published solution
Trace the arguments of every invocation: f(100)→f(50)→f(25)→f(12)→f(6)→f(3)→f(1). Integer division truncates each positive argument down. The base call f(1) also counts, even though it returns immediately. There are seven calls including the initial call. There is just one recursive branch per invocation.
**Answer: 7.**
Question 14 NAT · 3.0 marks
Consider the following code snippet.
Enter the sum of all the values present in the dictionary.
Published solution
The keys are parity values i%2. For i=0,2,4, key 0 accumulates 0+2+4=6. For i=1,3, key 1 accumulates 1+3=4. dict.get supplies zero when a key first appears. The final dictionary is {0:6,1:4}, so the sum of its values is 6+4=10.
**Answer: 10.**
Question 15 NAT · 3.0 marks
In Madhava's shop, each item is stored in its own tray. Each tray can have a maximum of **40** items.
Madhava stores the inventory as a dictionary, where the key is the item name and the value is a
list containing the quantity of that item in each tray.
When Madhava counts the inventory, an item contributes to the total only if its overall quantity is
at least **80**. Otherwise, the item is added to a reload list.
Consider the following code.
What is the output of the above code?
Published solution
Sum trays separately for each item. Ladoo totals 20+30+10=60, below 80, so it enters reload_list and contributes nothing to total. Barfi totals 40+30+20=90 and jalebi totals 40+40+10=90; both contribute, making total=180. There is one reload item, so total+len(reload_list)=180+1=181. The threshold applies to an item's overall quantity, not each tray.
**Answer: 181.**
Question 16 MCQ · 2.0 marks
The sweet shop owned by Madhava has four salespersons. Their sales are tracked using
dictionaries. Each salesperson earns points based on their sales and the daily bonus for that day (5
for weekdays and 8 for weekends).
**Note:**
returns the element for which the
function returns the largest value.
Based on the above data, answer the given subquestions
What does the given code print?
Ahmed 2755
Vishaka 2755
Harry 2353
Raises a
because Purushottam is missing
and
Published solution
Multiply each recorded day's sales by that day's bonus, then sum by salesperson. Harry: (120+95+150+80)×5+(8+8)×8=2353. Vishaka and Ahmed each have (120+95+0+80)×5+(80+80)×8=2755. Purushottam has (5+5+5)×5+(8+8)×8=203. max returns the first key among tied maximum values in iteration order; Vishaka precedes Ahmed, so the print is Vishaka 2755. Missing days are never accessed because the inner loop visits only each person's recorded entries.
**Answer: B.**
A: **Incorrect:** Ahmed ties for the maximum but follows Vishaka in dictionary order.
B: **Correct:** Vishaka is the first maximum-scoring key.
C: **Incorrect:** Harry's score is 2353 but is not maximal.
D: **Incorrect:** No lookup for an absent day is performed.
Question 17 MCQ · 2.0 marks
The sweet shop owned by Madhava has four salespersons. Their sales are tracked using
dictionaries. Each salesperson earns points based on their sales and the daily bonus for that day (5
for weekdays and 8 for weekends).
**Note:**
returns the element for which the
function returns the largest value.
Based on the above data, answer the given subquestions
Madhava also tracks his shop's daily sales as a list of tuples containing the day, sweet name, and
sales amount.
Which of the following code snippets correctly prints the day and **total** sales for the day with the
highest total sales (across all sweets sold that day)?
Published solution
First aggregate all sweet records by day: Monday has 4000+8000+400=12400, Tuesday 2000, and Wednesday 8000. Then choose the day with the largest aggregate, giving Mon 12400. A correctly accumulates with get(day,0)+amount before applying max. Choosing a largest individual transaction would miss the combined Monday total.
**Answer: A.**
A: **Correct:** Correctly sums by day and maximises those totals.
B: **Incorrect:** Finds the largest single sweet transaction instead of total daily sales.
C: **Incorrect:** Overwrites each day's earlier amounts instead of adding them.
D: **Incorrect:** Sorting individual records by amount still selects a single transaction rather than a daily total.
Question 18 NAT · 2.0 marks
Consider the following code snippet:
Based on the above data, answer the given subquestions
What is the element at position
in
?
A published solution is not available for this question yet.
Question 19 NAT · 2.0 marks
Consider the following code snippet:
Based on the above data, answer the given subquestions
What is the value of the second line of output?
Published solution
The transpose swaps matrix[i][j] with matrix[j][i] only when j>i, so each off-diagonal pair is swapped once. The result is [[2,4,7],[1,5,8],[3,6,9]]. Transposition leaves diagonal values 2,5,9 unchanged. diagonal_product multiplies these, giving 2×5×9=90 for the second printed line.
**Answer: 90.**