Trace the recursion from its base case. calc(3,0) returns (3,0,1). The next level uses x=3,y=0,z=1 and returns (4,3,2). The final level uses x=4,y=3,z=2 and returns (5,7,4). Unpacking gives p=5,q=7,r=4, so print(q,r) outputs `7 4`. Each return expression uses the values received from the smaller call.
**Answer: B.**
A: **Incorrect:** Does not accumulate y across both recursive levels.
B: **Correct:** Matches q=7 and r=4 after the two recursive returns.
C: **Incorrect:** Incorrectly changes both the accumulation and doubling count.
D: **Incorrect:** Does not match the traced q and r values.
The loop processes 'python' from left to right. It appends p, y and t. At h, continue skips the append for that iteration. At o, break exits the entire loop before appending o, so n is never visited. The final string is `pyt`.
**Answer: B.**
A: **Incorrect:** Includes h despite continue and o despite break.
B: **Correct:** Contains only p, y and t, which reach the append.
C: **Incorrect:** Treats break as if it only skipped o, and also mishandles h.
D: **Incorrect:** n cannot be appended because the loop stops at o.
The loop removes the last digit using n%10, then removes that digit using n//10. Its successive (digit,p,contribution) triples are (4,1,4), (3,2,6), and (2,3,6). The total is 4+6+6=16. Position weights increase from the units digit towards the left, rather than applying to digits left-to-right.
**Answer: D.**
A: **Incorrect:** 20 is not the sum of the three traced contributions.
B: **Incorrect:** 14 omits part of the weighted sum.
C: **Incorrect:** 24 does not match the digit weights 1,2,3.
D: **Correct:** 16 equals 4 times 1 plus 3 times 2 plus 2 times 3.
a.copy() creates a separate list b. Sorting b changes only b to [1,1,3,4,5]. Then a[2]=9 replaces the third element of a, giving [3,1,9,1,5]. Thus the output is `[3, 1, 9, 1, 5] [1, 1, 3, 4, 5]`. A shallow copy suffices because the elements are immutable integers.
**Answer: B.**
A: **Incorrect:** Leaves a unchanged even though a[2] is assigned 9.
B: **Correct:** Correctly keeps the original-order a and separately sorted b.
C: **Incorrect:** Treats the two lists as if sorting and replacement were shared.
D: **Incorrect:** Treats the copy as an alias and also loses b's sorting.
The branches form one if/elif chain, so each character can contribute only once. In 'abracadabra', a occurs five times and contributes 1 each; b occurs twice and contributes 2 each; c occurs once and contributes 3; r and d contribute zero. Total=5+4+3=12. The a characters do not also enter the later membership test in 'abc', because their first branch already succeeded.
**Answer: A, B, D.**
A: **Correct:** The total is 12.
B: **Correct:** a is a vowel and contributes 1 via the first branch.
C: **Incorrect:** Although r contributes no increment, the stated alternative does not describe the actual b/c branch increments; b adds 2, not 3.
D: **Correct:** b adds 2 and c adds 3 through their respective first matching branches.
The first call process(5) uses default y=3. Since 5>3, x becomes 5-3=2; then y=2*2=4, returning (2,4). The second call process(2,4) explicitly supplies y=4; since 2>4 is false, x becomes 2+4=6 and y becomes 12. The prints are `2 4` and `6 12`. A comma-separated return expression packages its two items as a tuple for unpacking.
**Answer: A, D, E.**
A: **Correct:** The first call returns (2,4).
B: **Incorrect:** The second call returns (6,12), not (5,10).
C: **Incorrect:** The second call passes 4 explicitly, so it does not use the default 3.
D: **Correct:** return x,y creates a two-item tuple.
E: **Correct:** The second print outputs 6 and 12.
What is the output of the following snippet of code?
Published solution
Evaluate the three terms separately. data[0] is 'Courses', whose length is 7. data[1][1] is '2026', and converting and summing its digits gives 2+0+2+6=10. data[2] is 'quiz', whose length is 4. Therefore the printed total is 7+10+4=21. Indexing into the nested list is different from taking its length.
**Answer: 21.**
Consider the following snippet of code. What will the output be?
Published solution
For each word, only the first matching branch runs. pen has length 3 and is appended to small; book has length 4 and does not begin with p, so result gains 3; pencil gains 2; eraser gains 3; ink is appended to small. Thus small=['pen','ink'] has length 2 and result=8. The print is 2+8=10. pen does not also add 2 because it already matched the short-word branch.
**Answer: 10.**
What is the output of the following snippet of code?
**Note:**
:checks whether an object belongs to a given type or any of
its subclasses.
Published solution
Check each independent if statement. a=True is a bool and true, so add 3. b=10 is an int and truthy, so add 4. c=False is a bool and not c is true, so add 5. d=0 is an int, not a bool, so the bool test adds nothing; its int test and not d both succeed, adding 2. Total=3+4+5+2=14. Although bool subclasses int in Python, an ordinary integer zero is not an instance of bool.
**Answer: 14.**
What is the output of the following snippet of code?
Published solution
The list comprehension doubles even integers and adds 1 to odd integers. For [1,2,1,3,3,4,5], it produces [2,4,2,4,4,8,6]. Summing gives 2+4+2+4+4+8+6=30. The conditional expression is evaluated separately for every element; it does not filter out odd values.
**Answer: 30.**
For each pair, test the second component. If it is even, double the first component; otherwise add 1 to the first. (2,3) gives 3; (4,6) gives 8; (5,1) gives 6; (3,8) gives 6. The result is `[3,8,6,6]`. The loop increments i after each append, visiting all four pairs exactly once.
**Answer: B.**
A: **Incorrect:** Does not apply the stated even/odd rule to the second components.
B: **Correct:** Matches all four traced values in order.
C: **Incorrect:** Incorrectly transforms the first and third pairs.
D: **Incorrect:** The second pair's even second component requires 4 times 2=8, not 6.
Changing the condition to item[1]%2==1 swaps which parity uses the doubling branch. (2,3) now gives 4; (4,6) gives 5; (5,1) gives 10; (3,8) gives 4. Hence output becomes `[4,5,10,4]`. The branches' arithmetic stays unchanged; only the parity test changes.
**Answer: A.**
A: **Correct:** Matches the four outputs after reversing the parity condition.
B: **Incorrect:** This is the original output before the condition changes.
C: **Incorrect:** Keeps some original-branch results instead of consistently reversing parity.
D: **Incorrect:** Does not consistently apply double-for-odd and plus-one-for-even.
For text='CoMPuter', indices 1 through 4 give A='oMPu', because slice stop 5 is excluded. The final four characters give B='uter'. Strings compare lexicographically and case-sensitively; at the first character, 'o' is less than 'u', so A>B is false. The else branch returns B,A. Therefore P='uter' and Q='oMPu'.
**Answer: B.**
A: **Incorrect:** Returns A,B even though the condition is false.
B: **Correct:** Correctly returns B,A with the two actual slices.
C: **Incorrect:** Uses an incorrect slice and the wrong returned values.
D: **Incorrect:** The last slice is correct, but Q should be oMPu, not MPut.
Consider the following snippet of code and answer the sub-questions:
Which of the following statements are true?
6406536343764.
Both
and
are substrings of
6406536343765.
The expression
evaluates to
6406536343766.
The comparison ignores uppercase and lowercase letters
6406536343767.
The expression
evaluates to
Published solution
Using the function's local values, A='oMPu' and B='uter'. They are slices, hence substrings of text. A[1:] is 'MPu', while B[:-1] is 'ute'; these are unequal. Comparisons are case-sensitive and lexicographic, so text[1:5]>text[-4:] is false because 'o'<'u'. A and B are local to process; a literal attempt to evaluate their names outside the function without defining them would raise NameError. The exercise's comparison statements refer to their values inside the function.
**Answer: A, D.**
A: **Correct:** Both returned values originate from contiguous slices of text.
B: **Incorrect:** MPu and ute are different strings, so the equality is false.
C: **Incorrect:** Python string comparison does not ignore case.
D: **Correct:** oMPu is lexicographically less than uter, so the displayed greater-than expression is false.