Bird
0
0

Which of the following is a correct way to define a constructor with a default value for the parameter color set to 'red'?

easy📝 Conceptual Q2 of 15
Python - Constructors and Object Initialization
Which of the following is a correct way to define a constructor with a default value for the parameter color set to 'red'?
Adef __init__(self, color): if not color: color='red'
Bdef __init__(self, color): color='red'
Cdef __init__(self, color='red'):
Ddef __init__(self, color=None): color='red'
Step-by-Step Solution
Solution:
  1. Step 1: Understand default parameter syntax

    Default values are set directly in the parameter list using assignment, e.g., param=default.
  2. Step 2: Check each option

    def __init__(self, color='red'): correctly sets color='red' in the parameter list. Others either assign inside the function incorrectly or don't set default properly.
  3. Final Answer:

    def __init__(self, color='red'): -> Option C
  4. Quick Check:

    Default value syntax = param=default [OK]
Quick Trick: Set defaults in parameter list, not inside function body [OK]
Common Mistakes:
  • Assigning defaults inside constructor body
  • Using None without checks
  • Missing equals sign in parameter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes