Home / Past papers / MAD1 Quiz 2 / this paper

Consider the following table "ExamScores" in the SQLite database. | id | score | |----|-------| | 1 | 57 | | 2 | 98 | | 3 | 83 | Which of the following Python statements will print the maximum score value?

```python >>> print(db.session.query.max(ExamScores.score)) ```

```python >>> print(db.session.query(func.max(ExamScores.score)).scalar()) ```

```python >>> print(db.session.query(func.max(ExamScores.score))) ```

```python >>> print(db.session.query.max(ExamScores.score).scalar()) ```

```html <script src="jsfile.js"></script> ```

```html <script href="jsfile.js"></script> ```

```html <import src="jsfile.js"></import> ```

```html <import link="jsfile.js"></import> ```

Consider the following Flask and HTML implementations.
Flask file: main.py
```python
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/text/size/<int:size>/<color>")
def set_size(size, color):
style = {"font-size": str(size) + "px", "color": color}
return render_template("index.html", css_style=style)
app.run(debug=True)
```
HTML file: index.html
```html
<!DOCTYPE html>
<html>
<div style="color: {{css_style['color']}};
font-size:{{css_style['font-size']}};">
IITM BS Degree Program
</div>
</html>
```
Assume that the Flask code is running on the server http://127.0.0.1:5000 without any errors.
Choose the correct URL to render the output below.
IITM BS Degree Program
http://127.0.0.1:5000?text=red&size=20

``` http://127.0.0.1:5000?size=20&text=red ```

``` http://127.0.0.1:5000/text/size/red/20 ```

``` http://127.0.0.1:5000/text/size/20/red ```

Consider the following Python code.
File name: main.py
```python
def calculate(funx):
def wrapper(a, b):
x = funx(a, b)
return x**2
return wrapper
@calculate
def foo(x, y):
return x - y
# main code
print(foo(5, 2))
```
What is the correct output of the given code?
Consider the following Python code snippet. ```python from string import Template msg = "The $lang programming language is $desc and $useful for web development." template = Template(msg) print(=== OUTPUT ===) ``` Which of the following statements, when substituted in place of `=== OUTPUT ===`, will throw a KeyError?

```python
template.substitute({'lang': 'Python'})
```
```python
template.substitute({'lang': 'Python', 'desc': 'popular',
'useful': 'useful'})
```
```python
template.safe_substitute({'lang': 'Python'})
```
```python
template.safe_substitute({'lang': 'Python', 'desc':
'versatile'})
```
Consider the following Flask app code.
```python
from flask import Flask, request
valid_users = [
{"uid": "student", "pwd": "12345"},
{"uid": "professor", "pwd": "54321"},
{"uid": "hod", "pwd": "abcde"},
]
app = Flask(__name__)
@app.route("/signin")
def login():
args = request.args
for vu in valid_users:
if vu["uid"] == args["uid"] and vu["pwd"] == args["pwd"]:
return "You are authorized!!"
else:
return "You are unauthorized!!"
app.run(debug=True)
```
Assume the above flask app runs on "http://127.0.0.1:5000/" and is accessed through the web browser. What will be the output for the URL: "http://127.0.0.1:5000/signin?uid=professor&pwd=54321" ?

Consider the following HTML code.
Filename: main.html
```html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Greetings</title>
</head>
<body>
<button onclick="alert('Hello, welcome')">Greet1!</button><br>
<button onclick="console.log('Hello, welcome')">Greet2!</button><br>
<button onclick="document.write('Hello, welcome')">Greet3!</button><br>
<button onclick="console.error('Hello, welcome')">Greet4!</button>
</body>
</html>
```
Rendered HTML output below:
[Greet1!] (button)
[Greet2!] (button)
[Greet3!] (button)
[Greet4!] (button)
Which of the above “Greet” buttons will print greetings on the dialogue box?
Consider the following flask application.
```python
from flask import Flask, abort
app = Flask(__name__)
users = ["admin", "user_1", "user_2", "user_3"]
@app.route('/home/<string:username>', methods = ['POST'])
def home(username):
if username in users:
return f"<h1>Hello {username}, Welcome!</h1>"
else:
abort(407)
@app.errorhandler(404)
def user_error_1(error):
return "<h1>The user you are looking for is invalid.</h1>"
@app.errorhandler(405)
def user_error_2(error):
return "<h1>Please check the web request.</h1>"
@app.errorhandler(407)
def user_error_4(error):
return "<h1>Proxy authentication required.</h1>"
app.run()
```
If the application is running locally on `http://127.0.0.1:5000`. What will be rendered on the browser for the URL `http://127.0.0.1:5000/home/user_4` ?
Hello user_4, Welcome!

The user you are looking for is invalid.

Not Found

Please check the web request

Consider the Flask Restful API code given below:
File name: app.py
```python
from flask import Flask, request
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
class APICalculator(Resource):
def post(self):
d = request.json
a = int(d["val1"])
b = int(d["val2"])
oper = d["operator"]
if oper == "+":
return {"Sum": (a + b)}, 200
elif oper == "-":
return {"Difference": (a - b)}, 200
elif oper == "*":
return {"Product": (a * b)}, 200
else:
return {"Error": "Invalid option"}, 401
api.add_resource(APICalculator, "/")
app.run(debug=True)
```
Assume that the Flask server is running on http://127.0.0.1:5000. Which of the following is the correct curl command(s) that will return calculated values?
**Note: Assume data sent is valid for cmd, and powershell**
```
curl -X POST "http://127.0.0.1:5000" -H 'Content-Type: application/json' -d '{"val1":27, "val2":3, "operator":"+"}'
```
```
curl -X POST "http://127.0.0.1:5000" -H 'Content-Type: application/json' -d
'{"val1":27,"val2":3,"operator":"**"}'
```
```
curl -X POST "http://127.0.0.1:5000" -H 'Content-Type: application/json' -d '{"val1":27,"val2":3,"operator":"/"}'
```
```
curl -X POST "http://127.0.0.1:5000" -d
'{"val1":27, "val2":3, "operator":"/"}'
```
```
curl -X POST "http://127.0.0.1:5000" -H 'Content-Type: application/json' -d '{"val1":27, "val2":3, "operator":"-"}'
```
Consider a document of 2000 characters written using only the characters from the character set L1 = {'x', 'y', 'z', 'w', 'v', 'u', 't', 's'}. If the character set L1 is to be upgraded by adding all the characters from the set L2 = {'1', '2', '3', '4', '5'}, by how many bits will the size of the original document increase?(assume an optimal number of bits are used for representing each character).

You need to obtain a list of (id, days_to_fix) where days_to_fix is the number of days between broken_at and fixed_at. Using Flask-SQLAlchemy, which of the following approaches will work?

```python
repairs = GlassRepair.query.all()
result = [
(r.id, (r.fixed_at - r.broken_at).days)
for r in repairs
]
```
```python
repairs = db.session.query(GlassRepair).all()
result = []
for r in repairs:
delta = r.fixed_at - r.broken_at
result.append((r.id, delta.days))
```
```python
repairs = GlassRepair.query(
GlassRepair.id,
(GlassRepair.fixed_at - GlassRepair.broken_at).label('days_to_fix')
).all()
```
```python
from datetime import timedelta
repairs = GlassRepair.query.all()
result = [
(r.id, timedelta(days=(r.fixed_at - r.broken_at)).days)
for r in repairs
]
```
You want to retrieve all repairs that took more than 7 days to fix. Using Flask-SQLAlchemy, which of the following approaches will correctly yield a list of (id, days_to_fix) only for those records where days_to_fix > 7?

```python
repairs = GlassRepair.query.filter_by(status='fixed').all()
result = [
(r.id, (r.fixed_at - r.broken_at).days)
for r in repairs
if (r.fixed_at - r.broken_at).days > 7
]
```
```python
from sqlalchemy import and_
repairs = GlassRepair.query.filter(
and_(
GlassRepair.status=='fixed',
(GlassRepair.fixed_at - GlassRepair.broken_at) > 7
)
).all()
result = [(r.id, (r.fixed_at - r.broken_at).days) for r in repairs]
```
```python
repairs = db.session.query(GlassRepair).all()
result = []
for r in repairs:
days = (r.fixed_at - r.broken_at).days
if days > 7 and r.status == 'fixed':
result.append((r.id, days))
```
```python
repairs = GlassRepair.query(
GlassRepair.id,
(GlassRepair.fixed_at - GlassRepair.broken_at).label('days_to_fix')
).filter_by(status='fixed').filter(
GlassRepair.fixed_at - GlassRepair.broken_at > 7
).all()
```Auto-generated summary from the source site. Handy as a checklist; the lessons above explain each idea from scratch.
Template class from the string module for string substitution.Bits Required for Character Representation: