CBSE Class 12 Computer Science Question 24 of 42

Solved 2024 Sample Question Paper CBSE Class 12 Computer Science (083) — Question 6

Back to all questions
6
Question

Question 22

Predict the output of the following code:

S = "LOST"
L = [10, 21, 33, 4]
D = {}
for I in range(len(S)):
    if I % 2 == 0:
        D[L.pop()] = S[I]
    else:
        D[L.pop()] = I + 3
for K, V in D.items():
    print(K, V, sep = "*")
Answer
Output
4*L
33*4
21*S
10*6
Explanation

The code initializes variables S, L, and D, representing a string, list, and dictionary, respectively. The code iterates through the indices of string S. If the index is even, it assigns the popped element from list L as a key in dictionary D with the corresponding character from S as its value. If the index is odd, it assigns the popped element from L as a key in D with the value being the index plus 3. This process continues until the loop ends. After the loop, it prints each key-value pair from D, with keys and values separated by "*".