CBSE Class 11 Computer Science
Question 81 of 104
List Manipulation — Question 17
Back to all questions 17
Question Question 15
What will be the output of following code?
x = ['3', '2', '5']
y = ''
while x:
y = y + x[-1]
x = x[:len(x) - 1]
print(y)
print(x)
print(type(x), type(y))Answer
Output
523
[]
<class 'list'> <class 'str'>
Explanation
The loop while x will continue executing as long as the length of list x is greater than 0. y is initially an empty string. Inside the loop, we are adding the last element of x to y and after that we are removing the last element of x from x. So, at the end of the loop y becomes 523 and x becomes empty. Type of x and y are list and str respectively.