MauryaHub PYQ Practice

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

Question 2 MCQ · 3.0 marks

Consider the following code snippet:
Source diagram or notation
What does the code snippet print?
6406537371715.
True -4 True 1
6406537371716.
False -2 False 0
6406537371717.
True -2 False 0
6406537371718.
True 0 True 0

Published solution

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.

Report a correction for Q2

Question 3 MCQ · 3.0 marks

Consider the following code snippet.
Source diagram or notation
What is the output?
6406537371719.
4
6406537371720.
10
6406537371721.
16
6406537371722.
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.

Report a correction for Q3

Question 4 MCQ · 3.0 marks

Source diagram or notation
Which of the following best describes what
Source diagram or notation
does to the list
Source diagram or notation
between indices
Source diagram or notation
and
Source diagram or notation
?
6406537371723.
It reverses the entire sub-list from
Source diagram or notation
to
Source diagram or notation
, regardless of whether the elements are multiples of 3.
6406537371724.
It reverses only the elements that are **not** multiples of 3 in the range
Source diagram or notation
, leaving multiples of 3 in their original positions.
6406537371725.
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.
6406537371726.
It sorts the sub-list from
Source diagram or notation
to
Source diagram or notation
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.

Report a correction for Q4

Question 5 MCQ · 3.0 marks

Consider the following code snippet.
Source diagram or notation
What will be the output?
6406537371727.Source diagram or notation
6406537371728.Source diagram or notation
6406537371729.Source diagram or notation
6406537371730.Source diagram or notation

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.

Report a correction for Q5

Question 6 MCQ · 3.0 marks

Consider the following code snippet. **Note:**
Source diagram or notation
returns the largest integer less than or equal to
Source diagram or notation
. For example,
Source diagram or notation
returns
Source diagram or notation
, and
Source diagram or notation
returns
Source diagram or notation
.
Source diagram or notation
What is the output of the above code?
6406537371731.
1
Source diagram or notation
6406537371732.
16
Source diagram or notation
6406537371733.
1616
Source diagram or notation
6406537371734.
16
Source diagram or notation

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.

Report a correction for Q6

Question 7 MCQ · 3.0 marks

Consider the following class used at the school fruit stall to track volunteer hours.
Source diagram or notation
What are the values of
Source diagram or notation
and
Source diagram or notation
, respectively?
6406537371735.
True, 1.25
6406537371736.
True, 7.00
6406537371737.
False, 7.00
6406537371738.
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.

Report a correction for Q7

Question 8 MSQ · 3.0 marks

You are debugging a Python script that manages a local database file (
Source diagram or notation
). The script contains the following function:
Source diagram or notation
Which of the following strings can be passed as the
Source diagram or notation
argument so that the script successfully creates the missing file, writes the data, and returns
Source diagram or notation
without throwing an error?
6406537371739.Source diagram or notation
6406537371740.Source diagram or notation
6406537371741.Source diagram or notation
6406537371742.Source diagram or notation

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.

Report a correction for Q8

Question 9 MSQ · 3.0 marks

Consider the following code snippet.
Source diagram or notation
Select **all** correct statements.
6406537371743.
Removing
Source diagram or notation
changes the output by 10.
6406537371744.
Removing the entire
Source diagram or notation
block (i.e., removing both
Source diagram or notation
and
Source diagram or notation
) changes
Source diagram or notation
by 1000.
6406537371745.
The program prints
Source diagram or notation
.
6406537371746.
The program prints
Source diagram or notation
.

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.

Report a correction for Q9

Question 10 MSQ · 3.0 marks

Consider the following snippet.
Source diagram or notation
Select **all** that apply.
6406537371747.
The first line of output is
Source diagram or notation
.
6406537371748.
The first line of output is
Source diagram or notation
.
6406537371749.
The second line of output is
Source diagram or notation
.
6406537371750.
The third line of output is
Source diagram or notation
.
6406537371751.
The fourth line of output is
Source diagram or notation
.

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.

Report a correction for Q10

Question 11 NAT · 3.0 marks

Consider the following code snippet.
Source diagram or notation
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.**

Report a correction for Q11

Question 12 NAT · 3.0 marks

At Hari's yoga class, a running total is maintained while processing attendance codes.
Source diagram or notation
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.**

Report a correction for Q12

Question 13 NAT · 3.0 marks

Consider the following code snippet.
Source diagram or notation
How many function calls (including the initial call) occur for
Source diagram or notation
?

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.**

Report a correction for Q13

Question 14 NAT · 3.0 marks

Consider the following code snippet.
Source diagram or notation
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.**

Report a correction for Q14

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.
Source diagram or notation
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.**

Report a correction for Q15

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:**
Source diagram or notation
returns the element for which the
Source diagram or notation
function returns the largest value.
Source diagram or notation
Based on the above data, answer the given subquestions
What does the given code print?
6406537371757.
Ahmed 2755
6406537371758.
Vishaka 2755
6406537371759.
Harry 2353
6406537371760.
Raises a
Source diagram or notation
because Purushottam is missing
Source diagram or notation
and
Source diagram or notation

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.

Report a correction for Q16

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:**
Source diagram or notation
returns the element for which the
Source diagram or notation
function returns the largest value.
Source diagram or notation
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.
Source diagram or notation
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)?
6406537371761.Source diagram or notation
6406537371762.Source diagram or notation
6406537371763.Source diagram or notation
6406537371764.Source diagram or notation

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.

Report a correction for Q17

Question 18 NAT · 2.0 marks

Consider the following code snippet:
Source diagram or notation
Based on the above data, answer the given subquestions
What is the element at position
Source diagram or notation
in
Source diagram or notation
?

A published solution is not available for this question yet.

Report a correction for Q18

Question 19 NAT · 2.0 marks

Consider the following code snippet:
Source diagram or notation
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.**

Report a correction for Q19