Bird
0
0

You need to track how many instances of a class have been created and assign each instance a unique sequential ID starting from 1. Which design correctly uses static and instance members in C#?

hard🚀 Application Q8 of 15
C Sharp (C#) - Classes and Objects
You need to track how many instances of a class have been created and assign each instance a unique sequential ID starting from 1. Which design correctly uses static and instance members in C#?
AUse only instance fields to count objects and assign IDs.
BUse an instance counter incremented in the constructor and a static field to store the unique ID.
CUse a static counter incremented in the constructor and an instance field to store the unique ID assigned from the counter.
DUse only static fields to assign IDs without instance fields.
Step-by-Step Solution
Solution:
  1. Step 1: Understand requirements

    Count total instances and assign unique IDs starting at 1.
  2. Step 2: Use static for shared data

    A static counter tracks total instances across all objects.
  3. Step 3: Use instance field for unique ID

    Each object stores its own unique ID assigned from the static counter.
  4. Final Answer:

    Use a static counter incremented in the constructor and an instance field to store the unique ID assigned from the counter. -> Option C
  5. Quick Check:

    Static for shared count, instance for unique ID [OK]
Quick Trick: Static tracks total; instance stores unique ID [OK]
Common Mistakes:
MISTAKES
  • Using instance fields to count all objects
  • Assigning IDs using static fields only without instance storage
  • Confusing static and instance roles

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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