DSA Questions on Lists using Python
Question 1 :
What will be the output of the following code block?
a=[1,2,3,4,5,6,7,8,9]
a[::2]=10,20,30,40,50,60
print(a)
ValueError: attempt to assign sequence of size 6 to extended slice of size 5
Answer : Value Error: attempt to assign sequence of size 6 to extended slice of size 5
Explanation: The code block provided here contains a syntax error. The slice assignment a[::2] = 10, 20, 30, 40, 50, 60 should have the same number of elements on both sides, but it doesn't. The slice a[::2] selects every second element of the list a, which results in a list of 5 elements: [1, 3, 5, 7, 9]. On the right side of the assignment, there are 6 elements: 10, 20, 30, 40, 50, 60. Since the number of elements is not equal, Python will raise a ValueError when executing this code:
ValueError: attempt to assign sequence of size 6 to extended slice of size 5
To fix the error, we should ensure that the number of elements on both sides of the assignment is the same. For example, we can change the right side of the assignment to have only 5 elements:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
a[::2] = 10, 20, 30, 40, 50
print(a)
The output of the corrected code block will be:
[10, 2, 20, 4, 30, 6, 40, 8, 50]
************************************************************************************
Question 2 :
What will be the output of the following code block?
a=[1,2,3,4,5]
print(a[3:0:-1])
Answer : [4, 3, 2]
Explanation : The given code block performs a slice operation on the list a using the syntax a[start:stop:step]. In this case, start is 3, stop is 0, and step is -1. This slice operation creates a new list containing elements from the original list, starting at index 3, going until index 0 (not inclusive), and selecting every element with a step of -1 (moving in reverse).
So, the output of the code block will be:
[4, 3, 2]
The slice operation picks the elements at indices 3, 2, and 1 from the original list.
************************************************************************************
Question 3 :
What will be the output of the following code snippet?
def f(value, values):
v = 1
values[0] = 44
t = 3
v = [1, 2, 3]
f(t, v)
print(t, v[0])
Answer : 3 44
Explanation: In the given code snippet, the function f takes two arguments, value and values. Inside the function, it assigns 1 to a local variable v and sets the first element of the list values to 44. The function doesn't have a return statement, so it implicitly returns None.
Here's a step-by-step explanation of the code execution:
So, the output of the code snippet will be:
3 44
***********************************************************************************
Question 4 :
What will be the output of the following code block?
fruit_list1 = ['Apple', 'Berry', 'Cherry', 'Papaya']
fruit_list2 = fruit_list1
fruit_list3 = fruit_list1[:]
fruit_list2[0] = 'Guava'
fruit_list3[1] = 'Kiwi'
sum = 0
for ls in (fruit_list1, fruit_list2, fruit_list3):
if ls[0] == 'Guava':
sum += 1
if ls[1] == 'Kiwi':
sum += 20 print(sum)
print(sum)
Answer : 22
Explanation: Here's a step-by-step explanation of the code block:
After the modifications, the lists are as follows:
Now let's calculate the sum:
So, the output of the code block will be:
22
************************************************************************************
Recommended by LinkedIn
Question 5 :
What will be the output of the following code block?
arr = [[1, 2, 3, 4],
[4, 5, 6, 7],
[8, 9, 10, 11],
[12, 13, 14, 15]]
for i in range(0, 4):
print(arr[i].pop())
Answer : 4 7 11 15
Explanation: In the given code block, we have a 2D list called arr. The code iterates through the rows of the 2D list using the for loop with a range from 0 to 3 (4 not included). In each iteration, the pop() method is called on the current row (i.e., arr[i]), which removes and returns the last element of the row.
Here's the step-by-step execution of the code:
So, the output of the code block will be: 4 7 11 15
************************************************************************************
Question 6 :
What will be the output of the following code block?
a=[1,2,3,4,5,6,7,8,9]
print(a[::2])
Answer : [1,3,5,7,9]
Explanation: In the given code block, a slice operation is performed on the list a. The slice operation uses the syntax a[start:stop:step]. In this case, start is not specified (defaults to 0), stop is not specified (defaults to the end of the list), and the step is 2. The slice operation creates a new list containing elements from the original list, starting at the beginning, going until the end, and selecting every second element (using a step of 2).
So, the output of the code block will be:
[1, 3, 5, 7, 9]
The slice operation picks the elements at indices 0, 2, 4, 6, and 8 from the original list.
**********************************************************************************
Question 7 :
What is the correct command to shuffle the following list?
fruit=['apple', 'banana', 'papaya', 'cherry']
Answer : random.shuffle(fruit)
Explanation: To shuffle a list in Python, you can use the shuffle() function from the random module. Here's the correct command to shuffle the fruit list:
import random fruit = ['apple', 'banana', 'papaya', 'cherry']random.shuffle(fruit)
Now, the fruit list will be shuffled randomly. Note that the shuffle() function shuffles the list in place, meaning it modifies the original list rather than returning a new shuffled list.
*******************************************************************************
Question 8 :
What will be the output of the following code block?
data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
def fun(m): v = m[0][0]
for row in m:
for element in row:
if v < element: v = element
return vprint(fun(data[0]))
Answer : 4
Explanation: In the given code block, the function fun takes a 2D list (matrix) as an argument and iterates through its elements to find the largest value. The code block calls the function fun with the first element of the data list, which is [[1, 2], [3, 4]].
Here's a step-by-step explanation of the code execution:
So, the output of the code block will be:
4
*************************************************************************************
Question 9 :
What will be the output of the following code block?
arr = [1, 2, 3, 4, 5, 6]for i in range(1, 6):
arr[i - 1] = arr[i]for i in range(0, 6):
print(arr[i], end = " ")
Answer : 2 3 4 5 6 6
**********************
Explanation: The given code block first modifies the arr list using a for loop and then prints the elements of the modified list. Here's the step-by-step execution of the code:
So, the output of the code block will be:
2 3 4 5 6 6
############################################################