Bird
0
0

Which of the following is the correct way to check if obj is a bool and assign it to b?

easy📝 Syntax Q3 of 15
C Sharp (C#) - Polymorphism and Abstract Classes
Which of the following is the correct way to check if obj is a bool and assign it to b?
Aif (obj is bool)
Bif (obj as bool b)
Cif (obj == bool b)
Dif (obj is bool b)
Step-by-Step Solution
Solution:
  1. Step 1: Understand pattern matching syntax

    The correct syntax to check type and assign is if (obj is Type variable).
  2. Step 2: Analyze options

    if (obj is bool b) uses the correct pattern matching syntax for bool. if (obj as bool b) uses invalid syntax with as. if (obj == bool b) is syntactically incorrect. if (obj is bool) checks type but does not assign.
  3. Final Answer:

    if (obj is bool b) -> Option D
  4. Quick Check:

    Pattern matching with is and variable assignment [OK]
Quick Trick: Use 'is Type variable' for type check and assignment [OK]
Common Mistakes:
MISTAKES
  • Using 'as' keyword incorrectly for pattern matching
  • Checking type without variable assignment
  • Using equality operator for type check

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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