Home / Past papers / MAD1 Quiz 2 / this paper

Consider the following table "product" created in SQLite database corresponding to model class "Product" using flask_sqlalchemy. | id | name | type | form | quantity | |---|---|---|---|---| | 1001 | Cold Drinks | edible | liquid | 100 | | 1002 | Detergent | non-edible | liquid | 126 | | 1005 | Rugs | non-edible | solid | 345 | | 1008 | LPG Cylinders | accessories | solid | 236 | | 1009 | Curd | edible | semi-solid | 520 | Which of the following code snippets will change the type of edible liquids to "perishable" and will increase their quantities by 450 correctly when typed in the Python console?

```python >>> prod = Product.query.filter_by(type = 'edible').all() >>> for item in prod: ... item.type = "perishable" ... item.quantity = 450 ... >>> db.session.commit() ```

```python >>> prod = Product.query.filter_by(type='edible', form="liquid").all() >>> for item in prod: ... item.type = "perishable" ... item.quantity += 450 ... >>> db.session.commit() ```

```python >>> prod = Product.query.filter_by(type='edible', form="liquid").all() >>> for item in prod: ... item.form = "perishable" ... item.quantity = 450 ... >>> db.session.commit() ```

```python >>> prod = Product.query.filter_by(type = 'edible').all() >>> for item in prod: ... item.form = "perishable" ... item.quantity += 450 ... >>> db.session.commit() ```

Consider the following python code.
```python
from flask import Flask
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
salaries = {
1: {"name": "Ravi", "basic": 14500},
2: {"name": "David", "basic": 9500},
3: {"name": "Tinu", "basic": 15009},
}
class Salaries(Resource):
def do_something(self):
global salaries
for s in salaries.keys():
salaries[s]["hra"] = salaries[s]["basic"] * 0.10
def get(self):
self.do_something()
return salaries
api.add_resource(Salaries, "/api/salaries")
app.run(debug=True)
```
Consider that the above code is running locally on "http://127.0.0.1:5000". What will the output for the URL: "http://127.0.0.1:5000/api/salaries"?
```json
{
"1": {"hra": 1450.0 },
"2": {"hra": 950.0 },
"3": {"hra": 1500.9}
}
```
```json
{
"1": {
"name": "Ravi",
"basic": 14500,
"hra": 1450.0
},
"2": {
"name": "David",
"basic": 9500,
"hra": 950.0
},
"3": {
"name": "Tinu",
"basic": 15009,
"hra": 1500.9
}
}
```
```json
{
"1": {
"name": "Ravi",
"basic": 14500
},
"2": {
"name": "David",
"basic": 9500
},
"3": {
"name": "Tinu",
"basic": 15009
}
}
```
```json
{
"1": {
"basic": 14500,
"hra": 1450.0
},
"2": {
"basic": 9500,
"hra": 950.0
},
"3": {
"basic": 15009,
"hra": 1500.9
}
}
```
Consider the following flask application.
app.py
```python
from flask import Flask, abort, request
app = Flask(__name__)
@app.route('/login/<id>')
def login(id):
if id:
if id.isalnum():
abort(400, "Bad Request: Invalid ID")
return f'<h1>Your ID is: {id}</h1>'
return f'<h1>Invalid ID</h1>'
app.run(debug=True)
```
Which of the following statements is/are true if the application is running locally on http://127.0.0.1:5000 ?
For URL http://127.0.0.1:5000/login?id=MAD-I the browser will render Not Found

For URL http://127.0.0.1:5000/login/ the browser will render **Invalid ID**

For URL http://127.0.0.1:5000/login/CS2003 the browser will render Bad Request: Invalid ID

For URL http://127.0.0.1:5000/login/PDSA the browser will render Bad Request: Invalid ID

Consider the following flask application.
app.py
```python
from flask import Flask, abort, request
app = Flask(__name__)
data = {'PDSA':'CS2003', 'DBMS':'CS2004', 'Java':'CS2005'}
@app.route('/course/<course_name>')
def course(course_name):
course_id = request.args.get('id')
if course_name in data and course_id == data[course_name]:
return f'<h1>The Course ID for {course_name} is: {course_id}</h1>'
else:
abort(400, "Bad Request: Invalid data")
app.run(debug=True)
```
Which of the following statements is/are true if the application is running locally on
http://127.0.0.1:5000 ?
For URL http://127.0.0.1:5000/PDSA?id=CS2003 the browser will render; **Not Found**

For URL http://127.0.0.1:5000/course/Java?id=CS2005 the browser will render; **The Course ID for Java is: CS2005**

For URL http://127.0.0.1:5000/course?course_name=DBMS&id=CS2004 the browser will render; **The Course ID for DBMS is: CS2004**

For URL http://127.0.0.1:5000/course/DBMS?id=CS2003 the browser will render; Bad Request: Invalid data
Consider the following HTML document “form.html” and the flask route given in “app.py”. Assume all the required methods are imported, Which of the following statements is/are correct.
Filename: form.html
```html
<!DOCTYPE html>
<html>
<head>
<style>
input:valid {border: 2px solid red;}
</style>
</head>
<body>
<form action="/submit_form" method="post">
<label for="e-mail">Please enter your email address</label>
<input id="e-mail" name="e-mail" required/>
<button>Submit</button>
</form>
</body>
</html>
```
Filename: app.py
```python
@app.route('/submit_form', methods = ['GET', 'POST'])
def form_on_submit():
if request.method == 'POST':
email = request.form.get('e-mail')
if "@" in email:
return "Form is validated"
return render_template('form.html')
```
Consider the following flask code.
```python
from flask import Flask
import json
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def __str__(self) -> str:
result = {
"brand": self.brand,
"model": self.model,
}
return str(result)
class MyApi(Resource):
def get(self, brand, model):
c = Car(brand, model)
return json.dumps(c, default=vars)
api.add_resource(MyApi, "/api/<string:brand>/<string:model>")
app.run(debug=True)
```
If the flask server is running locally on "http://127.0.0.1:5000/". Which of the following URLs will result in a Not Found error?
http://127.0.0.1:5000/api/maruti/swift

``` http://127.0.0.1:5000/api/brand=Hyundai/model=Creta ```

``` http://127.0.0.1:5000/api/brand=Ford,model=Fiesta ```

http://127.0.0.1:5000/api/Honda&City

http://127.0.0.1:5000/api/model=Accent/brand=Hundai

Consider the flask code below.
```python
from flask import Flask
app = Flask(__name__)
@app.route("/")
def get_home():
a = [i for i in range(11)]
t = ", ".join(map(str, a))
return t
@app.route("/numbers/<int:n>")
def get_numbers(n):
a = [i for i in range(n + 1)]
t = ", ".join(map(str, a))
return t
if __name__ == "__main__":
app.run(debug=True)
```
Assume that the above flask application is running on http://127.0.0.1:5000. Which of the following statements is/are True for the above code snippet?
URL : http://127.0.0.1:5000/ Output: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

URL: http://127.0.0.1:5000/numbers/5 Output: 0, 1, 2, 3, 4, 5

URL: http://127.0.0.1:5000/7 Output: 0, 1, 2, 3, 4, 5, 6, 7

URL: http://127.0.0.1:5000/7 Output: **Not Found**

```python
class Student(db.Model):
id=db.Column(db.Integer, primary_key=True)
name=db.Column(db.String(50))
class Assignment(db.Model):
id=db.Column(db.Integer, primary_key=True)
type=db.Column(db.String(4))
marks=db.Column(db.Integer)
student_id=db.Column(db.Integer)
```
```python
class Student(db.Model):
id=db.Column(db.Integer, primary_key=True)
name=db.Column(db.String(50))
assignments=db.relationship('Assignment', backref='student')
class Assignment(db.Model):
id=db.Column(db.Integer, primary_key=True)
type=db.Column(db.String(4))
marks=db.Column(db.Integer)
student_id=db.ForeignKey('student.id')
```
```python
class Student(db.Model):
id=db.Column(db.Integer, primary_key=True)
name=db.Column(db.String(50))
assignments=db.relationship('Assignment', backref='student')
class Assignment(db.Model):
id=db.Column(db.Integer, primary_key=True)
type=db.Column(db.String(4))
marks=db.Column(db.Integer)
student_id=db.Column(db.Integer, db.ForeignKey('student.id'))
```
```python
class Student(db.Model):
id=db.Column(db.Integer, primary_key=True)
name=db.Column(db.String(50))
assign_id=db.Column(db.Integer, db.Foreignkey('Assignment.id'))
class Assignment(db.Model):
id=db.Column(db.Integer, primary_key=True)
type=db.Column(db.String(4))
marks=db.Column(db.Integer)
```
Consider the following resource created using Flask, assume that the application is running on a terminal 1 and being accessed using CURL on terminal 2. Answer the given subquestions.
What will be returned on the terminal 2 for the command:
``` curl http://127.0.0.1:5000/delete/val1 ```

```json
{"message": "Value deleted successfully", "value": "val1"}
```
```json
{"message": "Value updated successfully", "value": "val1"}
```
```json
{"val1": "value1"}
```
None of these.
What will be returned on the terminal 2 for the command:
``` curl http://127.0.0.1:5000/delete/va11 -X PUT ```

```json
{
"message": "Value deleted successfully",
"value": "val1"
}
```
```json
{"message": "Value updated successfully", "value": "val1"}
```
```json
{"val1": "value1"}
```
None of these.

Identify the correct order of tasks that take place when a client makes a request on the URL: https://example.com/home.html. 1. The web browser sends an HTTPS request to the server, requesting a copy of home.html. 2. The web browser assembles the response and displays it. 3. The server responds either with the requested resource or an error code. 4. The web browser connects to the DNS server to get the server IP address, for example.com.
Auto-generated summary from the source site. Handy as a checklist; the lessons above explain each idea from scratch.
Equations:
Code Snippets:
session.query(Product).filter(Product.type == 'edible', Product.form == 'liquid').update(
{Product.type: 'perishable', Product.quantity: Product.quantity + 450},
synchronize_session=False
)
session.commit()Code Snippets:
from flask import Flask, request, jsonify
from flask_restful import Resource, Apiapp = Flask(_name_) api = Api(app)
class Salaries(Resource): def get(self): salaries = { 1: {"name": "Ravi", "basic": 14500, "hra": 1450}, 2: {"name": "David", "basic": 9500, "hra": 950}, 3: {"name": "Jiny", "basic": 15009, "hra": 1500.9} } return jsonify(salaries)
api.add_resource(Salaries, '/api/salaries')
if _name_ == '_main_': app.run(debug=True) ```
Code Snippets:
<!DOCTYPE html>
<html>
<head>
<style>
input:valid {border: 2px solid red;}
</style>
</head>
<body>
<form action="/submit_form" method="post">
<label for="e-mail">Please enter your email address</label>
<input id="e-mail" name="e-mail" required/>
<button>Submit</button>
</form>
</body>
</html>Equations:
Code Snippets:
class Student(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50))
assignments = db.relationship('Assignment', backref='student')class Assignment(db.Model): id = db.Column(db.Integer, primary_key=True) type = db.Column(db.String(4)) marks = db.Column(db.Integer) student_id = db.Column(db.Integer, db.ForeignKey('student.id')) ```
Code Snippets:
curl http://127.0.0.1:5000/delete/val1