ProgrammingHardAccenture pattern

What is printed?

What is printed?
def validate(f):
    def wrapper(x):
        if x < 0:
            raise ValueError("negative")
        return f(x)
    return wrapper
@validate
def sqrtApprox(x):
    return x ** 0.5
try:
    print(sqrtApprox(-4))
except ValueError as e:
    print("error:", e)
print(sqrtApprox(16))
  1. A. error: negative 4.0
  2. B. -4.0 4.0
  3. C. error: negative error: negative
  4. D. 2.0 4.0

Correct answer: A. error: negative 4.0

sqrtApprox(-4) is wrapped by validate, which raises ValueError before ever computing a square root, caught and printed as 'error: negative'. sqrtApprox(16) passes validation and returns 16**0.5 = 4.0.

More Python Programming questions

Exams that ask this topic