MauryaHub PYQ Practice

Programming in Python · End Term · Jan 2026 · FN

Programming in Python · End Term · Jan 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.

18 / 18 published solutions

Question 2 MCQ · 3.0 marks

Consider the following recursive functions:
Source diagram or notation
Which of the following function calls returns the last digit of 5! ?
6406536956268.Source diagram or notation
6406536956269.Source diagram or notation
6406536956270.Source diagram or notation
6406536956271.Source diagram or notation

Published solution

First compute the factorial: 5!=5×4×3×2×1=120. Then last_digit(n) returns n%10, so last_digit(factorial(5)) gives 120%10=0. The order of function composition matters: the inner call supplies the result to the outer call. **Answer: A.** A: **Correct:** Computes 5! first and then extracts its final digit. B: **Incorrect:** Computes factorial(5), since last_digit(5)=5, returning 120 rather than 0. C: **Incorrect:** Multiplies 5 by 120 and returns 600, not its last digit. D: **Incorrect:** Computes factorial(5%10)=factorial(5)=120.

Report a correction for Q2

Question 3 MCQ · 3.0 marks

Consider the following class:
Source diagram or notation
What are the values of
Source diagram or notation
,
Source diagram or notation
, and
Source diagram or notation
respectively?
6406536956272.
1, False, 300
6406536956273.
2, False, 500
6406536956274.
2, True, 300
6406536956275.
3, True, 300

Published solution

Creating acc1 and acc2 executes __init__ twice, so the shared class counter is 2. The deposit changes only acc1's balance from 1000 to 1500. acc2 starts at 500; withdrawing 200 is permitted, reduces its balance to 300, and returns True. Hence the values are 2, True, 300. A successful method call does not create another account. **Answer: C.** A: **Incorrect:** There are two accounts and the withdrawal succeeds. B: **Incorrect:** The withdrawal succeeds and reduces acc2's balance to 300. C: **Correct:** Matches the account count, Boolean result and remaining balance. D: **Incorrect:** Depositing or withdrawing does not increment the constructor's counter.

Report a correction for Q3

Question 4 MCQ · 3.0 marks

What will be the output of the following Python code?
Source diagram or notation
6406536956276.Source diagram or notation
6406536956277.Source diagram or notation
6406536956278.Source diagram or notation
6406536956279.Source diagram or notation

Published solution

The condition checks inequality, not equality. For r, the first inner comparison against y already succeeds, so append r and break the inner loop. The same happens for e versus y and d versus y. The outer loop continues through all characters of red, giving ['r','e','d']. This is not a test for common letters between the two strings. **Answer: A.** A: **Correct:** Each outer character is appended at its first unequal inner character. B: **Incorrect:** Incorrectly omits e even though e differs from the first y. C: **Incorrect:** Would fit a common-letter idea, but the code tests inequality. D: **Incorrect:** The code appends characters from colors1, not all of colors2.

Report a correction for Q4

Question 5 MCQ · 3.0 marks

What is the output of the following snippet of code?
Source diagram or notation
6406536956280.Source diagram or notation
6406536956281.Source diagram or notation
6406536956282.Source diagram or notation
6406536956283.Source diagram or notation

Published solution

Ignoring the space, hello world contains l three times, o twice, and each other letter once. The dictionary records these frequencies. The second loop scans sorted keys and keeps a new key only for a strictly greater frequency. l reaches the maximum of 3, so it prints `l`. **Answer: B.** A: **Incorrect:** h occurs only once. B: **Correct:** l occurs three times, the maximum frequency. C: **Incorrect:** o occurs twice, fewer than l. D: **Incorrect:** w occurs once.

Report a correction for Q5

Question 6 MCQ · 3.0 marks

What is the output of the following code?
Source diagram or notation
6406536956284.
15
6406536956285.
10
6406536956286.
20
6406536956287.
5

Published solution

range(1,6) visits 1,2,3,4,5. The if/elif/else chain contributes +2 for 1, +2 for 2, -3 for 3, +4 for 4, and +10 for 5. The running totals are 2,4,1,5,15. Thus it prints 15. Only one branch executes for each integer. **Answer: A.** A: **Correct:** Matches 2+2-3+4+10=15. B: **Incorrect:** Does not include all five contributions correctly. C: **Incorrect:** Does not account for the subtraction at i=3 correctly. D: **Incorrect:** This is the total before processing i=5.

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
6406536956288.Source diagram or notation
6406536956289.Source diagram or notation
6406536956290.Source diagram or notation
6406536956291.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

Source diagram or notation
is a list of tuples. Each tuple is of the form
Source diagram or notation
. Select all snippets of code that create a list
Source diagram or notation
containing the names of products with price greater than 500. You can assume that
Source diagram or notation
is already defined. **Sample Input**
Source diagram or notation
**Sample Output**
Source diagram or notation
6406536956292.Source diagram or notation
6406536956293.Source diagram or notation
6406536956294.Source diagram or notation
6406536956295.Source diagram or notation

Published solution

The requirement has two parts: select products with price strictly greater than 500, and output only their names. A correctly combines tuple unpacking, the >500 filter and the name expression. C performs the same logic with an explicit loop and append. The sample alone cannot distinguish >500 from >=500 because it contains no price exactly 500; the stated requirement must be checked. **Answer: A, C.** A: **Correct:** Correctly returns the name for each tuple whose price is greater than 500. B: **Incorrect:** Includes a product priced exactly 500, violating the strict condition. C: **Correct:** The loop tests price>500 and appends only the name. D: **Incorrect:** Tests equality to 500 and returns whole tuples rather than the required names.

Report a correction for Q8

Question 9 MSQ · 3.0 marks

Consider the following class:
Source diagram or notation
Which of the following statements are **TRUE**?
6406536956296.Source diagram or notation
contains 2 entries
6406536956297.Source diagram or notation
returns 87
6406536956298.Source diagram or notation
is equal to
Source diagram or notation
6406536956299.Source diagram or notation
is True
6406536956300.Source diagram or notation
is a string

Published solution

Each constructor creates its own grades dictionary and registers the object in the shared students dictionary. There are two distinct names, so the registry has two entries. Priya has marks 90 and 85; her method uses integer division, (90+85)//2=87. Rahul has only Math:75. The registry stores objects, not name strings, and one student's marks do not populate the other's dictionary. **Answer: A, B, D.** A: **Correct:** Priya and Rahul are the two registry entries. B: **Correct:** Integer division of 175 by 2 gives 87. C: **Incorrect:** Rahul has only Math:75; Science:85 belongs to Priya. D: **Correct:** Priya's dictionary contains exactly Math:90 and Science:85. E: **Incorrect:** The value under Rahul is a StudentGrade instance, not a string.

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.
6406536956301.
The first line of the output is
Source diagram or notation
.
6406536956302.
The second line of the output is
Source diagram or notation
.
6406536956303.
The second line of the output is
Source diagram or notation
.
6406536956304.
There are 5 lines in the output.
6406536956305.
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=10 and count=0. Values 2, '10' and 5 convert successfully to integers, adding 7,15 and 10, so total becomes 42. Converting 'abc' raises ValueError before result is assigned for that iteration. The handler adds 1 to count and subtracts 3 from total, leaving total=39,count=1. The final print is total+count=40. **Answer: 40.**

Report a correction for Q12

Question 13 NAT · 3.0 marks

What is the output of the following snippet?
Source diagram or notation

Published solution

The recursion compares the first and last character, rejecting immediately if they differ, and otherwise checks the inside substring. 11, 121, 151 and 99 read the same forwards and backwards. 12 fails its first comparison. Therefore four numbers pass and count is 4. Converting each number to a string makes character indexing possible. **Answer: 4.**

Report a correction for Q13

Question 14 NAT · 3.0 marks

What is the output?
Source diagram or notation

Published solution

accumulated is extended rather than reset. For i=0 it becomes [10], and its sum 10 is added. For i=1, [10,20] is appended, making [10,10,20] with sum 40. For i=2, [10,20,30] is appended, making [10,10,20,10,20,30] with sum 100. The running total adds 10+40+100=150. The nested loop re-sums all accumulated values each time. **Answer: 150.**

Report a correction for Q14

Question 15 NAT · 3.0 marks

What is the output of the following code?
Source diagram or notation

Published solution

helper(x) returns x squared. nested_call(0) returns zero; each positive call adds its own square to the result for n-1. Thus nested_call(4)=4²+3²+2²+1²+0=16+9+4+1=30. Defining the helper inside the function does not add extra terms; its returned value is added once per non-base call. **Answer: 30.**

Report a correction for Q15

Question 16 MCQ · 2.0 marks

Source diagram or notation
Based on the above program, answer the given subquestions.
What does the first
Source diagram or notation
statement output?
6406536956311.
97
6406536956312.
93
6406536956313.
288
6406536956314.
98

Published solution

The first loop iterates over dictionary keys and adds their corresponding values. The total is d['P']+d['C']+d['B']=97+93+98=288. It sums all marks, rather than selecting only the largest mark. **Answer: C.** A: **Incorrect:** 97 is P's individual value. B: **Incorrect:** 93 is C's individual value. C: **Correct:** 288 is the sum of the three values. D: **Incorrect:** 98 is the maximum individual value, not the sum.

Report a correction for Q16

Question 17 MCQ · 2.0 marks

Source diagram or notation
Based on the above program, answer the given subquestions.
What is printed in the second line?
6406536956315.
P
6406536956316.
C
6406536956317.
B
6406536956318.
None

Published solution

max(d,key=d.get) iterates over the keys but compares d.get(key), their associated values. B has 98, which exceeds P's 97 and C's 93. The expression returns the winning key, so the second print is B rather than 98. **Answer: C.** A: **Incorrect:** P's 97 is below B's 98. B: **Incorrect:** C's 93 is below the other values. C: **Correct:** B has the largest associated value. D: **Incorrect:** The dictionary is nonempty and max returns its winning key.

Report a correction for Q17

Question 18 NAT · 2.0 marks

Source diagram or notation
Based on the above program, answer the given subquestions.
What is the element at position [0][2] in
Source diagram or notation
?

Published solution

The function changes only diagonal positions matrix[i][i]. Position [0][2] is off the main diagonal, so it remains the original value 3. For completeness, i=0 swaps diagonal values 1 and 9, i=1 swaps the centre with itself, and i=2 swaps 9 and 1 back. The loop therefore restores the original diagonal too. **Answer: 3.**

Report a correction for Q18

Question 19 NAT · 2.0 marks

Source diagram or notation
Based on the above program, answer the given subquestions.
What is the value of the second line of output?

Published solution

The diagonal-swap loop visits both ends of every pair, so each noncentral swap is undone later. For the 3×3 matrix, the final diagonal is again 1,5,9. g multiplies these diagonal entries, starting from prod=1: 1×1×5×9=45. It is a product, not the trace (sum), and not a product of every matrix element. **Answer: 45.**

Report a correction for Q19