Registry Synced

Jan 2026 - Python - Week 10 - Graded Assignment 10

1634 words
8 min read

Week 10 - Graded Assignment 10

Course: Jan 2026 - Python
Week 10 - Graded Assignment 10
Last Submitted: You have last submitted on: 2026-04-22, 19:10 IST

Introduction

This assignment will be evaluated after the deadline passes. You will get your score 48 hrs after the deadline. Until then the score will be shown as Zero.
Common Data for questions 1 to 7 in this assignment
Execute the code-snippet given below and answer all the questions that follow.
python
class Student:
    count = 0
    def __init__(self, name, roll, maths, physics, chemistry):
        Student.count += 1
        self.roll = roll
        self.name = name
        self.maths = maths
        self.physics = physics
        self.chemistry = chemistry

class Group:
    def __init__(self):
        self.members = [ ]

    def add(self, student):
        self.members.append(student)

    def print_members(self):
        for student in self.members:
            print(student.name)


Question 1

We wish to create an object named student of type Student. Select all the correct ways of doing this. [MSQ]
  • ```python student = Student()
  • ```python student = Student('Anish')
  • ```python student = Student('Anish', 4, 90, 95, 100)
  • ```python student = Student(self, 'Anish', 4, 90, 95, 100)
Status: Yes, the answer is correct. Score: Score: 1
Feedback/Explanation:
python
student = Student('Anish', 4, 90, 95, 100)
Accepted Answers:
python
student = Student('Anish', 4, 90, 95, 100)

Question 2

scores.csv is a CSV file with the following content:
python
roll,name,maths,physics,chemistry
1,aditya,90,85,78
2,aishwarya,70,89,99
3,anish,90,85,100
4,deeptha,100,95,85
5,lakshmi,95,90,100
What is the output of the following snippet of code?
python
Student.count = 0
f = open('scores.csv', 'r')
f.readline()    # ignore the header
students = [ ]
for line in f:
    roll, name, maths, physics, chemistry = line.strip().split(',')
    roll, maths, physics, chemistry = int(roll), int(maths), int(physics), int(chemistry)
    students.append(Student(name, roll, maths, physics, chemistry))
f.close()
print(Student.count)
Your Answer: (Not answered)
Status: Yes, the answer is correct. Score: Score: 2
Feedback: count is a class attribute. The value of count is initialized to 0 in the beginning. The value of count is incremented by 1 every time an object is created. Since the __init__ method is called during object creation, count represents of the number of objects of type Student.
Accepted Answers:
(Type: Numeric) 5

Question 3

Write a method for the Student class that returns the sum of the marks scored by the student in all three subjects.
  • ```python def total(): return maths + physics + chemistry
  • ```python def total(): return self.maths + self.physics + self.chemistry
  • ```python def total(self): return maths + physics + chemistry
  • ```python def total(self): return self.maths + self.physics + self.chemistry
Status: Yes, the answer is correct. Score: Score: 1
Feedback: A method of a class requires self as first parameter. Every attribute of object should accessed using self. For example, the physics marks of the object is accessed by self.physics.
Accepted Answers:
python
def total(self):
    return self.maths +  self.physics + self.chemistry

Question 4

What is the output of the following snippet of code?
python
study_group = Group()
study_group.add(Student('Lathika', 1, 100, 90, 80))
study_group.add(Student('Keerthana', 2, 80, 70, 60))
study_group.add(Student('Sourabh', 3, 100, 50, 60))
study_group.print_members()
  • ```python Lathika Sourabh Keerthana
  • ```python Sourabh Keerthana Lathika
  • ```python Keerthana Lathika Sourabh
  • ```python Lathika Keerthana Sourabh
Status: Yes, the answer is correct. Score: Score: 2
Feedback/Explanation:
python
Lathika
Keerthana
Sourabh
Accepted Answers:
python
Lathika
Keerthana
Sourabh

Question 5

Is the following statement true or false?
print(Group.members) will print the value of the attribute members.
  • True
  • False
Status: Yes, the answer is correct. Score: Score: 2
Feedback: The list members is an object attribute not a class attribute.
Accepted Answers:
False

Question 6

Write a method named remove for the class group that accepts the roll number of a student as argument. It should perform the following function:
  • If the student is not there in the group, it should print the string "Student not found"
  • If the student is found in the group, it should remove the student from the group.
  • ```python def remove(self, roll): if roll in self.members: self.members.remove(roll) else: print('Student not found')
  • ```python def remove(self, roll): found = False for student in self.members: if student.roll == roll: found = True break if found: self.members.remove(roll) else: print('Student not found')
  • ```python def remove(self, roll): found = False for student in self.members: if student.roll == roll: found = True break if found: self.members.remove(student) else: print('Student not found')
  • ```python def remove(self, roll): found = False for student in self.members: if student.roll == roll: found = True if found: self.members.remove(student) else: print('Student not found')
Status: Yes, the answer is correct. Score: Score: 3
Feedback: Option (c) is correct. Option-(d) is very close. But it is missing the break statement. So, even after the student to be removed is found, the for loop will continue iterating. When it comes out, the variable student may or may not point to the student to be removed.
Options (a) and (b) are wrong. Each of them misses the fact that members is a list of objects of type Student and not merely a list of roll numbers.
Accepted Answers:
python
def remove(self, roll):
    found = False
    for student in self.members:
        if student.roll == roll:
            found = True
            break
    if found:
        self.members.remove(student)
    else:
        print('Student not found')

Question 7

Consider the following method that is added to the class Group:
python
# Assume that the random library has been imported and is available
def pick_leader(self):
    potential = [ ]
    for student in self.members:
        avg = (student.maths +
               student.physics +
               student.chemistry) // 3
        if avg >= 80:
            potential.append(student)
    if len(potential) > 0:
        index = random.randint(0, len(potential) - 1)
        self.leader = potential[index].name
    else:
        self.leader = None
Consider the following study group.
python
study_group = Group()
study_group.add(Student('Lathika', 1, 80, 80, 80))
study_group.add(Student('Keerthana', 2, 80, 70, 60))
study_group.add(Student('Sourabh', 3, 75, 85, 80))
study_group.add(Student('Nikhil', 4, 100, 79, 59))
study_group.pick_leader()
print(study_group.leader)
Based on your understanding of the method pick_leader, which of the following students could become a leader of this group?
  • ```python Lathika
  • ```python Keerthana
  • ```python Sourabh
  • ```python Nikhil
Status: Yes, the answer is correct. Score: Score: 4
Feedback: The criterion for being considered a leader should be clear: the student should have an average score of at least 80 across the three subjects. Only two students satisfy this.
Accepted Answers:
python
Lathika
python
Sourabh

Question 8

What is the output of the following code-snippet?
python
D = {'Anita': 23, 'Ashwin': 43,
     'Ahana': '24',
     'Adarsh': 30, 'Archana': 15}
try:
    # iterates through the keys from left to right
    for key in D:
        value = D[key]
        if type(value) == str:
            raise 'Error'
        print(f'{key}:{value}')
except:
    print("Values cannot be strings")
  • ```python Anita:23 Ashwin:43 Ahana:24 Adarsh:30 Archna:15
  • ```python Anita:23 Ashwin:43
  • ```python Values cannot be strings
  • ```python Anita:23 Ashwin:43
  • ```python Anita:23 Ashwin:43 Values cannot be strings
Status: Yes, the answer is correct. Score: Score: 2
Feedback: We are raising an error if the type of any one of the values of the dictionary is a string. The program terminates gracefully by raising an exception that is caught by the except block. This exception occurs after the first string-value is encountered.
Accepted Answers:
python
Anita:23
Ashwin:43
Values cannot be strings

Question 9

What is the output of the following snippet of code?
python
L = [1, 3, -1, 4, -2, 5, 3]

try:
    n = 10
    for i in range(n):
        if L[i] < 0:
            L[i] = 0
except IndexError:
    for i in range(n - len(L)):
        L.append(0)
finally:
    print(L)
  • ```python [1, 3, -1, 4, -2, 5, 3]
  • ```python [1, 3, 0, 4, 0, 5, 3]
  • ```python [1, 3, 0, 4, 0, 5, 3, 0, 0, 0]
  • This code doesn't print anything to the console.
Status: Yes, the answer is correct. Score: Score: 2
Feedback: IndexError will be raised. Then the except block gets into action. finally block is always executed at the end.
Accepted Answers:
python
[1, 3, 0, 4, 0, 5, 3, 0, 0, 0]

Question 10

What is the output of the following snippet of code?
python
try:
    L = [x for x in range(10)]
    f = open('numbers.txt', 'w')
    for x in L:
        f.write(x)
except FileNotFoundError:
    print('File was not found')
except:
    print('This is some other error')
finally:
    print('The file has been closed')
    f.close()
  • ```python File was not found
  • ```python File was not found The file has been closed
  • ```python This is some other error
  • ```python This is some other error The file has been closed
Status: Yes, the answer is correct. Score: Score: 1
Feedback: Opening a file in write mode creates a new file. So, we can never get the FileNotFoundError when writing to files. It could only occur when files are opened in read mode. The finally block is always executed. Here, it has an important purpose. The file that was opened in the try block is closed in the finally block.
Accepted Answers:
python
This is some other error
The file has been closed

Document Outline
Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.