Multiple Choice Questions (MCQs)
Q1. How do you access a single value of the ‘limit’ parameter in Bottle?
a) request.query[‘limit’]
b) app.request.query.get(‘limit’)
c) bottle.params.limit
d) None, as there’s no direct way to get parameters
Answer: b) app.request.query.get(‘limit’)
Q2. In a Bottle application that retrieves query strings using the request.query
object (which behaves like a dictionary), what would be returned if you call /search?query=bottle&limit=5
without setting any default values for missing parameters in your endpoint function?
a) An empty string and 0
b) ‘default’ value as set by .get() method with no defaults specified elsewhere.
c) KeyError since the key doesn’t exist unless a default is provided using .get('key', default)
d) The exact query values passed
Answer: c) KeyError since the key doesn’t exist unless a default is provided using `.get(‘key’, default’)
Q3. Which Bottle method should you use to handle multiple occurrences of ‘tags’ in your endpoint function?
a) request.query.get
b) bottle.params[‘tags’]
c) app.request.query.all
d) request.query.getall
Answer: d) request.query.getall
Q4. How do you provide a default value for the query parameter category
if it is not included in the URL?
a) By setting category=default directly into your endpoint function.
b) Using app.request.query[‘category’] = ‘default’ within your route decorator
c) Via .get(‘key’, default), such as: return f"Category set to {request.query.get(‘category’, ‘all’)}"
d) The Bottle framework does not support setting a default value for query parameters
*Answer: c) Via .get(‘key’, default), such as: return f"Category set to {request.query.get(‘category’, ‘all’)}"
Q5. Which of the following correctly shows how you should handle an integer conversion attempt on page
with validation and a fallback in Bottle?
a)
def items():
page = int(request.query.get('page'))
b)
@app.route('/items')
def items():
try:
page = int(request.query.get('page', 1))
except ValueError as e:
return "Invalid page number", 400
return f"Showing page {page}"
c)
None, since Bottle does not support conversion or validation.
d)
```python
@app.route('/items')
def items():
try:
limit = int(request.query.get('limit'))
except ValueError as e:
return "Invalid value for 'limit'", 400
return f"Showing {page}"
e)
Answer: b)
Multiple Choice Questions (MCQs)
Q6. Which of the following statements is true about handling multiple occurrences in query parameters using Bottle?
a) The .getall()
method can only retrieve a single value for each key.
b) If ‘tags’ appears twice, it will be retrieved as [‘python’, None].
c) Using request.query.get('tag')
would ignore all subsequent values of the same parameter name in the URL after its first occurrence if not using .getall()
.
d) The second call to /search?query=bottle&limit=5&tags=java&tags=django
will return None for ‘tags’.
Answer: c) Using
request.query.get('tag')
would ignore all subsequent values of the same parameter name in the URL after its first occurrence if not using.getall()
.
Q7. What is a common practice to ensure your web application behaves predictably when dealing with query parameters that might be missing or malformed?
a) Always assume data will always come through correctly and directly use it without validation.
b) Provide default values for expected input fields, validate inputs before processing them further in the app logic.
c) Log all incoming request details to debug later instead of handling defaults within code flow.
d) Only allow integer parameters with a fallback that converts any non-integer value into zero.
Answer: b) Provide default values for expected input fields and validate inputs before proceeding.
Q8. How does Bottle’s request.query.get()
method behave when called without specifying an optional default parameter?
a) It raises KeyError since the key doesn’t exist.
b) Returns None if no value is found in query parameters, even with a non-empty URL string containing that ‘key’.
c) Defaults to returning True as it behaves like Python’s dictionary get() for boolean values by default when not specified explicitly or implicitly.
d) Throws an exception because Bottle discourages accessing the request.query
directly.
Answer: b) Returns None if no value is found in query parameters, even with a non-empty URL string containing that ‘key’.
Q9. When should you use .getall()
instead of simply getting values from individual keys for handling multiple occurrences?
a) Only when there are exactly two unique parameter names passed.
b) To retrieve all instances (occurrences in the query parameters), not just a single value or default, especially if more than one is expected and needed to be processed separately within your code logic. This method returns an array of values for those keys rather than None or any other fallback result when no matching key exists.
Answer: b) To retrieve all instances (occurrences in the query parameters), not just a single value, especially if more than one is expected and needed to be processed separately within your code logic.
True/False Questions
Q10. In Bottle web applications handling queries for multiple occurrences of keys like ‘tags’, it’s best practice always using .get()
instead of .getall()
.
Answer: False
*Q11. Request parameters passed in a URL after the ?
symbol can be handled directly as strings without conversion or validation unless specifically required by your application’s logic.
Answer: True
Match The Following Questions with Answers
12.Match the following statements about handling query parameters to their corresponding answers:
- Use
.get('key', default)
for providing defaults. - Convert and validate numeric values received from
request.query
. - If a key appears multiple times, use
request.query.getall()
instead of directly accessing it as if it’s unique each time.
a) This ensures that parameters like ‘limit’ are properly processed even when invalid data is encountered. Example: try { int(page); } except ValueError.
b) An example would be /search?category=books&tags=fantasy
, where the returned object might look something like {“category”: “books”, “tags”: [“fantasy”]}.
c) Using .get(‘tag’, ‘default_value’) in a function, such as return f"Selected tag: {request.query.get('tag', 'unselected')}"
d) When you want to access all occurrences of the same query parameter name. For example /search?category=books&tags=fantasy
will return {“categories”: [“books”], “tags”: [“fantasy”]} using .getall()
.
Answers:
1 - c)
2 - a)
3 - b)