Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
C Sharp (C#) - Properties and Encapsulation
Identify the error in this code snippet:
class Car {
  private string model;
  public string GetModel() {
    return model;
  }
}
class Program {
  static void Main() {
    Car c = new Car();
    Console.WriteLine(c.model);
  }
}
ACannot access private field 'model' directly outside class.
BGetModel method is missing a return statement.
CClass Car must be static to access fields.
DConsole.WriteLine cannot print strings.
Step-by-Step Solution
Solution:
  1. Step 1: Check field access in Main method

    Code tries to access private field 'model' directly.
  2. Step 2: Understand access restrictions

    Private fields cannot be accessed outside their class, causing error.
  3. Final Answer:

    Cannot access private field 'model' directly outside class. -> Option A
  4. Quick Check:

    Private field direct access = Error [OK]
Quick Trick: Private fields only accessible inside their class [OK]
Common Mistakes:
MISTAKES
  • Thinking GetModel lacks return
  • Believing Car must be static
  • Assuming Console.WriteLine can't print strings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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