Bird
Raised Fist0

Find the error in this code snippet:

medium📝 Debug Q7 of Q15
Python - Constructors and Object Initialization
Find the error in this code snippet:
class Student:
    def __init__(self, name='Unknown', grade=10):
        self.name = name
        self.grade = grade

s = Student(grade=12)
ANo error; object is created with name='Unknown' and grade=12.
BCannot use keyword arguments in constructor calls.
CDefault values cannot be used with keyword arguments.
DMissing required positional argument 'name' when creating Student object.
Step-by-Step Solution
Solution:
  1. Step 1: Understand default values and keyword arguments

    Default values allow skipping arguments; keyword arguments can specify any parameter by name.
  2. Step 2: Analyze object creation

    s is created with grade=12 explicitly, name uses default 'Unknown'. This is valid syntax.
  3. Final Answer:

    No error; object is created with name='Unknown' and grade=12. -> Option A
  4. Quick Check:

    Keyword args + defaults = valid [OK]
Quick Trick: Use keyword args to skip defaults easily [OK]
Common Mistakes:
MISTAKES
  • Thinking positional args are mandatory
  • Believing keyword args cause errors
  • Confusing default values usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes