Bird
Raised Fist0

Which of the following constructor definitions will cause a syntax error in Python?

easy📝 Syntax Q3 of Q15
Python - Constructors and Object Initialization
Which of the following constructor definitions will cause a syntax error in Python?
Adef __init__(self, x, y=20):
Bdef __init__(self, x, y):
Cdef __init__(self, x=10, y=20):
Ddef __init__(self, x=10, y):
Step-by-Step Solution
Solution:
  1. Step 1: Recall parameter order rules

    In Python, parameters with default values must come after parameters without defaults.
  2. Step 2: Analyze each option

    def __init__(self, x=10, y): has a default parameter x=10 before y without default, causing syntax error. Others follow correct order.
  3. Final Answer:

    def __init__(self, x=10, y): causes syntax error -> Option D
  4. Quick Check:

    Default params must follow non-defaults [OK]
Quick Trick: Non-default parameters must come before default ones [OK]
Common Mistakes:
MISTAKES
  • Placing default parameters before non-default
  • Confusing default value syntax
  • Ignoring parameter order rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes