I went to a job interview yesterday and I got the following test question:
What is the output of this logic:
if(TRUE && FALSE AND TRUE OR 1 || TRUE && ((TRUE || FALSE) || FALSE)) {
PRINT "A";
}
else print "B";
I had to evaluate the result in my head, which I could not do.
How would you go about this?
The expression has in the middle, outside any parenthesis: OR 1 ||
. It does not matter how the left and right expressions evaluate, at the end they are combined with OR TRUE
and the result is always TRUE
.
The printed value is A
.
The result will print A
.
It will go in if
Explanation:
TRUE && FALSE AND TRUE OR 1 || TRUE && ((TRUE || FALSE) || FALSE)
TRUE && FALSE -> FALSE -> LETS NAME IT FALSE(1)
FALSE(1) AND TRUE -> FALSE -> LETS NAME IT FALSE(2)
FALSE(2) OR 1 -> TRUE -> LETS NAME IT TRUE(3)
------ Now second part ------------
TRUE --> TRUE (4)
((TRUE || FALSE) -> TRUE (5)
TRUE (5) || FALSE --> TRUE (6)
NOW
combining two expressions:
TRUE(3) || TRUE(6) is TRUE
Take a look at the OR 1
As this is on the... well let's call it top level it will be true OR anything else... which results in "true" ;)