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 code snippet.
What will be the output?
6406537406538.
1 1
6406537406539.
2 2
6406537406540.
1 2
6406537406541.
Error
Published solution
The default list coins=[] is created once when __init__ is defined, not separately for each call. Both Wallet() calls therefore assign the same list to self.coins. w1.add(5) and w2.add(10) mutate that shared list to [5,10]. Both lengths are 2, so the output is `2 2`. A usual way to avoid this sharing is a None default followed by creating a new list inside the method.
**Answer: B.**
A: **Incorrect:** Would require independent lists, which the mutable default does not create.
B: **Correct:** Both attributes refer to the same two-element list.
C: **Incorrect:** Both references see both appends, so their lengths cannot differ here.
D: **Incorrect:** Sharing a mutable default is legal Python; it does not itself raise an exception.
Hari runs a yoga class and grades students into levels based on a flexibility score out of 100.
Which of the following snippets correctly compute and print the level after accepting the score as
input? Assume the input entered will always be an integer in the range **[0,100]**.
**Snippet-1**
**Snippet-2**
**Snippet-3**
6406537406542.
Snippet-1 and Snippet-2 are correct, Snippet-3 is incorrect.
6406537406543.
Only Snippet-1 is correct.
6406537406544.
Snippet-1 and Snippet-3 are correct, Snippet-2 is incorrect.
6406537406545.
All three snippets are correct.
Published solution
The intervals are [0,50), [50,75), [75,90), and [90,100]. Snippet 1 uses an if/elif chain, so later tests apply only after earlier ones fail. Snippet 2 spells out the same intervals for integer inputs as 0–49, 50–74 and 75–89. Snippet 3 uses separate if statements: for a score such as 20 it first assigns Beginner, then Intermediate, then Advanced, overwriting the correct level. Its final else belongs only to the last if.
**Answer: A.**
A: **Correct:** Both first snippets implement the intervals; the third overwrites lower levels.
B: **Incorrect:** Snippet 2 is also correct because inputs are integers in the specified range.
C: **Incorrect:** Snippet 3 fails for scores below 75, while snippet 2 is correct.
D: **Incorrect:** Snippet 3's independent tests cause incorrect overwriting.
Dictionary keys use equality and hashing. True, 1 and 1.0 are equal numeric keys, so their assignments update the same entry, ending with value C. False and 0 likewise share an entry, ending with E. Thus there are two entries, d[1] is C, and d[0.0] is E because 0.0 equals zero. The output is `2 C E`.
**Answer: C.**
A: **Incorrect:** Equal numeric keys do not create five entries, and the zero lookup succeeds.
B: **Incorrect:** Only two distinct numeric-equivalence keys remain.
C: **Correct:** Correctly gives the two entries and their last assigned values.
D: **Incorrect:** Later equal-key assignments replace A with C and D with E.
The daily sales limits are 5,6,7,... because day starts at zero. The first ten iterations sell 5 through 14 items, whose sum is (5+14)×10/2=95. Five items remain. On the eleventh iteration min(5,15)=5 sells the rest. Stock becomes zero and the loop stops with day=11. Every one of the 100 items earns 2 revenue units, so revenue=200. Output: `11 0 200`.
**Answer: A.**
A: **Correct:** Matches eleven iterations, zero remaining stock and revenue 200.
B: **Incorrect:** Stops after only 95 items, before stock is exhausted.
C: **Incorrect:** Revenue cannot exceed 100 times 2 because min prevents overselling.
D: **Incorrect:** Ten iterations leave 5 items and revenue 190, rather than the displayed values.
Which of the following statements correctly explains why the printed value is obtained?
6406537406554.
The slice operation (
) creates a new outer list, but both lists still
refer to the same dictionary objects. Since
also refers to the same nested list, both
operations affect
.
6406537406555.
The slice operation creates new dictionary objects but shares the nested lists.
Therefore, only
changes
.
6406537406556.
Assigning
creates a copy of the list.
Therefore, only
changes
.
6406537406557.
Both
and
create independent copies,
so neither
operation modifies
.
Published solution
students[:] copies only the outer list. Its elements remain references to the same dictionaries, including their nested marks lists. report is another reference to students[0]['marks'], not a copy. The two appends add 100 and 95 to the original [80,90], giving [80,90,100,95], whose length is 4. Both mutations are visible through students.
**Answer: A.**
A: **Correct:** Correctly describes the shallow outer copy and the shared nested list.
B: **Incorrect:** The slice does not create new dictionaries; both appends affect the original nested marks.
C: **Incorrect:** Assigning report shares the list rather than copying it.
D: **Incorrect:** Neither operation deep-copies the nested objects.
A set removes duplicate equal elements: {(1,2),(1,2),3} contains only (1,2) and 3, and adding 3 changes nothing, so len(s)=2. t has three elements, its third being a mutable list. t[2].append(5) mutates that list without replacing a tuple slot, so it is legal and len(t) remains 3. Output is `2 3`.
**Answer: A.**
A: **Correct:** Correctly accounts for set uniqueness and mutation inside a tuple.
B: **Incorrect:** The repeated tuple is stored only once in the set.
C: **Incorrect:** Appending inside the third element does not remove a tuple element.
D: **Incorrect:** Mutating a contained list is allowed even though the containing tuple is immutable.
The requirement is tuples with price>80, ordered by descending price. The qualifying tuples are Jalebi:150, Ladoo:120 and Peda:90 in that order. A sorts by the price field descending and filters; B filters then performs the same price sort. C omits the price key, so tuples sort primarily by name. D returns names rather than tuples, uses >=, and does not sort.
**Answer: A, B.**
A: **Correct:** Sorting by x[1] descending then filtering satisfies both requirements.
B: **Correct:** Filtering then sorting by price descending is equivalent here.
C: **Incorrect:** Default tuple sorting uses the name first, not the required price.
D: **Incorrect:** Wrong output type, non-strict filter and missing descending price sort.
reads **n characters** from the current file position.
•
moves the file pointer to character position
from the beginning
of the file.
6406537406566.
The first line of the output is
.
6406537406567.
The second line of the output is
.
6406537406568.
The third line of the output is
.
6406537406569.
There are **4** lines in the output.
6406537406570.
The output contains empty lines.
Published solution
Each entry has six characters: Entry1, Entry2, etc., separated by newline characters. The initial readline skips Entry1. read(6) prints Entry2 but leaves its newline. readline consumes that newline, so the next read(6) prints Entry3. seek(0) resets the position and read(6) prints Entry1. The next readline consumes its newline, then the final read prints Entry2. Thus the four output lines are Entry2, Entry3, Entry1, Entry2, with no blank lines.
**Answer: A, B, C, D.**
A: **Correct:** The first readline positions the read at Entry2.
B: **Correct:** After consuming Entry2's newline, the next read starts at Entry3.
C: **Correct:** seek(0) returns the pointer to Entry1.
D: **Correct:** Each read produces six non-newline characters and print supplies one newline, giving four lines.
E: **Incorrect:** The pending input newlines are consumed by readline rather than printed.
You are reviewing a connection-manager script for a load balancer.
Which of the following statements are true at the end of the script?
6406537406571.
evaluates to
6406537406572.
evaluates to
6406537406573.
evaluates to
6406537406574.
evaluates to
Published solution
Initially both instances find the same class-level list. Connecting Alice through node1 and Bob through node2 mutates it to ['Alice','Bob']. reset on node1 assigns a new instance attribute ['Charlie']; it does not replace the class list. node2 still accesses the class list and appends Dave, making it ['Alice','Bob','Dave']. node1 retains its separate ['Charlie'].
**Answer: B, C.**
A: **Incorrect:** Dave is appended to the class list, not node1's replacement instance list.
B: **Correct:** node1's reset creates its separate one-element list.
C: **Correct:** node2 still sees the shared class list including all three connections.
D: **Incorrect:** The class list retains Alice; reset did not remove her from it.
The outer index i selects a row and the inner index j selects an entry. For the first row, i=0, the formula is 7-3j: j=0 gives 7, j=1 gives 4, and j=2 gives 1. matrix[0] is [7,4,1], whose sum is 12. The other rows are constructed but not included in the printed sum.
**Answer: 12.**
For fixed i, j runs from 1 through i and k runs from j through i, giving i-j+1 increments. Summing over j gives i+(i-1)+...+1=i(i+1)/2. For i=1,2,3,4 these totals are 1,3,6,10. The full count is 1+3+6+10=20. Remember that Python range excludes its stop, so i+1 permits k=i.
**Answer: 20.**
In order to protect secrecy, OTPs are often encrypted across the network. Caesar cipher is one
such encryption technique.
Consider the following code.
What is the output of the above code?
Published solution
The function processes each digit independently, adds shift=3 and reduces modulo 10. For '9742', the transformed digits are (9+3)%10=2, (7+3)%10=0, (4+3)%10=7, and (2+3)%10=5. Concatenating their string forms gives '2075', which print displays as 2075. There is no carry between positions because each digit is reduced separately.
**Answer: 2075.**
(exactly **12** characters, with no
newline).
Consider the following Python script executed on that file.
How many characters does the file contain after complete execution?
Published solution
r+ opens the existing 12-character file without truncating it. Writing '123' overwrites positions 0–2. seek(5) moves to position 5, where 'ABC' overwrites positions 5–7. Neither write reaches past the original end, so the file still has 12 characters. Opening in append mode then writes '99' at the end, adding two characters. Final length=12+2=14.
**Answer: 14.**
The first if adds every even number to sums[0]. The second if is separate: multiples of 3 go into sums[1], while its else subtracts nonmultiples of 3 from sums[0]. Thus sums[0]=(2+4+6)-(1+2+4+5)=12-12=0; sums[1]=3+6=9. The final sum is 9. The else belongs to the divisibility-by-3 test, so even values 2 and 4 are both added and subtracted.
**Answer: 9.**
The file gives 101:82, 102:AB, 103:91 and 104:75. Converting AB to int raises ValueError, so roll 102 is skipped before any assignment. The resulting dictionary contains integer marks for 101,103,104. The comprehension keeps integer marks at least 80, selecting [101,103]. Roll 104 fails the threshold and roll 102 is absent.
**Answer: B.**
A: **Incorrect:** Includes the absent roll 102 and below-threshold roll 104.
B: **Correct:** Both 82 and 91 meet the threshold, giving 101 and 103.
C: **Incorrect:** Incorrectly excludes roll 101, whose mark is 82.
D: **Incorrect:** 102 has nonnumeric marks and is skipped.
contains one student's marks per line in the format:
Example:
The following program processes the file.
**Note:**
returns
if
is an integer; otherwise, it returns
.
Based on the above data, answer the given subquestions
Which **one** of the following changes causes Roll No. **102** to be included in
?
6406537406584.
Replace
with
6406537406585.
Replace
with
6406537406586.6406537406587.
Replace
with
Published solution
Replacing int(marks) by marks stores the marks as raw strings. Then AB no longer causes conversion to fail, so result includes key 102 with value 'AB'. This question asks for inclusion in result, not qualified: the later isinstance(...,int) test would reject all string-valued marks. Changing continue to pass does not complete the failed assignment, because execution resumes after the except handler rather than retrying it.
**Answer: B.**
A: **Incorrect:** Pass does not retry the assignment that already failed.
B: **Correct:** Keeping the raw string allows result[102]='AB' to be assigned.
C: **Incorrect:** Removing exception handling does not make AB numeric; an uncaught conversion error stops execution.
D: **Incorrect:** strip returns one string, not the two fields required by roll,marks unpacking.
A university stores information about students as a list of dictionaries. A student is allotted an
elective only if:
• the student's CGPA is at least **8**, and
• the requested course has remaining seats.
Based on the above data, answer the given subquestions
Which **ONE** of the following statements **must** be true after execution?
6406537406588.
Every student having CGPA ≥ 8 receives a course.
6406537406589.
Every course appearing in
has at least one unfilled seat **and** no
further eligible student requesting that course appears after the allocation is complete.
6406537406590.
The number of keys in
is always equal to the number of keys in
.
6406537406591.
Every course requested by at least one eligible student has zero remaining
capacity.
Published solution
The code allocates AI to eligible students 101 and 103, Python to 104, and no course to ineligible 102 or to 105 after AI fills. Remaining capacities are AI:0, DBMS:1, Python:1, so remaining=['DBMS','Python']. In general, a course left with a seat did not run out while any eligible requester was processed: all such listed requesters would already have been served. The wording about 'no further eligible student' concerns the completed input list; the code cannot make promises about new students arriving later.
**Answer: B.**
A: **Incorrect:** Eligible 105 cannot receive AI because its seats are full.
B: **Correct:** The remaining list contains positive capacities after all listed students have been processed.
C: **Incorrect:** Allotted keys are student IDs and capacity keys are courses; their counts need not agree in general.
D: **Incorrect:** Python has an eligible requester but still has one seat left.
A university stores information about students as a list of dictionaries. A student is allotted an
elective only if:
• the student's CGPA is at least **8**, and
• the requested course has remaining seats.
Based on the above data, answer the given subquestions
Suppose the order of students in the list is changed arbitrarily, while keeping every student's
information unchanged.
Which **ONE** of the following statements is correct?
6406537406592.
The final value of
may differ between different orderings,
since it depends on which eligible students happen to be processed before the seats run out.
6406537406593.
The set of allotted students is guaranteed to remain unchanged.
6406537406594.
The number of allotted students is guaranteed to remain unchanged, but the
students receiving a particular course may change.
6406537406595.
The program raises a
if the order is modified.
Published solution
For each course, the number allocated is min(initial seats, number of eligible requesters). Reordering does not change either quantity, so it does not change the number allocated or the final capacities. It can change which eligible requesters are served first. Here three eligible students request two AI seats; any two can receive them depending on order. The one eligible Python requester also receives a seat, making three allocations overall.
**Answer: C.**
A: **Incorrect:** AI always has three eligible requesters and two seats, so its final capacity is always zero.
B: **Incorrect:** The identities of the two AI recipients can change.
C: **Correct:** The total is invariant, but the recipients can vary.
D: **Incorrect:** Reordering does not remove any course keys from capacity.