Home / Past papers / MAD1 Quiz 2 / this paper

Consider the following flask application and select the correct option if the application is running locally on http://127.0.0.1:5000.
```python
from flask import Flask
app = Flask(__name__)
@app.route('/work')
@app.route('/home')
def my_task():
return "<h1>Hello! Reporting for my task</h1>"
app.run()
```
The application will return 200 OK status code for /home endpoint only

The application will return 200 OK status code for /work endpoint only

The application will return 200 OK status code for both the endpoints

The application will throw an AssertionError for both the endpoints

Which of the following statements about OpenAPI Specification (OAS) is true?

Map the terms in column A with their correct interpretations in column B. | | Column A | | Column B | |---|---|---|---| | a | Scale up | 1 | financial transactions | | b | ACID | 2 | adding more physical servers | | c | Scale out | 3 | increasing RAM | | d | BASE | 4 | social media |

a → 3, b → 1, c → 2, d → 4

a → 4, b → 2, c → 1, d → 3

a → 4, b → 3, c → 1, d → 2

a → 3, b → 2, c → 1, d → 4

Consider the following view function.
```python
@app.route('/student', methods = ['GET', 'POST'])
def show_student():
std = request.args
details = {
'Department': std['dept'],
'Course-level': std['level'],
'Course': std['course']
}
return details
```
If this flask app is running locally on http://127.0.0.1:5000, which of the following URLs will be handled by the controller correctly?
``` http://127.0.0.1:5000/student/Electrical/Diploma/MAD1 ```

``` http://127.0.0.1:5000/Electrical/Diploma/MAD1 ```

``` http://127.0.0.1:5000/student?dept=Electrical&level=Diploma&course=MAD1 ```

``` http://127.0.0.1:5000?endpoint=student&dept=Electrical&level=Diploma&course=MAD1 ```

What will be the output of the following Python code snippet on the terminal?
```python
def modify(n):
def modifier(n):
ser = [0,1]
for i in range(n-2):
new = ser[i]+ser[i+1]
ser.append(new)
print(ser)
return modifier
@modify
def list_num(n):
nums = []
for i in range(n):
nums.append(i+1)
print(nums)
list_num(10)
```
``` [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ```

``` [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ```

``` [0, 1, 1, 2, 3, 5, 8, 13, 21] ```

``` [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] ```

Consider the following HTML Document file given below.
index.html
```html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
input:invalid {
background: red;
}
input:valid {
background: green;
}
</style>
</head>
<body>
<form>
<label for="uname">Enter a valid e-mail:</label>
<input type="text" name="uname" minlength="8">
</form>
</body>
</html>
```
Suppose the index.html is rendered on the browser. What will be the background color of the input box when the user enters the name "madcourse.mail.com"?
Consider the following Python code snippet.
```python
from flask import Flask
from flask_restful import Api, Resource
app = Flask(__name__)
api = Api(app)
class MyApi(Resource):
def get(self):
return {"greet":"Hello from GET Api!"}
def put(self):
return {"greet":"Hello from PUT Api!"}
api.add_resource(MyApi, '/api/get', '/api/put', '/api/post')
app.run()
```
If this application is running locally on http://127.0.0.1:5000, which of the following curl commands will throw an error?
1. `curl http://127.0.0.1:5000/api/get -X get`
2. `curl http://127.0.0.1:5000/api/put -X put`
3. `curl http://127.0.0.1:5000/api/post -X post`
4. `curl http://127.0.0.1:5000/api/get -X put`
5. `curl http://127.0.0.1:5000/api/put -X get`
6. `curl http://127.0.0.1:5000/api/post -X get`
Consider the following code.
```python
from flask import Flask
app = Flask(__name__)
@app.route('/')
def generate1():
return "This is generate1"
@app.route('//')
def generate2():
return "This is generate2"
@app.errorhandler(404)
def page_not_found(e):
# setting 404 status explicitly
return 'page not found'
app.run()
```
If the flask application is running on http://127.0.0.1:5000, what will browser render for URL http://127.0.0.1:5000//
In the code snippet given below, what should come in place of `code 1` and `code 2` such that one parent can have multiple children and the converse does not hold true?
```python
from sqlalchemy import ForeignKey
from sqlalchemy import Integer, Column
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import relationship
class Base(DeclarativeBase):
pass
class Parent(Base):
__tablename__ = "parent_table"
id = Column(Integer, primary_key=True)
# write your code 1 here
class Child(Base):
__tablename__ = "child_table"
id = Column(Integer, primary_key=True)
# write your code 2 here
```
```python
code 1: parent_id=Column(Integer, ForeignKey("child_table.id"))
code 2: children = relationship("Parent")
```
```python
code 1: children = relationship("Child")
code 2: parent_id=Column(Integer, ForeignKey("parent_table.id"))
```
```python
code 1: parent_id=Column(Integer, ForeignKey("parent_table.id"))
code 2: children = relationship("Child")
```
```python
code 1: children = relationship("Parent")
code 2: parent_id=Column(Integer, ForeignKey("child_table.id"))
```
Consider the following models Brand and Cellphone corresponding to tables brand and cellphone in SQLite database.
```python
class Brand(db.Model):
id = db.Column(db.Integer(), primary_key = True)
name = db.Column(db.String(), unique = True)
class Cellphone(db.Model):
id = db.Column(db.Integer(), primary_key = True)
name = db.Column(db.String(), unique = True)
brand = db.Column(db.Integer(), unique = True, db.ForeignKey("brand.id"))
```
Based on the model schemas, what relationship do the classes Brand and Cellphone share?
Consider the schema for the Class Student.
```sql
CREATE TABLE "student" (
"s_id" INTEGER,
"roll_number" TEXT NOT NULL UNIQUE,
"first_name" TEXT NOT NULL,
"last_name" TEXT NOT NULL,
PRIMARY KEY("s_id" AUTOINCREMENT)
);
```
What will be the output of the flask_sqlalchemy command given below?
```
>>> s1 = Student(roll_number = M01, first_name = "Yash", last_name = "Raj")
>>> db.session.add(s1)
>>> s2 = Student(roll_number = M02, first_name = "Yash", last_name = "Maurya")
>>> db.session.add(s2)
>>> s3 = Student(roll_number = M03, first_name = "Ansh", last_name = "Raj")
>>> db.session.add(s3)
>>> db.session.commit()
>>> user1= Student.query.filter_by(first_name="Yash").first()
>>> user1.first_name= "Ansh"
>>> user1.last_name= "Maurya"
>>> db.session.commit()
>>> s1 = Student.query.all()
>>> for student in s1:
print(student.first_name)
print(student.last_name)
```
Yash Raj Yash Maurya Ansh Raj

Ansh Maurya Yash Maurya Ansh Raj

Ansh Maurya Ansh Maurya Ansh Raj

None

Consider the following route in the flask for a signup page and select the correct option.
```python
@app.route('/signup', methods=['GET', 'POST'])
def signup():
if request.method == 'GET':
return """<form action='/signup' method='POST'>
<label for='username'>Username</label>
<input type='text' name='username' required>
<label for='password'>Password</label>
<input type='text' name='password' required minlength="8">
<input type='submit' value='Submit'>
</form>
"""
if request.method == 'POST':
if request.form.get('username') is None:
return redirect(url_for(signup))
if request.form.get('password') is None:
return redirect(url_for(signup))
if len(request.form.get('password')) < 8:
return redirect(url_for(signup))
return f"<h1>Welcome, {request.form.get('username')}!</h1>"
```
Consider the following flask app. Given that test_request_context() allows text to be printed on the terminal, which of the following statements is/are correct?
```python
from flask import Flask, url_for
app = Flask(__name__)
@app.route('/home')
def index():
return 'Mad-I welcomes you!'
@app.route('/user/<username>')
def profile(username):
return f'{username}\'s profile'
with app.test_request_context():
#== print statement ==#
```
If `print statement` is replaced by `print(url_for('home'))`,
the output on the terminal will be Mad-I welcomes you!.
If `print statement` is replaced by `print(url_for('profile', username='Harry'))`, the output on the terminal will be `/user/Harry`
If `print statement` is replaced by `print(url_for('profile', username='Harry', next='course'))`, the output on the terminal will be
`/user/Harry/course`
If `print statement` is replaced by `print(url_for('index', username='Harry'))`, the output on the terminal will be
`/home?username=Harry`
```json
{
"Age": 27,
"firstName": "John",
"lastName": "Pollard",
"married": false,
"phone_numbers": ["212-555-1234", "212-666-5678"]
}
```
```json
{
"RollNumber": 11,
"firstName": "Nick",
"lastName": "Paul",
"phone_numbers": [2125551234, "2124441234"]
}
```
```json
{
"firstName": "Will",
"lastName": "Smith",
"address": [
{
"addressLine": "Lake Union Hill Way",
"city": "Atlanta",
"zipCode": 50005
}
]
}
```
```json
{
"firstName": "Hannah",
"lastName": "Smith",
"cities": ["Dallas", "San Antonio", "Irving"],
"phone_numbers": ["212-555-1234", "212-666-5678"]
}
```
Consider the following flask application.
```python
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def out():
val = request.args
if val['num'] == '':
return "<html><body> <h1>Invalid number</h1></body></html>"
elif val['num'].isalpha()==True:
return "<html><body><h1>Enter a valid number</h1></body></html>"
else:
out = int(val['num']) * int(val['num'])
return f'<html><body> <h1>The output is {out}</h1></body></html>'
if __name__ == "__main__":
app.run(debug=True)
```
If this flask app is running locally on http://localhost:5000, then which of the following statements is/are correct?
For URL: http://localhost:5000/?num=abc Output is: **Invalid number**

For URL: http://localhost:5000/?num=abc Output is: **Enter a valid number**

For URL: http://localhost:5000/?num= Output is: **Invalid number**

For URL: http://localhost:5000/?num=12 Output is: **The output is 1212**

Consider the following flask application.
```python
from flask import Flask, redirect, url_for
app = Flask(__name__)
@app.route('/admin')
def hello_admin():
return 'Hello Admin'
@app.route('/guest/<guest>')
def hello_guest(guest):
return 'Hello ' +guest+ ' as Guest'
@app.route('/user/<name>')
def hello_user(name):
if name == 'admin':
return redirect(url_for('hello_admin'))
else:
return redirect(url_for('hello_guest', guest = name))
if __name__ == '__main__':
app.run(debug = True)
```
If this flask app is running locally on http://localhost:5000, what is the output for the following URL?
For input: http://localhost:5000/user/admin?guest=appdev1