Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
C Sharp (C#) - Classes and Objects
What will be the output of this code?
class Box {
  public int width = 5;
  public int height = 10;
}

var b = new Box();
Console.WriteLine(b.width + b.height);
A15
B510
CError: Cannot access fields
D0
Step-by-Step Solution
Solution:
  1. Step 1: Understand field values

    The instance fields width and height are set to 5 and 10 respectively.
  2. Step 2: Calculate the sum

    Adding 5 + 10 results in 15, which is printed.
  3. Final Answer:

    15 -> Option A
  4. Quick Check:

    Sum of instance fields = 15 [OK]
Quick Trick: Instance fields hold values accessible via object [OK]
Common Mistakes:
MISTAKES
  • Concatenating numbers as strings
  • Assuming fields are inaccessible
  • Confusing default values with assigned values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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