Software Development Exam  >  Software Development Questions  >  class Student: def __init__(self, name, roll... Start Learning for Free
class Student:
    def __init__(self, name, roll_number):
        self.name = name
        self.roll_number = roll_number
students = [Student('John', 1), Student('Emma', 2), Student('Mike', 3)]
student_dict = {student.roll_number: student for student in students}
print(student_dict[2].name)
What will be the output of the above code?
  • a)
    'John'
  • b)
    'Emma'
  • c)
    'Mike'
  • d)
    2
Correct answer is option 'B'. Can you explain this answer?
Verified Answer
class Student: def __init__(self, name, roll_number): self.name = ...
The code creates instances of the Student class and stores them in a dictionary student_dict with the 'roll_number' as the key. The code accesses the student with a roll number of 2 (student_dict[2]) and prints its name, which is 'Emma'.
View all questions of this test
Most Upvoted Answer
class Student: def __init__(self, name, roll_number): self.name = ...
Output:
The output of the above code will be "Emma".

Explanation:
1. First, we define a class called "Student" with two attributes: name and roll_number. The class has an __init__ method that initializes these attributes.
2. Then, we create a list called "students" and populate it with three instances of the Student class, each with a different name and roll number.
3. Next, we create a dictionary called "student_dict" using a dictionary comprehension. The key of each item in the dictionary is the roll number of the student, and the value is the corresponding student instance.
4. Finally, we print the name of the student with a roll number of 2 from the "student_dict" dictionary. Since the value associated with the key 2 is the student instance with the name "Emma", the output will be "Emma".

Key Points:
- The code defines a Student class with two attributes: name and roll_number.
- The code creates a list of Student instances and populates it with three students.
- The code creates a dictionary using a dictionary comprehension, where the roll number is the key and the student instance is the value.
- The code prints the name of the student with a roll number of 2 from the dictionary.
Explore Courses for Software Development exam

Top Courses for Software Development

class Student: def __init__(self, name, roll_number): self.name = name self.roll_number = roll_numberstudents = [Student(John, 1), Student(Emma, 2), Student(Mike, 3)]student_dict = {student.roll_number: student for student in students}print(student_dict[2].name)What will be the output of the above code?a)Johnb)Emmac)Miked)2Correct answer is option 'B'. Can you explain this answer?
Question Description
class Student: def __init__(self, name, roll_number): self.name = name self.roll_number = roll_numberstudents = [Student(John, 1), Student(Emma, 2), Student(Mike, 3)]student_dict = {student.roll_number: student for student in students}print(student_dict[2].name)What will be the output of the above code?a)Johnb)Emmac)Miked)2Correct answer is option 'B'. Can you explain this answer? for Software Development 2025 is part of Software Development preparation. The Question and answers have been prepared according to the Software Development exam syllabus. Information about class Student: def __init__(self, name, roll_number): self.name = name self.roll_number = roll_numberstudents = [Student(John, 1), Student(Emma, 2), Student(Mike, 3)]student_dict = {student.roll_number: student for student in students}print(student_dict[2].name)What will be the output of the above code?a)Johnb)Emmac)Miked)2Correct answer is option 'B'. Can you explain this answer? covers all topics & solutions for Software Development 2025 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for class Student: def __init__(self, name, roll_number): self.name = name self.roll_number = roll_numberstudents = [Student(John, 1), Student(Emma, 2), Student(Mike, 3)]student_dict = {student.roll_number: student for student in students}print(student_dict[2].name)What will be the output of the above code?a)Johnb)Emmac)Miked)2Correct answer is option 'B'. Can you explain this answer?.
Solutions for class Student: def __init__(self, name, roll_number): self.name = name self.roll_number = roll_numberstudents = [Student(John, 1), Student(Emma, 2), Student(Mike, 3)]student_dict = {student.roll_number: student for student in students}print(student_dict[2].name)What will be the output of the above code?a)Johnb)Emmac)Miked)2Correct answer is option 'B'. Can you explain this answer? in English & in Hindi are available as part of our courses for Software Development. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.
Here you can find the meaning of class Student: def __init__(self, name, roll_number): self.name = name self.roll_number = roll_numberstudents = [Student(John, 1), Student(Emma, 2), Student(Mike, 3)]student_dict = {student.roll_number: student for student in students}print(student_dict[2].name)What will be the output of the above code?a)Johnb)Emmac)Miked)2Correct answer is option 'B'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of class Student: def __init__(self, name, roll_number): self.name = name self.roll_number = roll_numberstudents = [Student(John, 1), Student(Emma, 2), Student(Mike, 3)]student_dict = {student.roll_number: student for student in students}print(student_dict[2].name)What will be the output of the above code?a)Johnb)Emmac)Miked)2Correct answer is option 'B'. Can you explain this answer?, a detailed solution for class Student: def __init__(self, name, roll_number): self.name = name self.roll_number = roll_numberstudents = [Student(John, 1), Student(Emma, 2), Student(Mike, 3)]student_dict = {student.roll_number: student for student in students}print(student_dict[2].name)What will be the output of the above code?a)Johnb)Emmac)Miked)2Correct answer is option 'B'. Can you explain this answer? has been provided alongside types of class Student: def __init__(self, name, roll_number): self.name = name self.roll_number = roll_numberstudents = [Student(John, 1), Student(Emma, 2), Student(Mike, 3)]student_dict = {student.roll_number: student for student in students}print(student_dict[2].name)What will be the output of the above code?a)Johnb)Emmac)Miked)2Correct answer is option 'B'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice class Student: def __init__(self, name, roll_number): self.name = name self.roll_number = roll_numberstudents = [Student(John, 1), Student(Emma, 2), Student(Mike, 3)]student_dict = {student.roll_number: student for student in students}print(student_dict[2].name)What will be the output of the above code?a)Johnb)Emmac)Miked)2Correct answer is option 'B'. Can you explain this answer? tests, examples and also practice Software Development tests.
Explore Courses for Software Development exam

Top Courses for Software Development

Explore Courses
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev