Question 12
What is the output of the following code?
Solution
foo(p1)
Given p1 = (4,-4), in foo(), x = 4, y = -4.
x > 0andy > 0:True and False\(\Rightarrow\)Falsex > 0 or y > 0:True or False\(\Rightarrow\)False, hencefoo(p1)returnsFalse.
foo(p2)
Given p2 = (0,0), in foo(), x = 0, y = 0.
x > 0 and y > 0:False and False\(\Rightarrow\)Falsex > 0 or y > 0:False or False\(\Rightarrow\)Falsex < 0:False
We approach print(p), where you print out (0,0) as the value passed into argument p before returning None from foo().
What this means for the output is that (0, 0) will be printed here immediately.
(0, 0)
# formulating print(foo(p1), foo(p2), foo(p3)), since it's trying to get all the returned values from each foo() call before printing it out
foo(p3)
Given p3 = (4,-4), in foo(), x = -4, y = -4.
x > 0 and y > 0:False and False\(\Rightarrow\)Falsex > 0 or y > 0:False or False\(\Rightarrow\)Falsex < 0:True, hencefoo(p3)returnsTrue.
With foo(p1), foo(p2) and foo(p3) returning False, None and True respectively, the print statement at the end prints them with space characters in between.