The Joy of Computing using Python Week 5 NPTEL Assignment Answers 2025

Need help with this week’s assignment? Get detailed and trusted solutions for The Joy of Computing using Python Week 5 NPTEL Assignment Answers. Our expert-curated answers help you solve your assignments faster while deepening your conceptual clarity.

✅ Subject: The Joy of Computing using Python
📅 Week: 5
🎯 Session: NPTEL 2025 July-October
🔗 Course Link: Click Here
🔍 Reliability: Verified and expert-reviewed answers
📌 Trusted By: 5000+ Students

For complete and in-depth solutions to all weekly assignments, check out 👉 NPTEL The Joy of Computing using Python Week 5 NPTEL Assignment Answers

🚀 Stay ahead in your NPTEL journey with fresh, updated solutions every week!

NPTEL The Joy of Computing using Python Week 5 Assignment Answers 2025

1.

Which of the following options correctly represents the possible outputs of the function defined above ?

  • 0,1
  • even, odd
  • 5,6
  • 2,1
Answer : See Answers

2. Consider:

What is the result of:

  • 4 7 10
  • 4 6 8
  • 3 6 9
  • 1 2 3
Answer :

3. Observe the following:

What type of mathematical function does mystery_function represent?

  • One-to-one, since different inputs give different outputs
  • Onto, because all values in the range are covered
  • Many-to-one, since multiple inputs map to the same output
  • Bijective, because the function is both one-to-one and onto
Answer :

4. A student defines the following function to simulate a formula from math class:

They then execute:

What will be the output?

  • 9
  • 21
  • 11
  • Error: positional argument follows keyword argument
Answer :

5. In the PyCalc system, two functions are defined:

A student composes them like this:

What is the output of composed(4)?

  • 11
  • 14
  • 7
  • 24
Answer :

6. The PyCalc team loaded a NumPy array and wrote the following code:

A student is asked to print the shape of the array and reshape it into 4 rows and 1 column. They write:

What will happen when this code is executed?

  • It prints (2, 2) and (4, 1)
  • It raises an error on line 1 because reshape cannot be used this way
  • It raises an error because .shape is not a function and cannot be called with ()
  • It raises an error because reshaping changes the original array
Answer :

7.

What will be printed?

  • 2 6
  • 1 6
  • 4 14
  • 9 6
Answer : See Answers

8.

Later, they write the following code:

What will happen when this code is run?

  • Function called
    Function called
    Just a value
  • <function identity at …>
    Function called
    Error: ‘str’ object is not callable
  • <function identity at …>
    <function identity at …>
    Just a value
  • Function called
    Error
    Error
Answer :

9. A student is trying to understand the difference between variables and functions in Python. They write the following:

What will happen when this code runs?

  • This is a function
  • This is a variable
  • Error: ‘str’ object is not callable
  • None
Answer :

10. A student defines the following function to collect error logs during program execution:

They run the following code:

What will be the output?

  • [‘E1’]
    [‘E2’]
    [‘E3’]
  • [‘E1’]
    [‘E1’, ‘E2’]
    [‘E3’]
  • [‘E1’]
    [‘E1’]
    [‘E3’]
  • [‘E1’]
    [‘E2’]
    [‘E3’, ‘E1’]
Answer :

In computer science, searching algorithms are fundamental techniques used to locate specific data within a collection, such as an array or list.

Linear Search is the simplest searching algorithm. It works by checking each element one by one, starting from the beginning of the list, until the desired
element is found or the end of the list is reached. It does not require the list to be sorted.

Here is a Python implementation of linear search:

In contrast, Binary Search is a more efficient algorithm, but it only works on sorted arrays. It repeatedly divides the search interval in half. If the target value is less than the element in the middle, the search continues in the left half. Otherwise, it continues in the right half. This continues until the target is found or the interval is empty.

Here is a Python implementation of binary search:

11. What is the time complexity of the worst-case scenario for linear search?

  • O(1)
  • O(log n)
  • O(n)
  • O(n log n)
Answer : See Answers

12. Which condition must be met for binary search to function correctly?

  • The array must have unique elements
  • The array must be unsorted
  • The array must be sorted
  • The array size must be even
Answer :

13. What will be the output of binary_search([1, 3, 5, 7, 9], 7)?

  • 2
  • 3
  • 4
  • 1
Answer :

14. In the binary search algorithm, what happens if arr[mid] > target?

  • Search left half of the array
  • Search right half of the array
  • Return mid
  • Return -1
Answer :

15. In the following code, what value is returned?

  • 2
  • 3
  • -1
  • Error
Answer :

16. For a sorted list of 1,000,000 elements, which search algorithm is generally faster for finding a value in terms of average-case performance?

  • Linear Search
  • Binary Search
  • Both are equally fast
  • Performance depends mainly on the programming language
Answer : See Answers

17. Using the binary_search implementation above, what is the maximum number of times the comparison arr[mid] == target can be executed when searching a sorted list of 256 elements?

  • 256
  • 128
  • 8
  • 9
Answer :

18. Which statement is FALSE?

  • Linear search does not need sorted data
  • Binary search needs sorted data
  • Binary search always performs better than linear search
  • Linear search checks every element until it finds the match
Answer :

19.

What will be the output of the following code?

  • 0
  • -1
  • 1
  • None
Answer :

20. If the element you are searching for is known to be at the end of a 100-element list and you use linear search, how many comparisons will it take?

  • 100
  • 50
  • 25
  • 10
Answer : See Answers

The Monty Hall Problem is a famous probability puzzle based on a game show scenario. It presents a counterintuitive result that surprises many people when
they first encounter it.

The Problem Setup:

You are a contestant on a game show. There are three doors:
Behind one door is a car (the prize).
Behind the other two doors are goats.
The game proceeds as follows:

  1. You choose one of the three doors.
  2. The host (Monty Hall), who knows what’s behind the doors, opens one of the other two doors to reveal a goat.
  3. You are then given a choice: stick with your original choice, or switch to the remaining unopened door.

Should You Switch or Stay?

Surprisingly, switching gives you a higher chance of winning the car.
If you stick: your chance of winning = 1/3
If you switch: your chance of winning = 2/3
This result can be verified through simulation using lists in Python.

You can simulate this many times to check the probability of winning by
switching vs. staying:

21. In the Monty Hall simulation, why does switching give a higher probability of winning the car, even though there are only two doors left at the end?

  • The host reveals the car
  • The initial choice had a 1/3 chance of being correct, so switching captures the remaining 2/3
  • The remaining door is chosen randomly
  • All options become equally likely after Monty’s reveal
Answer :

22. In the following Python code, what does the line marked with # ensure ?

  • It guarantees the host never reveals the car
  • It helps the contestant win more often
  • It randomly opens a car door
  • It changes the position of the car
Answer :

23. What would happen if the list available_doors included doors that might have the car behind them?

  • The switch strategy would always win
  • The simulation would stop with an error
  • The probability advantage of switching would no longer hold
  • It would make the list empty
Answer :

24. What is the main purpose of random.shuffle(doors) in the simulation?

  • To select a goat door
  • To ensure fairness by randomly placing the car
  • To ensure Monty opens the correct door
  • To make the output deterministic
Answer :

25. Which of the following modifications would incorrectly simulate the Monty Hall problem?

  • Removing the use of random.shuffle() when placing the car behind one of the doors.
  • Allowing Monty to randomly open any unchosen door, even if it hides the car.
  • Always forcing the player to switch their initial choice after Monty opens a door.
  • Running the simulation 10,000 times to observe overall win rates.
Answer : See Answers

26. What does this snippet do in the simulation?

  • Selects the door with the car
  • Randomly opens a door
  • Switches the player’s choice to the remaining unopened door
  • Forces the player to lose
Answer :

27. Why is running the simulation with thousands of trials (e.g., 10,000) important?

  • To stress test the program
  • To ensure the code doesn’t crash
  • To approximate the actual win probabilities using empirical evidence
  • Because smaller numbers always produce 100% win rates
Answer :

28. You pick one of the three doors at random. What is the probability that your initial choice is wrong?

  • 1/3
  • 1/2
  • 2/3
  • Cannot be determined
Answer :

29. You pick Door 1. Monty, knowing the prize location, opens Door 3, which has a goat. What is the probability the car is behind Door 2 if you initially picked a goat?

  • 1/3
  • 1/2
  • 1
  • 2/3
Answer :

30. What kind of probability concept does the Monty Hall problem mainly help us understand?

  • How to calculate the total number of possible events
  • How to identify equally likely outcomes
  • How probability changes when new information is revealed
  • How to simulate random experiments using coins and dice
Answer : See Answers

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.