Carefully study the following Python program. The program defines a function…

2026

Carefully study the following Python program. The program defines a function SORTED() that performs different operations depending on the data type of the argument passed to it - list, tuple, or dictionary.

L = [4,6,1,3,2,5]        # Line 01
T = (4,6,1,3,2,5)        # Line 02
D = {4:6,1:3,2:5}        # Line 03

def SORTED(X):           # Line 04
    if type(X) == list:  # Line 05
        X.sort()         # Line 06
    elif type(X) == tuple:   # Line 07
        Y = sorted(X)        # Line 08
        X = type(X)(Y)       # Line 09
    elif type(X) == dict:    # Line 10
        Y = sorted(X)        # Line 11
        X = X.fromkeys(Y, 0) # Line 12
    print(X)                 # Line 13

SORTED(L)   # Line 14
SORTED(T)   # Line 15
SORTED(D)   # Line 16

print(L)    # Line 17
print(T)    # Line 18
print(D)    # Line 19

Note: In case of any error or exception, specify that along with the type of error or exception.

Questions:

(A) What output will be displayed by the print statement inside the function (Line 13) when the function SORTED() is called with the tuple in Line 15?

(B) What output will be displayed by the print statement inside the function (Line 13) when the function SORTED() is called with the dictionary in Line 16?

(C) What output will be displayed by the print statement in Line 17?

(D) What output will be displayed by the print statement in Line 18 and Line 19?

Show answer & explanation

(A) Output for Line 15 (inside function):
(1, 2, 3, 4, 5, 6)
Reason: sorted(X) returns a sorted list, and type(X)(Y) converts it back into a tuple.

(B) Output for Line 16 (inside function):
{1: 0, 2: 0, 4: 0}
Reason: sorted(X) sorts dictionary keys as [1, 2, 4], and fromkeys() assigns value 0 to each key.

(C) Output for Line 17 (Original List L):
[1, 2, 3, 4, 5, 6]
Reason: .sort() modifies the original list in-place (mutable object).

(D) Output for Line 18 and 19:
Line 18: (4, 6, 1, 3, 2, 5)
Line 19: {4: 6, 1: 3, 2: 5}
Reason: Tuple is immutable and unchanged. Dictionary inside function creates a new object, so original D remains unaffected.

Explore the full course: Hpsc Pgt Computer Science