Teachoo Sample Paper - Computer Science Class 12

Computer Science - Class 12
Solutions to CBSE Sample Paper - Computer Science Class 12

## ```      print(K,V,sep="*") ```

The output of the following code is:

4*7

8*9

12*I

16*R

• The code given below defines a string S, a list L, and an empty dictionary D.
• Then it loops through the indices of S using a for loop.
• For each index I, it checks if it is even or odd using the modulo operator (%).
• If it is even, it pops the last element of L using the pop() method and assigns it as a key to the dictionary D, with the corresponding character of S as the value.
•  If it is odd, it pops the first element of L using the pop(0) method and assigns it as a key to the dictionary D, with the index I plus 6 as the value.
• After the loop, it prints the key-value pairs of D in sorted order using the sorted() function and a for loop, with an asterisk (*) as the separator.
• To predict the output of this code, we need to trace the values of S, L, D, and I in each iteration of the loop. We can use a table to do this:  Iteration I S[I] L D 0 0 R [4, 8, 12] {16: ‘R’} 1 1 A [8, 12] {16: ‘R’, 4: 7} 2 2 I  {16: ‘R’, 4: 7, 12: ‘I’} 3 3 N [] {16: ‘R’, 4: 7, 12: ‘I’, 8: 9}
• After the loop, D contains four key-value pairs: {16: ‘R’, 4: 7, 12: ‘I’, 8: 9} . The sorted() function returns a list of tuples containing the key-value pairs of D in sorted order by default. Therefore, the list returned by sorted(D.items()) is [(4, 7), (8, 9), (12, ‘I’), (16, ‘R’)]. Then we loop through this list using a for loop and print each tuple with an asterisk (*) as the separator using the sep parameter of the print() function. Therefore, the output of the code is:

4*7

8*9

12*I

16*R

Learn in your speed, with individual attention - Teachoo Maths 1-on-1 Class 