Evaluate the Boolean expression in C#: what is assigned to c? int a = 10; int b = 20; bool c; c = !(a > b);

Difficulty: Easy

Correct Answer: There is no error and c gets True.

Explanation:

Introduction / Context:This checks understanding of Boolean comparisons and logical negation in C# as well as proper typing of Boolean variables.

Given Data / Assumptions:

  • a = 10, b = 20.
  • Expression is !(a > b).
  • c is of type bool.

Concept / Approach:(a > b) evaluates to a Boolean. The operator ! negates a Boolean, flipping true to false and vice versa. C#'s bool is not an int; assigning 1 or 0 to bool is not the model—use true/false.

Step-by-Step Solution:

Compute a > b → 10 > 20 → false.Apply ! → !false → true.Assign to c → c == true.

Verification / Alternative check:Print c to the console; it outputs True.

Why Other Options Are Wrong:

  • B: ! operates on bool, not int; there's no error.
  • A: C# bools are not assigned 1/0; they hold true/false (though Convert.ToInt32(true) yields 1, that is a different API call).
  • E: Opposite of the actual result.

Common Pitfalls:Thinking of bool as an integer; in C#, bool is distinct and should be used with true/false.

Final Answer:There is no error and c gets True.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion