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 e...

 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.

COMMENTS

Name

1sem,1,1st Sem,33,1st year,2,2 sem,1,2nd Sem,29,2sem,1,3rd Sem,40,4th sem,9,5th sem,28,6th sem,19,7th sem,8,8th sem,6,About BEU,1,ABOUT MAKAUT,1,aku civil Notes,15,Aku EE/EC Notes,14,aku ME Notes,14,aku notes,45,aku papers,11,aku syllabus,6,All Branch,2,all semester,19,B pharm,1,BAU Question Papers,1,BCA Notes,1,BEU Collage,12,BEU Model Paper Question,3,BEU Notes,10,BEU Organizer,31,BEU Previous Year Questions,2,Beu pyq,4,BEU PYQ Ans,5,BEU syllabus,8,Blogs,1,Btech results,1,Civil Branch,2,Civil Engineering,8,CS Engineering,8,CSE Branch,1,CSE Notes,19,Developing Soft Skills And Personality,13,EC Engineering,10,EE Branch,2,EE Engineering,9,engineering chemistry,5,Gate,1,internship,3,Introduction To Internet Of Things,21,Introduction To Machine Learning,2,iot,1,MAKAUT CE Organizer,6,MAKAUT CSE Organizer,5,MAKAUT ECE Organizer,3,MAKAUT EE Organizer,2,MAKAUT ME Organizer,4,MAKAUT Notes,5,MAKAUT Organizer,8,MAKAUT Question Paper,1,MAKAUT Syllabus,1,make money,6,ME Engineering,19,NPTEL,92,NPTEL COURSE,91,Programming Tutorial,12,Public Speaking,22,PYQ Solution,4,Question Bank,19,Soft Skills,33,Traffic & SEO,9,week 1,7,week 10,3,week 11,3,week 12,3,week 2,10,week 3,6,week 4,7,week 5,5,week 6,4,week 7,4,week 8,4,week 9,3,WEEK1,4,WEEK10,3,WEEK11,3,WEEK12,3,WEEK2,5,WEEK3,6,WEEK4,6,WEEK5,5,WEEK6,3,WEEK7,4,WEEK9,1,ztest,6,
ltr
item
BEU BIHAR : BEU PYQ , Beu previous year question All Courses All Semester Solutions: NPTEL The Joy of Computing using Python Week 2 Assignment Answers 2024 (July-October)
NPTEL The Joy of Computing using Python Week 2 Assignment Answers 2024 (July-October)
BEU BIHAR : BEU PYQ , Beu previous year question All Courses All Semester Solutions
https://www.beubihar.org.in/2024/08/nptel-joy-of-computing-using-python.html
https://www.beubihar.org.in/
https://www.beubihar.org.in/
https://www.beubihar.org.in/2024/08/nptel-joy-of-computing-using-python.html
true
8161375692651428750
UTF-8
Loaded All Posts Not found any posts VIEW ALL Read More Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content
×