MauryaHub PYQ Practice

Programming in Python · Quiz 1 · Sep 2025

Programming in Python · Quiz 1 · Sep 2025

← Course papers · Start practice / exam

This page contains the reliably extracted subset, not the complete original paper.

Questions and published explanations below are available without starting a test. Some questions may not have a published solution yet.

14 / 15 published solutions

Question 2 MCQ · 0.0 marks

**Useful Information** **NAT → integer** For all NAT questions in this exam, the answer will always be an integer and not a float value. If the answer to a question is 18, then just enter that value. Do not enter 18.0
6406535107702.
Instructions has been mentioned above.
6406535107703.
This Instructions is just for a reference & not for an evaluation.

A published solution is not available for this question yet.

Report a correction for Q2

Question 3 MCQ · 3.0 marks

Consider the following Python code. What will be the output? s = "programming" print(s[::-1].replace("g", "#"))
6406535107704.
#nimmarrop#
6406535107705.
#nimmar#orp
6406535107706.
programmin#
6406535107707.
gni#marropg

Published solution

**1. Reverse the string:** The slice s[::-1] traverses “programming” from the last character to the first, producing “gnimmargorp”. **2. Replace all matching characters:** str.replace replaces every g, not just the first. The two g characters become #, giving “#nimmar#orp”. **Answer: B — #nimmar#orp.** A: **Incorrect:** It reverses/reorders the middle letters incorrectly. B: **Correct:** It preserves the reversed order and replaces both g characters. C: **Incorrect:** It changes a character without first reversing the string. D: **Incorrect:** It neither gives the correct reversal nor replaces the required characters.

Report a correction for Q3

Question 4 MCQ · 3.0 marks

Consider the following Python code. What will be the output? x, y, z = "", "python", 0 print((x or y) and (z or len(y)))
6406535107708.
6
6406535107709.
""
6406535107710.
"python"
6406535107711.
0
6406535107712.
True

Published solution

**1. Evaluate the left operand:** Empty string x is false, so x or y returns y, the nonempty string “python”. **2. Evaluate the right operand:** Since the left value is true, and evaluates z or len(y). The number z=0 is false, so this returns len("python")=6. Python and/or return operands rather than forcing Boolean results. **Answer: A — 6.** A: **Correct:** The final operand returned is integer 6. B: **Incorrect:** The empty string is bypassed by x or y. C: **Incorrect:** The nonempty string makes and continue to its right operand. D: **Incorrect:** Zero is bypassed by z or len(y). E: **Incorrect:** Truthiness controls evaluation, but the returned value is 6 rather than Boolean True.

Report a correction for Q4

Question 5 MCQ · 3.0 marks

Consider the following python code. What will the value stored in x at the end of the program execution? x = 5 x = x + 2.0 x = str(x) + "3" x = x * 2 x = [x] x = x + [10]
6406535107713.
['7.03', '7.03', 10]
6406535107714.
['7.03', 10]
6406535107715.
['7.037.03']
6406535107716.
['7.037.03', 10]

Published solution

**1. Track the arithmetic and conversion:** Starting at 5, adding 2.0 gives floating-point 7.0. Converting to string and appending “3” gives “7.03”. **2. Track repetition and list construction:** Multiplying the string by 2 gives “7.037.03”. Wrapping it creates one list element; concatenating [10] adds a second element. The final list is ['7.037.03',10]. **Answer: D — ['7.037.03', 10].** A: **Incorrect:** String multiplication occurs before list wrapping, so it does not create two separate string elements. B: **Incorrect:** It omits repetition of the string. C: **Incorrect:** It omits the final list concatenation with [10]. D: **Correct:** It follows the type changes and both final list operations.

Report a correction for Q5

Question 6 MCQ · 4.0 marks

You have a list of lists: You want to **compute the sum of all even numbers greater than 3**. Consider the two code snippets below (with the condition left blank): **Snippet-1** s = 0 for row in data: for val in row: __________: s += val print(s) **Snippet-2** s = 0 i = 0 while i < len(data): j = 0 while _________: if data[i][j] % 2 == 0 and data[i][j] > 3: s += data[i][j] j += 1 i += 1 print(s) Which of the following conditions, when filled in the blanks in both snippets, will give the **correct result**?
6406535107717.
**Snippet 1:** if element % 2 == 0 and element > 3: **Snippet 2:** i > len(data[j]
6406535107718.
**Snippet 1:** if element % 2 != 0 and element > 3: **Snippet 2:** i < len(data[j]
6406535107719.
**Snippet 1:** if element % 2 == 0 and element > 3: **Snippet 2:** j < len(data[i]):
6406535107720.
**Snippet 1:** if element % 2 == 0 and element < 3: **Snippet 2:** j > len(data[i]):

Published solution

**1. Select the values:** The current value must satisfy both conditions: val%2==0 and val>3. Using and excludes odd values and even values at most 3. **2. Bound the inner index correctly:** For row i, j runs while j<len(data[i]); the row length, rather than the number of rows, determines the safe bound. Thus C supplies the intended predicate/bound pair after correcting its variable name. **Answer: C — The condition pair in C, with element corrected to val.** **Convention note:** The source for-loop variable is val, whereas the options call it element. Taken literally without defining element, even C raises NameError. The intended correction is if val%2==0 and val>3, together with while j<len(data[i]). Loop bodies must be properly indented; the PDF flattens their indentation. A: **Incorrect:** Its predicate has the intended meaning, but its while bound uses the wrong index/comparison and is incomplete as printed. B: **Incorrect:** It selects odd values and supplies an invalid/mismatched bound. C: **Correct:** Its intended even-and-greater-than-3 condition and j<len(data[i]) bound are correct; element must be renamed val in the shown for-loop. D: **Incorrect:** It selects the wrong side of 3 and uses the reversed loop bound.

Report a correction for Q6

Question 7 MCQ · 4.0 marks

Assume s is a str variable. What is the type of the expression "".join(sorted(set(s.lower()))) ?
6406535107721.
int
6406535107722.
set
6406535107723.
list
6406535107724.
bool
6406535107725.
str
6406535107726.
NoneType
6406535107727.
The code raises an error.

Published solution

**1. Track intermediate types:** s.lower() is a string; set(...) produces a set of characters; sorted(...) returns a list of those string characters. **2. Apply the outer operation:** The separator "".join(...) combines the characters into a string. The final type is str, even when s is empty. **Answer: E — str.** A: **Incorrect:** No conversion to an integer occurs. B: **Incorrect:** Set is an intermediate type, not the result after join. C: **Incorrect:** Sorted produces an intermediate list, which join consumes. D: **Incorrect:** No Boolean-producing comparison is the outer operation. E: **Correct:** The string method join returns a string. F: **Incorrect:** Join returns a string rather than None. G: **Incorrect:** For a str input, these operations compose validly.

Report a correction for Q7

Question 8 MSQ · 4.0 marks

Consider the following Python code: t = (5, [10, 20], (30, [40, 50],(30,))) Which of the following statements are **valid**? (Select all that apply)
6406535107728.
t[1].append(25)
6406535107729.
t[2] += (60,)
6406535107730.
t[2][0] = 35
6406535107731.
t[2][2].append(60)
6406535107732.
t[2][1][0] = 45

Published solution

**1. Locate the mutable objects:** t itself and t[2] are tuples, but t[1] and t[2][1] are lists. The object t[2][2] is the one-element tuple (30,). **2. Check changes versus rebinding:** Appending 25 to t[1] and changing t[2][1][0] to 45 mutate contained lists and are allowed. Assigning into a tuple is forbidden, including the tuple-item assignment attempted by t[2]+=..., and a tuple has no append method. **Answer: A, E — Appending to the contained list and changing the nested list element.** A: **Correct:** It mutates the list contained at t[1], which is allowed. B: **Incorrect:** It constructs a concatenated tuple but then tries to assign it into immutable t[2], raising TypeError. C: **Incorrect:** It directly tries to assign into a tuple, raising TypeError. D: **Incorrect:** t[2][2] is a tuple and has no append method, raising AttributeError. E: **Correct:** It changes an item of the nested list, which is mutable.

Report a correction for Q8

Question 9 MSQ · 3.0 marks

Consider the below code. if x: print('x') else: if y: if z: print('z') print('y') else: print('none') Select the possible output(s) of the given code, assuming any possible values for x , y , and z .
6406535107733.
x
6406535107734.
z y
6406535107735.
x y
6406535107736.
none
6406535107737.
x z
6406535107738.
y

Published solution

**1. Trace the outer choice:** If x is true, only “x” is printed. If x is false, execution enters the else branch; no x line can then appear. **2. Trace the nested choices:** With y false the output is “none”. With y true and z false it is “y”; with both y and z true it is “z” then “y”. Thus the four possible outputs are x; z/y; none; y. **Answer: A, B, D, F — x; z then y; none; or y.** **Convention note:** The intended indentation places the y test within the outer else, the z test within the y branch, and print(y) after the z test but still inside the y branch. The final else belongs to if y. A: **Correct:** It occurs whenever x is true. B: **Correct:** It occurs for x false, y true and z true. C: **Incorrect:** The outer branches are exclusive, so x cannot be followed by y. D: **Correct:** It occurs for x false and y false. E: **Incorrect:** The x branch cannot also execute the nested z branch. F: **Correct:** It occurs for x false, y true and z false.

Report a correction for Q9

Question 10 NAT · 4.0 marks

Consider the following Python code: for i in range(1, 6): for j in range(1, 6): for k in range(1, 2): if i == j: print("-") else: print("*", end=" ") print("+") How many lines will be printed when this code is executed?

Published solution

**1. Count newline-producing symbol calls:** There are 5×5×1=25 (i,j,k) iterations. Exactly five have i=j, and their print("-") calls each end a line. The other 20 calls use end=" " and do not add newline characters. **2. Count the final markers:** print("+") executes once per outer i iteration, adding another five newline-terminated lines. Stars may share a line with a dash or plus, but do not increase this line count. **3. Add the contributions:** Five dash newlines plus five plus newlines give 10 printed lines. **Answer: 10.** **Convention note:** This follows the intended nesting: k inside j inside i, and print("+") after the j loop within the i loop. The PDF removes visible indentation; moving the plus print to another nesting level changes the result.

Report a correction for Q10

Question 11 NAT · 3.0 marks

Consider the following Python code: for i in range(1, 5): for j in range(1, 5): if j == 3: break if i == 2: continue print(i, j) How many lines will be printed when this code is executed?

Published solution

**1. Apply the inner break:** For each i in 1,2,3,4, the j loop stops at j=3 before printing that value. Only j=1 and 2 can be printed. **2. Apply the continue:** When i=2, continue skips print(i,j) for both allowed j values. The other i values 1,3,4 each print two lines. **3. Count:** 3×2=6 lines. **Answer: 6.** **Convention note:** Read both break/continue tests and print(i,j) inside the inner j loop; the PDF flattens their block indentation.

Report a correction for Q11

Question 12 NAT · 3.0 marks

Consider the following Python code: P = {1, 2, 3, 4} Q = {3, 4, 5, 6} R = {4, 5, 6, 7} S = {2, 4, 6, 8} result = ((P | Q) - R) & S print(result) How many elements will be present in result?

Published solution

**1. Take the union and difference:** P|Q={1,2,3,4,5,6}. Removing R={4,5,6,7} leaves {1,2,3}. **2. Intersect with S:** Only 2 belongs to both {1,2,3} and S={2,4,6,8}, so result={2} has one element. **Answer: 1.**

Report a correction for Q12

Question 13 MCQ · 4.0 marks

Consider the following snippet of code and answer the given sub-questions: def analyze(data): result = [] for item in data: if isinstance(item, tuple): total = 0 for x in item: if x % 2 == 0: total += x result.append(total) elif isinstance(item, int) and item % 2 == 1: result.append(item * 3) else: continue return result sample = [(2, 5, 6), 7, (1, 3), 4, (8, 2, 1)] output = analyze(sample)
What is the value of output after executing the given code?
6406535107742.
[8, 21, 0, 4, 10]
6406535107743.
[8, 21, 4, 2, 10]
6406535107744.
[8, 21, 0, 10]
6406535107745.
[8, 7, 0, 10]

Published solution

**1. Process tuple items:** (2,5,6) contributes 2+6=8; (1,3) contributes 0; (8,2,1) contributes 8+2=10. Each tuple contributes its even-number sum once. **2. Process non-tuples:** Odd integer 7 contributes 7×3=21, while even integer 4 is skipped. In input order the result is [8,21,0,10]. **Answer: C — [8, 21, 0, 10].** **Convention note:** Use the intended block indentation: append(total) occurs after the inner x loop, once for each tuple; the elif belongs to the item-type test. A: **Incorrect:** Even integer 4 is skipped rather than appended. B: **Incorrect:** It includes values unsupported by the tuple sums and integer test. C: **Correct:** It preserves the four contributions in their original order. D: **Incorrect:** The odd integer 7 is tripled, not left unchanged.

Report a correction for Q13

Question 14 NAT · 4.0 marks

Consider the following snippet of code and answer the given sub-questions: def analyze(data): result = [] for item in data: if isinstance(item, tuple): total = 0 for x in item: if x % 2 == 0: total += x result.append(total) elif isinstance(item, int) and item % 2 == 1: result.append(item * 3) else: continue return result sample = [(2, 5, 6), 7, (1, 3), 4, (8, 2, 1)] output = analyze(sample)
How many times is the inner for x in item: loop executed in total when running analyze(sample) ?

Published solution

**1. Identify the tuples:** The inner loop runs only for (2,5,6), (1,3), and (8,2,1); the standalone integers do not enter it. **2. Count its body iterations:** Their lengths are 3,2,3, so the body executes 3+2+3=8 times. **Answer: 8.** **Convention note:** The loop is entered on three separate occasions and its body iterates eight times in total. The source wording “times the loop is executed” intends the latter count.

Report a correction for Q14

Question 15 MCQ · 4.0 marks

Consider the following code: def flipper(matrix): transformed = [] for row in matrix: new_row = [] for element in row: if element < 0: new_row.append(0) elif element % 2 == 0: new_row.append(element * 2) else: new_row.append(element * 3) transformed.append(new_row) result = [ val for sub in transformed for val in sub if val > 5 ] return transformed, result matrix = [[1, -2, 3], [4, 5, -6], [7, 8]] out1, out2 = flipper(matrix) print(out1) print(out2) Answer the subquestions based on the given data.
What is the value of out1 after executing the code?
6406535107747.
[[1, -4, 9], [8, 15, -12], [21, 16]]
6406535107748.
[[3, 0, 9], [8, 15, 0], [21, 16]]
6406535107749.
[[3, 0, 9], [4, 15, 0], [21, 16]]
6406535107750.
[[1, 0, 3], [8, 15, 0], [21, 16]]

Published solution

**1. Apply the branches in order:** Negative values -2 and -6 become 0 before any parity test. Positive odd values 1,3,5,7 are tripled, and positive even values 4,8 are doubled. **2. Preserve row structure:** The transformed rows are [3,0,9], [8,15,0] and [21,16], giving [[3,0,9],[8,15,0],[21,16]]. **Answer: B — [[3, 0, 9], [8, 15, 0], [21, 16]].** **Convention note:** Read new_row inside the row loop, and append each completed new_row to transformed once after the element loop; the PDF flattens indentation. A: **Incorrect:** The negative branch replaces negatives by zero and 1 should become 3. B: **Correct:** It applies all three branches correctly in each original row. C: **Incorrect:** The even positive value 4 must become 8. D: **Incorrect:** Positive odd values 1 and 3 must be tripled.

Report a correction for Q15

Question 16 MCQ · 4.0 marks

Consider the following code: def flipper(matrix): transformed = [] for row in matrix: new_row = [] for element in row: if element < 0: new_row.append(0) elif element % 2 == 0: new_row.append(element * 2) else: new_row.append(element * 3) transformed.append(new_row) result = [ val for sub in transformed for val in sub if val > 5 ] return transformed, result matrix = [[1, -2, 3], [4, 5, -6], [7, 8]] out1, out2 = flipper(matrix) print(out1) print(out2) Answer the subquestions based on the given data.
What is the value of out2 after executing the code?
6406535107751.
[3, 9, 8, 15, 21, 16]
6406535107752.
[9, 8, 15, 21, 16]
6406535107753.
[8, 15, 21, 16]
6406535107754.
[3, 0, 9, 8, 15, 0, 21, 16]       **Sem1 CT** **Section Id :** 640653105905 **Section Number :** 2 **Section type :** Online **Mandatory or Optional :** Mandatory **Number of Questions :** 14 **Number of Questions to be attempted :** 14 **Section Marks :** 50 **Display Number Panel :** Yes **Section Negative Marks :** 0 **Group All Questions :** No **Enable Mark as Answered Mark for Review and** No **Clear Response :** **Section Maximum Duration :** 0 **Section Minimum Duration :** 0 **Section Time In :** Minutes **Maximum Instruction Time :** 0

Published solution

**1. Apply the branches in order:** Negative values -2 and -6 become 0 before any parity test. Positive odd values 1,3,5,7 are tripled, and positive even values 4,8 are doubled. **2. Preserve row structure:** The transformed rows are [3,0,9], [8,15,0] and [21,16], giving [[3,0,9],[8,15,0],[21,16]]. **3. Flatten and filter:** Reading rows from left to right gives 3,0,9,8,15,0,21,16. Keeping only values strictly greater than 5 yields [9,8,15,21,16]. **Answer: B — [9, 8, 15, 21, 16].** A: **Incorrect:** 3 does not pass the >5 test. B: **Correct:** It retains exactly the qualifying values in row-major order. C: **Incorrect:** 9 also passes and must not be omitted. D: **Incorrect:** It includes values at most 5 that the comprehension filters out.

Report a correction for Q16