Question

In: Computer Science

Recall the is_hour(str) and is_minute(str) functions return True if the str contains valid hour and minute respectively


Recall the is_hour(str) and is_minute(str) functions return True if the str contains valid hour and minute respectively. That is, is_hour("03:65") would return True and is_minute("03:65") would return False.

For each of the following expression, indicate whether short-circuit evaluation would occur. That is, the function call on the LHS of the logic operator would not actually be evaluated.

Expression

Short-circuit evaluation

Expression

Short-circuit evaluation

is_hour("03:65") and is_minute("03:65")

--YesNo

is_hour("03:65") or is_minute("03:65"

--YesNo

is_minute("03:65") and is_hour("03:65")

--YesNo

is_minute("03:65") and is_hour("03:65")

--YesNo


Solutions

Expert Solution

The answer to the above question is as follows-

Short-circuit evaluation - It means that while evaluating any boolean, OR / AND expression, we stop as soon as we encounter a satisfaction or negation part.

Expression 1-

is_hour("03:65") and is_minute("03:65")

Short-circuit evaluation - NO

is_hour("03:65") returns True and AND condition requires both to be true to result in true .So second part,i.e,is_minute("03:65") is also evaluated which results in false. So there is not short circuit in this expression.

Expression 2-

is_minute("03:65") and is_hour("03:65")

Short-circuit evaluation - YES

is_minute("03:65") returns False and since, here we have an AND condition, first part being false results in false. So no need to check the second part. So there is short circuit evaluation.

Expression 3-

is_hour("03:65") or is_minute("03:65")

Short-circuit evaluation - YES

is_hour("03:65") returns True and this is an OR condition, it only needs one true part. So no need to check for second part and there is short circuit evaluation here.

Expression 4-

is_minute("03:65") or is_hour("03:65")

Short-circuit evaluation - NO
is_minute("03:65") returns False and since OR condition requires at least one true to result to true, it check the next part if its true. Then is_hour("03:65") part is checked. So there is no short circuit evaluation.


Related Solutions

Bash scripting return output: Determine the output this code 1. #!/bin/bash Valid = true count=1 while...
Bash scripting return output: Determine the output this code 1. #!/bin/bash Valid = true count=1 while [ $valid ] do echo $count if [ $count -eq 5 ]; then break fi ((count++)) done 2. #!/bin/bash num=100 for x in $(seq 1 5) do temp=$((num+x)) if [ $temp -1e 103]; then echo $temp else echo "Too Big" fi done 3.#!/bin/bash num=5 for x in $(seq 1 5) do echo $((num*x)) done 4. #!/bin/bash for x in $(seq 1 10) do echo...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT