The '/' route calls redirect('/welcome'), sending the browser to '/welcome'.
Step 2: Analyze the '/welcome' route
The '/welcome' route returns the string 'Hello, welcome!'. So the browser shows this text.
Final Answer:
Displays 'Hello, welcome!' in the browser -> Option B
Quick Check:
Redirect leads to '/welcome' which returns greeting [OK]
Hint: Redirect sends user to new route which returns the message [OK]
Common Mistakes:
Thinking redirect returns text directly
Expecting JSON instead of plain text
Assuming redirect causes error or crash
4. Identify the error in this Flask code snippet:
from flask import Flask, abort
app = Flask(__name__)
@app.route('/secret')
def secret():
abort(403)
return 'You should not see this'
medium
A. The return statement after abort is unreachable
B. abort must be called with a string, not a number
C. abort cannot be used inside route functions
D. Missing parentheses in abort call
Solution
Step 1: Understand abort behavior
The abort(403) immediately stops the request and sends a 403 error response.
Step 2: Check code after abort
The return statement after abort is never reached, so it is unreachable code.
Final Answer:
The return statement after abort is unreachable -> Option A
Quick Check:
Code after abort is ignored [OK]
Hint: Code after abort() won't run; it's unreachable [OK]
Common Mistakes:
Thinking abort needs string argument
Believing abort can't be used in routes
Forgetting abort stops execution immediately
5. You want to redirect users to a login page if they are not authenticated, otherwise show their dashboard. Which Flask code snippet correctly uses redirect and abort to handle this?
hard
A. if not user_authenticated:
abort(401)
else:
return redirect('/dashboard')
B. if user_authenticated:
return redirect('/login')
else:
abort(200)
C. if not user_authenticated:
return redirect('/login')
else:
return 'Welcome to your dashboard!'
D. if user_authenticated:
abort(404)
else:
return redirect('/login')
Solution
Step 1: Understand user authentication logic
If the user is not authenticated, they should be sent to the login page using redirect('/login').
Step 2: Handle authenticated users
If authenticated, show the dashboard message directly with a return string.
Step 3: Check abort usage
Using abort for normal redirects or success codes is incorrect; abort is for errors.
Final Answer:
if not user_authenticated:
return redirect('/login')
else:
return 'Welcome to your dashboard!' -> Option C
Quick Check:
Redirect unauthenticated, show dashboard if authenticated [OK]
Hint: Redirect unauthenticated users, return dashboard text if authenticated [OK]
Common Mistakes:
Using abort for normal redirects
Aborting with success codes like 200
Redirecting authenticated users to login
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.