NPTEL The Joy of Computing using Python Week 5 Assignment Answers 2024
1. Imagine the Monty Hall problem had four doors instead of three, and Monty opened one door to reveal a goat after your initial choice, what would be the probability of winning the car by switching?
a. 1/2
b. 1/3
c. 1/4
d. 3/4
✅ Answer :- d
Explanation: With four doors and one revealed to be a goat, switching increases the probability of winning to 3/4 since the chance of initially picking the car is only 1/4.
2. What happens if you try to add a new key-value pair to a dictionary using a key that already exists in the dictionary?
a. The new value overwrites the existing value
b. An error is raised
c. The dictionary becomes immutable
d. The new value is appended to the existing value
✅ Answer :- a
Explanation: In Python, if a key already exists, assigning a new value to it will overwrite the existing value.
3. What would be the possible change in steps for running “Rock, Paper, Scissors, Lizard, Spock”?
a. Step 1 would involve assigning 5 values to choices instead of 3
b. The bitwise operations involved in Step 3 would involve modulo operation with 5 instead of 3
c. None of the above
d. All the above
✅ Answer :- d
Explanation: The algorithm would need updates to accommodate 5 options and modify logic like modulo operations and comparisons accordingly.
4. How does Bubble Sort compare elements in an array?
a. Compares adjacent elements and swaps if they are in the wrong order
b. Compares random elements in the array
c. Compares all elements with the first element
d. Compares elements in reverse order
✅ Answer :- a
Explanation: Bubble Sort repeatedly compares and swaps adjacent elements if they are in the wrong order.
5. To sort a list in ascending order, when does Bubble Sort exhibit poor performance?
a. When the list is sorted in ascending order
b. When the list is sorted in descending order
c. When the list contains unique elements
d. Bubble Sort always exhibits poor performance
✅ Answer :- b
Explanation: Bubble Sort is slowest on reverse-sorted lists because it has to make the maximum number of comparisons and swaps.
6. How many passes does Bubble Sort make through the array in the worst-case scenario for sorting n elements?
a. n
b. n-1
c. 2n
d. n²
✅ Answer :- b
Explanation: In the worst case, Bubble Sort makes (n-1) passes to completely sort an array of n elements.
7. How many pairs of adjacent elements are compared in a single pass of the Bubble Sort algorithm?
a. n
b. n-1
c. n/2
d. 2n
✅ Answer :- b
Explanation: Each pass compares n-1 adjacent pairs to check and perform swaps if necessary.
8. Which steps would require modification if the array were sorted in descending order in Binary Search?
a. Step 2
b. Step 4
c. Steps 2 and 4
d. No modification in steps
✅ Answer :- c
Explanation: For descending arrays, logic in midpoint comparisons and update of low/high pointers must be reversed in steps 2 and 4.
9. Select the correct statements among the following:
a. In the presence of duplicate elements in a list, binary search always finds the first occurrence of the element in the list
b. In the presence of duplicate elements in a list, binary search may or may not find the first occurrence of the element in the list
c. In the presence of duplicate elements in a list, the existing binary search algorithm with minor modifications can be used to find the first occurrence of the target element in the list
d. In the presence of duplicate elements in a list, the existing binary search can be used to find any kth (k can be any number from 1 to number of times the element has been repeated) of target element in the list
✅ Answer :- b, c, d
Explanation: Standard binary search may not find the first occurrence, but with minor changes, it can. It may also return any repeated instance, including the kth.
10. Swapping of two numbers is an intermediate step in any sorting algorithm. In the lecture we use a temporary variable to swap two elements in the list.
Can swapping of two numbers be done without using a temporary variable?
a. Yes
b. No
c. Depends on size of list
d. Depends on nature of elements in the list
✅ Answer :- a
Explanation: Swapping without a temporary variable can be done using arithmetic (e.g., a = a + b; b = a - b; a = a - b
) or XOR operations.