Bird
0
0

You want to create a class Student with a field name and a method GetName that returns the student's name. Which is the correct complete class declaration?

hard🚀 Application Q15 of 15
C Sharp (C#) - Classes and Objects
You want to create a class Student with a field name and a method GetName that returns the student's name. Which is the correct complete class declaration?
Aclass Student { public string name; public void GetName() { return name; } }
Bclass Student { string name; string GetName() { name; } }
Cclass Student { public string name; string GetName() { return name; } }
Dclass Student { public string name; public string GetName() { return name; } }
Step-by-Step Solution
Solution:
  1. Step 1: Check field and method access modifiers

    The field name and method GetName should be public to be accessible outside the class.
  2. Step 2: Verify method return type and body

    GetName returns a string, so its return type must be string and it must return name.
  3. Step 3: Identify correct option

    class Student { public string name; public string GetName() { return name; } } correctly declares name as public string and GetName as public string method returning name.
  4. Final Answer:

    class Student { public string name; public string GetName() { return name; } } -> Option D
  5. Quick Check:

    Public field + public string method returning field [OK]
Quick Trick: Method return type must match returned value type [OK]
Common Mistakes:
MISTAKES
  • Missing return statement in method
  • Wrong method return type (void instead of string)
  • Missing public keyword for accessibility

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes