Bird
Raised Fist0

Identify the error in this class declaration:

medium📝 Debug Q14 of Q15
C Sharp (C#) - Classes and Objects
Identify the error in this class declaration:
class Book
{
  string title;
  void SetTitle(string t)
  {
    title = t;
  }
}
AMissing access modifiers for field and method
BClass name should be lowercase
CMethod SetTitle must return a value
DField title must be static
Step-by-Step Solution
Solution:
  1. Step 1: Check access modifiers in class members

    By default, class members are private, but it's good practice to specify access modifiers explicitly.
  2. Step 2: Identify missing access modifiers

    Field title and method SetTitle lack access modifiers like private or public, which can cause confusion or errors in some contexts.
  3. Final Answer:

    Missing access modifiers for field and method -> Option A
  4. Quick Check:

    Always specify access modifiers [OK]
Quick Trick: Always add public/private to fields and methods [OK]
Common Mistakes:
MISTAKES
  • Assuming lowercase class names are required
  • Thinking void methods must return a value
  • Believing fields must be static

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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