Home / Past papers / MAD1 Quiz 2 / this paper

```python
from flask import Flask, request
from string import Template
app = Flask(__name__)
@app.route("/welcome")
def welcome():
username = request.args.get("user", "Guest")
role = request.args.get("role", "Member")
t = Template("Hello $name! Your role is $role_type.")
result = t.substitute(name=username.upper(), role_type=role.lower())
return result
if __name__ == "__main__":
app.run()
```
Assume that the Flask app is running at http://127.0.0.1:5000. What will be the rendered output in the browser for the URL: http://127.0.0.1:5000/welcome?user=Alice&role=ADMIN
It raises a KeyError because `$role_type` is missing in the URL.
Which of the following is the correct output of the above code?```python
def modify(func):
def wrapper(p, q):
result = func(p, q)
return result + 5
return wrapper
@modify
def bar(a, b):
return a * b
print(bar(2, 3))
```
What will be the output when the above HTML file is rendered?```html
<!DOCTYPE html>
<html>
<head>
<style>
p { color: blue; }
.text { color: green; }
#para { color: red; }
</style>
</head>
<body>
<p id="para" class="text" style="color: black;">
IITM BS Degree Program
</p>
</body>
</html>
```
IITM BS Degree Program

IITM BS Degree Program

IITM BS Degree Program

IITM BS Degree Program

```python
@app.route("/user/id")
```
```python
@app.route("/user/<int:id>")
```
```python
@app.route("/user/<id>")
```
```python
@app.route("/user/<float:id>")
```

```python
from flask import Flask
app=Flask(__name__)
@app.route("/user/<name>")
def greet(name):
return f"<p>Welcome {name}</p>"
app.run(debug=True)
```
Assume that the Flask app is running at http://127.0.0.1:5000. Which of the following URL requests will render the output without any errors?

Filename: app.py
```python
from flask import Flask, render_template
app=Flask(__name__)
@app.route("/")
def home():
return render_template("index.html")
app.run(debug=True)
```
Filename: index.html
```html
<!DOCTYPE html>
<html lang="en">
<body>
<div>
Output is:
{% for i in [1,2,3] %}
{% set x = i %}
{% endfor %}
{{ x }}
</div>
</body>
</html>
```
Assume that the Flask app is running at http://127.0.0.1:5000. What will be the rendered output
in the browser for the URL: http://127.0.0.1:5000?
What will be the output in the console window when the above HTML doc is rendered on the browser ?```html
<!DOCTYPE html>
<html>
<body>
<ol id="lst">
<li>App Dev-1</li>
</ol>
</body>
<script>
let lst_items=document.getElementsByTagName("li");
document.getElementById("lst").innerHTML += "<li>App Dev-2</li><li>App dev projects</li>";
console.log(lst_items.length);
</script>
</html>
```
Filename: app.py
```python
from flask import Flask, request
app=Flask(__name__)
@app.route("/login",methods=["POST"])
def signin():
args=request.get_json()
if args["uid"]=="admin" and args["pwd"]=="12345":
return "Welcome Admin!"
else:
return "Invalid credentials..."
app.run(debug=True)
```
Assume that the Flask app is running at http://127.0.0.1:5000. Which of the following "curl" commands or URL requests returns Method Not Allowed error(on bash terminal)?

```python
from flask import Flask
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
products = [
{"id": 1, "name": "books", "price": 56},
{"id": 2, "name": "pens", "price": 61},
{"id": 3, "name": "bulbs", "price": 76},
]
class MyApiResource(Resource):
### Missing code ###
api.add_resource(MyApiResource, "/<int:id>")
if __name__ == "__main__":
app.run(debug=True)
```
Assume that the Flask app is running at http://127.0.0.1:5000. Which of the following codes when placed at ###Missing code ### will work correctly to search a product by id?
```python
def post(self):
for p in products:
if p["id"] == id:
return f"{p['id']}, {p['name']}, {p['price']}"
return f"msg: {id} not found"
```
```python
def get(self,id):
for p in products:
if p["id"] == id:
return f"{p['id']}}, {p['name']}}, {p['price']}}"
return f"msg: {id} not found"
```
```python
def get(self):
for p in products:
if p["id"] == id:
return f"{p['id']}, {p['name']}, {p['price']}"
return f"msg: {id} not found"
```
```python
def post(self, id):
for p in products:
if p["id"] == id:
return f"{p['id']}, {p['name']}, {p['price']}"
return f"msg: {id} not found"
```
Based on the above data, answer the given subquestions.
```python >>> u1 = User(uname = "Jane", pwd = "1234") >>> p1 = Profile(name = "Analyst", email = "zoozle.com", user_id = u1.id) >>> db.session.add_all([u1,p1]) >>> db.session.commit() >>> user1 = User.query.filter_by(uname="Jane").first() >>> user1.profile ```

<User 1>

``` [<Profile 1>] ```

```html <Profile 1> ```

``` 'Analyst' ```