Bird
0
0

You want to create a class Rectangle that stores width and height and calculates area. Which is the best way to do this?

hard🚀 Application Q8 of 15
C Sharp (C#) - Classes and Objects
You want to create a class Rectangle that stores width and height and calculates area. Which is the best way to do this?
ACreate two separate variables width and height outside any class
BCreate a class with width and height fields and a method that returns width * height
CUse only static methods without fields
DCreate a class with only a method that prints area without storing width and height
Step-by-Step Solution
Solution:
  1. Step 1: Understand class purpose

    Class should store data (width, height) and provide behavior (calculate area).
  2. Step 2: Evaluate options

    Only Create a class with width and height fields and a method that returns width * height stores data and calculates area properly; others miss data or proper design.
  3. Final Answer:

    Create a class with width and height fields and a method that returns width * height -> Option B
  4. Quick Check:

    Class design = Store data + behavior [OK]
Quick Trick: Classes hold data and methods for related tasks [OK]
Common Mistakes:
MISTAKES
  • Using variables outside classes
  • Ignoring data storage
  • Using only static methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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