Skip to content

Question 10

What is the output of the following code? [3 marks]

def fn(t):
    x, y = t**2, t**3
    if x > 1 and y > 1:
        return True
    elif x > 1 or y > 1:
        return x*y > 0
    elif y < 1:
        return False
    return x * y


print(fn(1), fn(0), fn(2))
Solution

For fn(1):

  • t = 1. We also define x = 1**2 = 1 and y = 1**3 = 1.
  • x > 1 and y > 1: False and False = False \(\Rightarrow\) skip if block
  • x > 1 or y > 1: False or False = False \(\Rightarrow\) skip elif block
  • y < 1: False \(\Rightarrow\) skip elif block
  • return x * y = 1 * 1 = 1

For fn(0):

  • t = 0. We also define x = 0**2 = 0 and y = 0**3 = 0.
  • x > 1 and y > 1: False and False = False \(\Rightarrow\) skip if block
  • x > 1 or y > 1: False or False = False \(\Rightarrow\) skip elif block
  • y < 1: True \(\Rightarrow\) enter elif block
  • return False

For fn(2):

  • t = 2. We also define x = 2**2 = 4 and y = 2**3 = 8.
  • x > 1 and y > 1: True and True = True \(\Rightarrow\) enter if block
  • return True

Hence, the print statement below outputs 1, False and True, in this order separated by spaces.

Answer
1 False True