Search code examples
booleanboolean-logicboolean-expressionboolean-operations

Boolean logic Multiple choice ques


Q. Which of the following boolean expressions will evaluate to true if x is between lower and upper, inclusive, and false otherwise.

1: (x<=lower) || (x>=upper)

2: (x>=lower) || (x<=upper)

3: lower <= x <= upper

4: (x>=lower) && (x<=upper)

5: u(x<=lower) && (x>=upper)

What is the answer to this question and why? Can someone briefly explain?


Solution

  • The correct one is #4, why?

    1. You can discard 1 and 2 because you need them to be between those values, so it must be over or equal to lower AND below or equal upper.
    2. -#5 is contradictory to your statement, because it looks for values that are not in the range of lower and upper inclusive, regardless that, it might be impossible to hit, because its stating that upper should be less than lower.
    3. It can be #3 if you are giving a mathematical expression of it, but for programming syntax you will most likely find it as #4 format.

    And with the condition of being inclusive, that means that your limits are inside your range.