makautnotes.in Provides makaut Previous Year Question Papers & Notes, answer keys, organizer, syllabus

Find your queries what you want today🙂 but first join my telegram group

NPTEL The Joy of Computing using Python Week 2 Assignment Answers 2024 (July-October)

 Week 2: Python Programming Assignment 2 - Problem Analysis and Solutions

Description:
This document provides an analysis and solution for each question in Week 2, Assignment 2 of the "Joy of Computing using Python" course on SWAYAM. The assignment covers fundamental Python concepts, including variable assignment, loops, conditionals, and basic data types. Each question is presented with the correct answer and an explanation for why it is correct.


Question 1:

Statement: If a variable is assigned multiple times, the latest value is not stored in the variable.

  • A) False (The variable stores all values it was assigned)
  • B) False (The variable stores its value from the latest assignment)
  • C) True (The variable stores the value from the second last assignment)
  • D) True (The variable stores values from the initial assignment)

Correct Answer: B) False (The variable stores its value from the latest assignment)

Explanation:
In Python, when a variable is reassigned, its previous value is overwritten. The variable only retains the most recent value it has been assigned.


Question 2:

Which of the following code blocks print "Hello Ram Lakshman and Hanuman"?

  • Option A:
python
name1 = "Ram" name2 = "Lakshman" name3 = "Hanuman" print("Hello", name1, name2, "and", name3, "!")
  • Option B:
python
name1 = "Ram" name2 = "Lakshman" name3 = "Hanuman" print("Hello", name1, name2, "and", name3,"!")
  • Option C:
python
name1 = "Ram" name2 = "Lakshman" name3 = "Hanuman" print("Hello Ram Lakshman and Hanuman !")
  • Option D:
python
name1 = "Ram" name2 = "Lakshman" name3 = "Hanuman" print("Hello", name1, name2, "and", name3, "!")

Correct Answer: D)

python
name1 = "Ram" name2 = "Lakshman" name3 = "Hanuman" print("Hello", name1, name2, "and", name3, "!")

Explanation:
The correct answer correctly combines all the variables into a single print statement, and the output will be the desired string with correct formatting.


Question 3:

What are the correct ways to inform Python that input is an integer?

  • A) int(input())
  • B) float(input())
  • C) input()
  • D) str(input())
  • E) eval(input())

Correct Answer: A) int(input())

Explanation:
To convert user input into an integer in Python, you use int(input()). This ensures that the input is treated as an integer rather than as a string.


Question 4:

The following program outputs 722:

python
a = 7 result = 1 for i in range(a): if(i > 0): result = result * i print(result+2)

For what value of a does the code output 8?

  • A) 2
  • B) 1
  • C) 4
  • D) 0

Correct Answer: A) 2

Explanation:
When a = 2, the loop runs once (since range(2) is [0, 1]), result remains 1 as result * i will only occur when i > 0. Therefore, result remains 1, and adding 2 gives the output 3.


Question 5:

What does the previous question calculate?

  • A) Calculates the factorial of a
  • B) Calculates the factorial of a and adds 3
  • C) Calculates the multiples of all integers from 1 and adds 2
  • D) Calculates the factorial of 1 and adds 2

Correct Answer: D) Calculates the factorial of 1 and adds 2

Explanation:
Given the value a = 1, the code calculates 1! (which is 1) and adds 2 to it.


Question 6:

Which loop is used to perform a set of repetitive tasks based on a condition in Python?

  • A) while loop
  • B) for loop
  • C) do-while loop
  • D) if-else loop

Correct Answer: A) while loop

Explanation:
The while loop in Python continues executing a block of code as long as a specified condition is True.


Question 7:

What happens when the condition inside the if and while evaluates to False?

  • A) Python interpreter ignores the if/while block and halts the program.
  • B) Python interpreter ignores the if/while block and proceeds to the program from the line after the if/while block.
  • C) Python interpreter executes the if/while block and exits the program.
  • D) Python interpreter executes the if/while block and the program runs in an infinite loop.

Correct Answer: B) Python interpreter ignores the if/while block and proceeds to the program from the line after the if/while block.

Explanation:
When the condition evaluates to False, Python skips the code inside the if or while block and continues executing the subsequent code in the program.


Question 8:

The following program might or might not have an infinite loop. Does the program have an infinite loop?

python
a = int(input()) while(a == 0): if(a<0): a=-1 if(a>0): a=1 if(a>1): a=1 if(a<1): a=-2 print(a)
  • A) No, the program doesn't have an infinite loop.
  • B) Yes, it can be prevented by updating the value of a before the if block at line 3.
  • C) Yes, it can be prevented by setting both the if blocks inside the while loop.
  • D) Yes, but it cannot be prevented.

Correct Answer: B) Yes, it can be prevented by updating the value of a before the if block at line 3.

Explanation:
An infinite loop occurs because the condition in the while loop might never change, depending on the value of a. Updating a before entering the if blocks can prevent this issue.


Question 9:

For which of the following values of name and age variables does the following code print "You are lucky"?

python
name = input("Enter your name: ") age = int(input("Enter your age: ")) flag = "False" if(age >= 18): flag = "True" else: flag = "False" counter = 0 for i in name: if(i == "a"): counter += 1 if(flag == "True"): if(counter >= 2): print("You are lucky") else: print("You are not lucky")
  • A) Aryan, 20
  • B) Sajith, 19
  • C) Arya, 17
  • D) Akash, 16

Correct Answer: A) Aryan, 20

Explanation:
For the output "You are lucky", the name must contain two 'a's and the age must be 18 or older. "Aryan, 20" satisfies these conditions.


Question 10:

For which of the options among the previous question does the program not print anything?

  • A) Aryan, 20
  • B) Sajith, 19
  • C) Arya, 17
  • D) Akash, 17

Correct Answer: B) Sajith, 19

Explanation:
In the case of "Sajith, 19", the flag is set to "True" (since age is greater than or equal to 18), but there is no condition met inside the loop that would cause the program to print anything. Thus, no output is printed.

Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Telegram Group For Nptel Answer Keys Join Now

html 5

lagged02

 

gamelix