Bird
0
0

Consider this retry decorator and test function:

medium📝 Predict Output Q4 of 15
Selenium Python - Advanced Patterns
Consider this retry decorator and test function:
def retry(func):
    def wrapper():
        attempts = 3
        for i in range(attempts):
            try:
                return func()
            except Exception:
                pass
        return 'Failed'

count = 0
@retry
def test():
    global count
    count += 1
    if count < 3:
        raise Exception('Fail')
    return 'Success'

print(test())

What will be printed when running this code?
ASuccess
BFail
CException raised
DNone
Step-by-Step Solution
Solution:
  1. Step 1: Analyze retry attempts

    The decorator retries up to 3 times.
  2. Step 2: Test function behavior

    Test raises exceptions on first two calls, then returns 'Success' on third.
  3. Step 3: Execution flow

    First two attempts raise exceptions caught by decorator; third attempt returns 'Success'.
  4. Final Answer:

    Success -> Option A
  5. Quick Check:

    Retries until success or max attempts [OK]
Quick Trick: Retries stop once function succeeds within max attempts [OK]
Common Mistakes:
  • Assuming it returns 'Failed' after two failures
  • Expecting an exception to propagate
  • Ignoring the retry count logic

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Python Quizzes