Bird
Raised Fist0
Flaskframework~20 mins

Route decorator (@app.route) in Flask - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Flask Route Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output when accessing '/' route?
Given this Flask app code, what will the browser display when visiting the '/' URL?
Flask
from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return 'Welcome Home!'

if __name__ == '__main__':
    app.run()
AThe browser shows a 404 Not Found error
BThe browser shows the text: Welcome Home!
CThe browser shows a 500 Internal Server Error
DThe browser shows a blank page
Attempts:
2 left
💡 Hint
Check what the @app.route('/') decorator does.
📝 Syntax
intermediate
2:00remaining
Which option causes a syntax error in route definition?
Identify which route decorator syntax is invalid in Flask.
A@app.route('/profile' methods=['GET'])
B@app.route('/contact', methods=['GET', 'POST'])
C@app.route('/about')
D@app.route('/login', methods=['POST'])
Attempts:
2 left
💡 Hint
Look for missing punctuation in the decorator.
❓ state_output
advanced
2:00remaining
What is the output of this Flask route with variable?
What will be the output when visiting '/user/Alice' in this Flask app?
Flask
from flask import Flask
app = Flask(__name__)

@app.route('/user/<name>')
def user(name):
    return f'Hello, {name}!'

if __name__ == '__main__':
    app.run()
AHello, user!
BHello, <name>!
CHello, Alice!
D404 Not Found error
Attempts:
2 left
💡 Hint
The part captures the URL segment as a function argument.
🔧 Debug
advanced
2:00remaining
Why does this Flask app raise a 404 error for '/hello'?
Given the code below, why does visiting '/hello' cause a 404 error?
Flask
from flask import Flask
app = Flask(__name__)

@app.route('/hello/')
def greet_slash():
    return 'Hi with slash!'

if __name__ == '__main__':
    app.run()
ABecause '/hello' and '/hello/' are treated as different routes in Flask
BBecause the greet() function has no return statement
CBecause Flask requires routes to be decorated with @app.get() instead of @app.route()
DBecause the app.run() is missing debug=True
Attempts:
2 left
💡 Hint
Check how Flask treats trailing slashes in routes.
🧠 Conceptual
expert
2:00remaining
Which option correctly explains Flask route matching behavior?
Choose the correct statement about how Flask matches URLs to route decorators.
AFlask ignores the order of route definitions when matching URLs
BFlask automatically matches any URL starting with the route prefix regardless of trailing slashes
CFlask matches routes case-insensitively by default
DFlask matches routes exactly and does not consider trailing slashes unless configured otherwise
Attempts:
2 left
💡 Hint
Think about how Flask treats trailing slashes and case sensitivity.

Practice

(1/5)
1. What does the @app.route decorator do in a Flask application?
easy
A. It connects a URL path to a specific function that runs when that URL is visited.
B. It creates a database connection for the app.
C. It styles the HTML pages served by Flask.
D. It starts the Flask server automatically.

Solution

  1. Step 1: Understand the role of @app.route

    The decorator @app.route is used to link a URL path to a function in Flask.
  2. Step 2: Identify what happens when the URL is visited

    When a user visits that URL, Flask runs the linked function to generate a response.
  3. Final Answer:

    It connects a URL path to a specific function that runs when that URL is visited. -> Option A
  4. Quick Check:

    @app.route links URL to function [OK]
Hint: Remember: @app.route maps URLs to functions [OK]
Common Mistakes:
  • Thinking @app.route starts the server
  • Confusing it with database setup
  • Assuming it styles pages
2. Which of the following is the correct syntax to create a route for the URL path /home in Flask?
easy
A. @app.route('/home')\ndef home():\n return 'Welcome!'
B. app.route('/home')\ndef home():\n return 'Welcome!'
C. @app.route('/home')\nreturn 'Welcome!'
D. @app.route('/home')\ndef home:\n return 'Welcome!'

Solution

  1. Step 1: Check the decorator syntax

    The correct decorator syntax uses @app.route('/home') placed immediately before a function definition.
  2. Step 2: Verify the function definition and return statement

    The function must be defined with parentheses and a colon, and it returns a string response.
  3. Final Answer:

    @app.route('/home')\ndef home():\n return 'Welcome!' -> Option A
  4. Quick Check:

    Decorator + function with parentheses = correct syntax [OK]
Hint: Decorator needs @ and function needs () and : [OK]
Common Mistakes:
  • Missing @ before app.route
  • Omitting parentheses in function definition
  • Placing return outside function
3. What will be the output when visiting the URL /user/42 given this Flask code?
@app.route('/user/<int:id>')
def show_user(id):
    return f"User ID is {id}"
medium
A. User ID is <int:id>
B. User ID is 42
C. 404 Not Found error
D. User ID is id

Solution

  1. Step 1: Understand the route with variable part

    The route /user/<int:id> captures an integer from the URL and passes it as the argument id to the function.
  2. Step 2: Check the function output

    The function returns a string with the captured id value inserted using an f-string.
  3. Final Answer:

    User ID is 42 -> Option B
  4. Quick Check:

    Variable URL part passes 42 to function [OK]
Hint: Variable parts in < > pass values to function [OK]
Common Mistakes:
  • Thinking is printed literally
  • Expecting a 404 error without reason
  • Confusing variable name with string
4. Identify the error in this Flask route code:
@app.route('/hello')
def hello():
print('Hello World')
return 'Hello!'
medium
A. The return statement is outside the function.
B. The route decorator is missing parentheses.
C. The function name is invalid.
D. The function body is not indented properly.

Solution

  1. Step 1: Check indentation inside the function

    Python requires the function body to be indented. Here, print and return are not indented under def hello():.
  2. Step 2: Verify decorator and function syntax

    The decorator has correct parentheses and the function name is valid, so no errors there.
  3. Final Answer:

    The function body is not indented properly. -> Option D
  4. Quick Check:

    Python needs indented function bodies [OK]
Hint: Indent function code under def line [OK]
Common Mistakes:
  • Forgetting to indent function body
  • Removing parentheses from decorator
  • Misnaming the function
5. You want to create a Flask route that accepts a username in the URL like /profile/john and returns Hello, john!. Which code correctly achieves this?
hard
A. @app.route('/profile/<username>')\ndef profile():\n return f'Hello, {username}!'
B. @app.route('/profile/')\ndef profile(username):\n return f'Hello, {username}!'
C. @app.route('/profile/<username>')\ndef profile(username):\n return f'Hello, {username}!'
D. @app.route('/profile/username')\ndef profile():\n return 'Hello, username!'

Solution

  1. Step 1: Use variable URL part for username

    The route must capture username as a string variable using <username> in the URL.
  2. Step 2: Define function with matching parameter and return greeting

    The function must accept username as a parameter and return the greeting using an f-string.
  3. Final Answer:

    @app.route('/profile/<username>')\ndef profile(username):\n return f'Hello, {username}!' -> Option C
  4. Quick Check:

    Variable URL + matching param + f-string = correct [OK]
Hint: Match variable name in route and function parameter [OK]
Common Mistakes:
  • Using for a string username
  • Not passing username parameter to function
  • Hardcoding username in route path
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard