Bird
0
0

Given this class, how can you add a method that returns the instance's name in uppercase?

hard📝 Application Q8 of 15
Python - Constructors and Object Initialization
Given this class, how can you add a method that returns the instance's name in uppercase?
class Person:
    def __init__(self, name):
        self.name = name
Adef get_upper(self): return name.upper()
Bdef get_upper(): return name.upper()
Cdef get_upper(self): return Person.name.upper()
Ddef get_upper(self): return self.name.upper()
Step-by-Step Solution
Solution:
  1. Step 1: Define method with self parameter

    The method must accept self to access instance variables.
  2. Step 2: Access instance variable and apply upper()

    Use self.name.upper() to return the uppercase string.
  3. Final Answer:

    def get_upper(self): return self.name.upper() -> Option D
  4. Quick Check:

    Access instance data with self and call string methods [OK]
Quick Trick: Use self.var to access instance data inside methods [OK]
Common Mistakes:
  • Omitting self parameter
  • Using variable name without self.
  • Trying to access class attribute instead of instance

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes