Boolean to integer via Convert in C#: what value is assigned? d = Convert.ToInt32(!(30 < 20));

C# Programming Operators Difficulty: Easy
Choose an option
Answer

Correct Answer: A value 1 will be assigned to d.

Explanation

Introduction / Context:This question ensures you know how Boolean expressions work in C# and how Convert.ToInt32 maps true/false to integers.

Given Data / Assumptions:

  • Expression: !(30 < 20).
  • Convert.ToInt32(bool) returns 1 for true, 0 for false.

Concept / Approach:Evaluate the comparison, apply logical NOT, then convert Boolean to int using the framework conversion.

Step-by-Step Solution:

30 < 20 → false.!false → true.Convert.ToInt32(true) → 1.

Verification / Alternative check:Replacing Convert.ToInt32 with a conditional operator yields the same: int d = (!(30 < 20)) ? 1 : 0;

Why Other Options Are Wrong:They contradict the true/false to 1/0 mapping or suggest syntax from other languages (Not is not C#).

Common Pitfalls:Assuming C-style implicit Boolean-to-integer conversions; C# requires explicit conversions (here via Convert).

Final Answer:A value 1 will be assigned to d.

Discussion & Comments
No comments yet. Be the first to comment!
More Questions from Operators
Join Discussion