MauryaHub PYQ Practice

Programming in Python · End Term · Jan 2026 · AN

Programming in Python · End Term · Jan 2026 AN

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

18 / 18 published solutions

Question 2 MCQ · 3.0 marks

Consider the following Python functions:
Source diagram or notation
Given that mystery(8) == 4, what should replace the missing line?
6406536932453.Source diagram or notation
6406536932454.Source diagram or notation
6406536932455.Source diagram or notation
6406536932456.Source diagram or notation

Published solution

Function c computes factorial recursively. Function a repeatedly removes the last decimal digit until only the leading digit remains. Thus c(8)=40320 and a(c(8))=4, as required. These functions must be composed in that order: factorial first, leading digit second. **Answer: B.** A: **Incorrect:** Returns 8 directly, not 4. B: **Correct:** Computes the leading digit of 8!=40320, giving 4. C: **Incorrect:** a(8)=8, so applying a again still gives 8. D: **Incorrect:** 8//10 is zero, not 4.

Report a correction for Q2

Question 3 MCQ · 3.0 marks

Consider the following code:
Source diagram or notation
What is the output?
6406536932457.Source diagram or notation
6406536932458.Source diagram or notation
6406536932459.Source diagram or notation
6406536932460.Source diagram or notation

Published solution

The first item pen has no earlier items, so its inner loop is empty and it is not appended. Each later item has length 6, which differs from the earlier pen's length 3. Therefore each is appended as soon as the comparison with pen succeeds, and the inner break prevents duplicate appends. The output is ['pencil','eraser','marker']. **Answer: A.** A: **Correct:** Each item after pen finds pen as an earlier different-length item. B: **Incorrect:** Pencil also differs in length from pen and must be included. C: **Incorrect:** Eraser also differs in length from pen and must be included. D: **Incorrect:** Pen has no earlier index to satisfy the inner condition.

Report a correction for Q3

Question 4 MCQ · 3.0 marks

Consider the following Python function.
Source diagram or notation
Which of the following inputs
Source diagram or notation
will produce the output 'b'?
6406536932461.Source diagram or notation
6406536932462.Source diagram or notation
6406536932463.Source diagram or notation
6406536932464.Source diagram or notation

Published solution

The function counts character frequencies, then scans characters alphabetically. It replaces the current winner only for a strictly larger count, so ties favour the alphabetically earliest character. In D, 'bbbcaac', b occurs 3 times and a and c occur twice, so b wins. In A, a and b tie at 3, so a wins; in B and C, a,b,c all tie at 2, so a wins. **Answer: D.** A: **Incorrect:** a and b tie at three occurrences; sorted iteration keeps a. B: **Incorrect:** All three letters tie at two; sorted iteration keeps a. C: **Incorrect:** All three letters tie at two; sorted iteration keeps a. D: **Correct:** b is uniquely most frequent, with three occurrences.

Report a correction for Q4

Question 5 MCQ · 3.0 marks

What does the following code compute?
Source diagram or notation
6406536932465.
85
6406536932466.
105
6406536932467.
91
6406536932468.
36

Published solution

selector(25) selects numbers for which (x-1)%4==0: [1,5,9,13,17,21,25]. All are odd, so aggregator retains and sums all seven. Their arithmetic-progression sum is 7×(1+25)/2=91. controller returns this sum, which is then printed. **Answer: C.** A: **Incorrect:** Does not equal the sum of the selected progression. B: **Incorrect:** Does not follow the x≡1 modulo 4 selection rule. C: **Correct:** 91 is the sum of all seven selected odd numbers. D: **Incorrect:** Omits terms of the selected progression.

Report a correction for Q5

Question 6 MCQ · 3.0 marks

Consider the snippets: **Code-1**
Source diagram or notation
**Code-2**
Source diagram or notation
**Code-3**
Source diagram or notation
Which statement is correct?
6406536932469.
Only Code-1 throws an error
6406536932470.
Only Code-2 throws an error
6406536932471.
Only Code-3 throws an error
6406536932472.
None throw an error

Published solution

Code 1 changes a list stored inside a tuple, which is permitted because the tuple's reference is not replaced. Code 2 concatenates a tuple and rebinds Y to a new tuple; this is also permitted. Code 3 assigns to Z[1], directly trying to replace a tuple element, so it raises TypeError. Tuple immutability concerns its slots, not the mutability of referenced objects. **Answer: C.** A: **Incorrect:** Appending to a contained list is legal. B: **Incorrect:** Tuple += creates a new tuple and rebinds the name; it is legal. C: **Correct:** Only the assignment to a tuple slot is forbidden. D: **Incorrect:** Code 3 does raise TypeError.

Report a correction for Q6

Question 7 MCQ · 3.0 marks

What is the output of the following snippet of code?
Source diagram or notation
6406536932473.Source diagram or notation
6406536932474.Source diagram or notation
6406536932475.Source diagram or notation
6406536932476.Source diagram or notation

Published solution

The dictionary counts characters in 'machinelearningwithpython'. n appears three times, while i, a, e, h, r and t each appear twice and the other letters once. The maximum-frequency scan therefore selects n. Sorting keys only controls the winner in a frequency tie; it does not change the counts. **Answer: B.** A: **Incorrect:** i occurs twice, less often than n. B: **Correct:** n occurs three times and has the unique maximum. C: **Incorrect:** e occurs twice. D: **Incorrect:** a occurs twice.

Report a correction for Q7

Question 8 MSQ · 3.0 marks

What is the output?
Source diagram or notation
6406536932477.
The output contains
Source diagram or notation
6406536932478.
The output contains
Source diagram or notation
6406536932479.
p == 5
6406536932480.
q == "Z"
6406536932481.
p == "F"
6406536932482.
q == "F"
6406536932483.
The finally block executes only when an exception occurs

Published solution

For f(2), try prints T and computes a pending return value 5. Before returning, finally prints Y and returns F, overriding that value. For f(0), try prints T, division raises ZeroDivisionError, except prints E and prepares Z; finally again prints Y and overrides the return with F. The full printed sequence is T,Y,T,E,Y on separate lines, and both p and q are 'F'. A finally block runs on normal return as well as handled exceptions. **Answer: B, E, F.** A: **Incorrect:** This is only the second call's three-line sequence; it omits the first call's T,Y output, so it is not the complete output intended by the option. B: **Correct:** Gives the complete five-line printed sequence. C: **Incorrect:** The return from finally overrides the pending 5. D: **Incorrect:** The return from finally overrides the pending Z. E: **Correct:** The first call ultimately returns F. F: **Correct:** The second call also ultimately returns F. G: **Incorrect:** The first call has no exception and still executes finally.

Report a correction for Q8

Question 9 MSQ · 3.0 marks

Source diagram or notation
is a list of tuples
Source diagram or notation
. Select **all** correct snippets that produce a list of names scoring **strictly above 80**, sorted by score descending.
6406536932484.Source diagram or notation
6406536932485.Source diagram or notation
6406536932486.Source diagram or notation
6406536932487.Source diagram or notation

Published solution

Sort records using score (tuple index 1) descending, retain only scores>80, and return the names at index 0. A combines these steps in a comprehension. B expresses the same operations with sorted, filter and map. Filtering a sorted sequence preserves its score order. **Answer: A, B.** A: **Correct:** Uses the score sorting key, strict filter and name projection. B: **Correct:** Sorts by descending score, filters strictly above 80 and maps tuples to names. C: **Incorrect:** Default sorting orders tuples primarily by name and retains tuples, not the required name list. D: **Incorrect:** Includes score 80 and does not perform the required descending sort.

Report a correction for Q9

Question 10 MSQ · 3.0 marks

Consider the following snippet of code:
Source diagram or notation
Select **all** that apply. **Note** • If a value is passed to
Source diagram or notation
, it reads that many number of characters from the current position of the file pointer. • The
Source diagram or notation
method moves the position of the file pointer to the given number of chars after the start of the file.
6406536932488.
The first line of the output is
Source diagram or notation
.
6406536932489.
The second line of the output is
Source diagram or notation
.
6406536932490.
The second line of the output is
Source diagram or notation
.
6406536932491.
There are 5 lines in the output.
6406536932492.
The output contains empty lines.

Published solution

The file contains Line1 through Line10 joined by newline characters. Two readline calls skip Line1 and Line2. read(5) returns 'Line3', leaving its newline unread. The next readline consumes that newline; read(10) then returns 'Line4\nLine', which prints across two lines. seek(0) resets the pointer; read(5) returns 'Line1'. A readline consumes its newline and another skips Line2, so the last read(5) returns 'Line3'. The displayed lines are Line3, Line4, Line, Line1, Line3: five lines with no blank line. **Answer: A, C, D.** A: **Correct:** The first two readline calls position the first print at Line3. B: **Incorrect:** The second printed line is Line4, not Line5. C: **Correct:** The next read starts at Line4 after consuming Line3's newline. D: **Correct:** One print contains an embedded newline, giving five output lines from four print calls. E: **Incorrect:** The embedded newline separates nonempty text; no output line is empty.

Report a correction for Q10

Question 11 NAT · 3.0 marks

Consider the following Python code snippet:
Source diagram or notation
What is the output of the program?

Published solution

Each recursive call counts uppercase letters at the two ends, then recurses on the interior slice lst[1:-1]. The pairs are A/G (2), b/f (0), C/E (2), and central d (0). Total=2+0+2+0=4. The single-element base case counts the central element only once. **Answer: 4.**

Report a correction for Q11

Question 12 NAT · 3.0 marks

What is printed?
Source diagram or notation

Published solution

Start total=1,err=0. The successful try operations multiply rather than add: for 5, total becomes 1×(10//5)=2; for '2', it becomes 2×(10//2)=10. Zero raises ZeroDivisionError, so err gains 3 and total gains 2, becoming 12. 'x' fails int conversion and the general except adds 1 to err. Final total=12 and err=4, so the print is 16. Failed right-hand expressions do not perform the multiplication assignment. **Answer: 16.**

Report a correction for Q12

Question 13 NAT · 3.0 marks

Fill in the blank with the missing expression so that the following conditions hold:
Source diagram or notation
Source diagram or notation
Source diagram or notation
Source diagram or notation

Published solution

Use a positive integer divisor 4 in the missing slot. The base cases give f(0)=0 and f(1)=1. Then f(2)=f(1)+f(0)=1, f(3)=f(1)+f(0)=1, and f(4)=f(2)+f(1)=2, satisfying all three requirements. A divisor larger than 4 would make f(4)=f(2)+f(0)=1; divisors 2 or 3 fail an earlier required value. **Answer: 4.**

Report a correction for Q13

Question 14 NAT · 3.0 marks

What is the output?
Source diagram or notation

Published solution

S accumulates copies of successive prefixes rather than being reset. At i=0, S=[2], contributing 2 to T. At i=1, S becomes [2,2,4], contributing 8. At i=2, S becomes [2,2,4,2,4,6], contributing 20. Therefore the final running total is 2+8+20=30. The inner loop traverses the entire accumulated S after each extension. **Answer: 30.**

Report a correction for Q14

Question 15 NAT · 3.0 marks

What is the output? Note : The function divmod(a, b) gives two values: (quotient : x // 10 , remainder : x % 10) for example: divmod(27, 10) gives (2, 7)
Source diagram or notation

Published solution

reduce_once repeatedly uses divmod(x,10) to remove the last digit and add that remainder, producing the digit sum. For 999 the first sum is 9+9+9=27. Since 27 still has two digits, g recurses and sums them to 2+7=9. The one-digit base case then returns 9. The recursion repeats digit summation until a single digit remains. **Answer: 9.**

Report a correction for Q15

Question 16 MCQ · 2.0 marks

Source diagram or notation
Based on the above data, answer the given subquestions.
What is printed by
Source diagram or notation
?
6406536932498.
4
6406536932499.
No output
6406536932500.
7
6406536932501.
8

Published solution

Trace the moves UUDUDDDUU. Starting from floor 0, the successive floors are 1,2,1,2,1,0,-1,0,1. None equals -2, so the conditional print(i+1) is never executed and break is never reached. The later print(u,d) still runs; 'No output' here refers specifically to the conditional print asked about. **Answer: B.** A: **Incorrect:** The fourth move leaves floor 2, not -2. B: **Correct:** The floor never reaches -2, so this print produces no output. C: **Incorrect:** The seventh move reaches -1, not -2. D: **Incorrect:** The eighth move returns to zero, not -2.

Report a correction for Q16

Question 17 MCQ · 2.0 marks

Source diagram or notation
Based on the above data, answer the given subquestions.
What is printed as the output?
6406536932502.Source diagram or notation
6406536932503.Source diagram or notation
6406536932504.Source diagram or notation
6406536932505.Source diagram or notation

Published solution

The floor never reaches -2, so all nine moves are processed. There are five U characters and four D characters. The loop increments u or d for each corresponding move, and the final print(u,d) therefore outputs `5 4`. **Answer: C.** A: **Incorrect:** There are five upward moves, not four. B: **Incorrect:** The loop processes all nine moves, not just six. C: **Correct:** Correctly counts five U moves and four D moves. D: **Incorrect:** Interchanges the upward and downward counts.

Report a correction for Q17

Question 18 MCQ · 2.0 marks

Source diagram or notation
Based on the above data, answer the given subquestions.
What is the value of
Source diagram or notation
?
6406536932506.
1
6406536932507.
2
6406536932508.
3
6406536932509.
8

Published solution

Counter.count is a shared class attribute increased once by each constructor. a=Counter(3) and b=Counter(5) create two instances, so the class count becomes 2. Calls to inc change each instance's x, not the class count. **Answer: B.** A: **Incorrect:** There are two constructor calls, not one. B: **Correct:** The class counter is incremented exactly twice. C: **Incorrect:** inc does not construct a third object. D: **Incorrect:** 8 is the sum of the initial x values, unrelated to the object counter.

Report a correction for Q18

Question 19 MCQ · 2.0 marks

Source diagram or notation
Based on the above data, answer the given subquestions.
What is the value of
Source diagram or notation
?
6406536932510.
8
6406536932511.
10
6406536932512.
11
6406536932513.
12

Published solution

a starts with x=3 and a.inc() increases it to 4, returning c=4. b starts with x=5 and b.inc() increases it to 6, returning d=6. The instances have separate x attributes. Therefore c+d=4+6=10. **Answer: B.** A: **Incorrect:** Uses the original 3+5 without applying increments. B: **Correct:** Correctly adds the two returned updated values, 4 and 6. C: **Incorrect:** Includes an extra increment absent from the code. D: **Incorrect:** Does not match either instance's single increment.

Report a correction for Q19