DSA Questions on Lists using Python

DSA Questions on Lists using Python

Question 1 :

What will be the output of the following code block?

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 should have the same number of elements on both sides, but it doesn't. The slice selects every second element of the list , which results in a list of 5 elements: . On the right side of the assignment, there are 6 elements: . Since the number of elements is not equal, Python will raise a when executing this code:

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:

The output of the corrected code block will be:

************************************************************************************

Question 2 :

What will be the output of the following code block?

Answer : [4, 3, 2]

Explanation : The given code block performs a slice operation on the list using the syntax . In this case, is , is , and is . This slice operation creates a new list containing elements from the original list, starting at index , going until index (not inclusive), and selecting every element with a step of (moving in reverse).

So, the output of the code block will be:

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?

Answer : 3 44

Explanation: In the given code snippet, the function takes two arguments, and . Inside the function, it assigns to a local variable and sets the first element of the list to . The function doesn't have a return statement, so it implicitly returns .

Here's a step-by-step explanation of the code execution:

  1. The variable is assigned the value .

  2. The variable is assigned the list .

  3. The function is called with arguments and . The parameter takes the value of (3), and takes the value of ().

  4. Inside the function, the local variable is assigned the value . This does not affect the global variable .

  5. The first element of the list (which is the same object as the global variable ) is set to , so the list (and also the global variable ) becomes .

  6. The function ends without a return statement, implicitly returning .

  7. The function is called with arguments and , which are and , respectively.

So, the output of the code snippet will be:

***********************************************************************************

Question 4 :

What will be the output of the following code block?

Answer : 22

Explanation: Here's a step-by-step explanation of the code block:

  1. is defined with four fruit names.

  2. is assigned the reference of , so both lists now point to the same memory location. Any changes made to either of them will be reflected in the other.

  3. is assigned a slice of that includes all elements. This creates a new, shallow copy of the list, so changes made to won't affect or .

  4. The first element of is changed to 'Guava'. Since and refer to the same list, the first element of is also changed to 'Guava'.

  5. The second element of is changed to 'Kiwi'. This does not affect or .

  6. A variable is initialized with the value 0.

  7. A for loop iterates over a tuple containing , , and .If the first element of the current list is 'Guava', is incremented by 1.If the second element of the current list is 'Kiwi', is incremented by 20.

  8. The final value of is printed.

After the modifications, the lists are as follows:

  • : ['Guava', 'Berry', 'Cherry', 'Papaya']

  • : ['Guava', 'Berry', 'Cherry', 'Papaya'] (same as )

  • : ['Apple', 'Kiwi', 'Cherry', 'Papaya']

Now let's calculate the :

  • For : sum += 1 (first element is 'Guava') = 1

  • For : sum += 1 (first element is 'Guava') = 2

  • For : sum += 20 (second element is 'Kiwi') = 22

So, the output of the code block will be:

Question 5 :

What will be the output of the following code block?

Answer : 4 7 11 15

Explanation: In the given code block, we have a 2D list called . The code iterates through the rows of the 2D list using the loop with a range from 0 to 3 (4 not included). In each iteration, the method is called on the current row (i.e., ), which removes and returns the last element of the row.

Here's the step-by-step execution of the code:

  1. : The last element of the first row () is 4. The method removes and returns 4, and the first row becomes . The function outputs .

  2. : The last element of the second row () is 7. The method removes and returns 7, and the second row becomes . The function outputs .

  3. : The last element of the third row () is 11. The method removes and returns 11, and the third row becomes . The function outputs .

  4. : The last element of the fourth row () is 15. The method removes and returns 15, and the fourth row becomes . The function outputs .

So, the output of the code block will be:

Question 6 :

What will be the output of the following code block?

Answer : [1,3,5,7,9]

Explanation: In the given code block, a slice operation is performed on the list . The slice operation uses the syntax . In this case, is not specified (defaults to ), is not specified (defaults to the end of the list), and the is . 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 ).

So, the output of the code block will be:

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?

Answer : random.shuffle(fruit)

Explanation: To shuffle a list in Python, you can use the function from the module. Here's the correct command to shuffle the list:

Now, the list will be shuffled randomly. Note that the 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?

Answer : 4

Explanation: In the given code block, the function takes a 2D list (matrix) as an argument and iterates through its elements to find the largest value. The code block calls the function with the first element of the list, which is .

Here's a step-by-step explanation of the code execution:

  1. The function is called with the argument .

  2. The variable is assigned the value of the first element in the first row of the matrix, which is .

  3. The function iterates through each row of the matrix and each element in each row using nested loops.

  4. If the current element is greater than the current value of , is updated with the new larger value.

  5. After iterating through all the elements, holds the largest value in the matrix.

  6. The function returns the largest value found, which is .

So, the output of the code block will be:

*************************************************************************************

Question 9 :

What will be the output of the following code block?

Answer : 2 3 4 5 6 6

**********************

Explanation: The given code block first modifies the list using a for loop and then prints the elements of the modified list. Here's the step-by-step execution of the code:

  1. Iterate through the list using a for loop with a range from 1 to 5 (6 not included). For each , the value of is assigned to . This operation effectively shifts each element to the left by one position except for the first element. After the loop, becomes:

  2. Iterate through the modified list using another for loop with a range from 0 to 5 (6 not included). The function is called with the parameter, which prints each element followed by a space instead of a newline.

So, the output of the code block will be:

############################################################

To view or add a comment, sign in

Others also viewed

Explore topics