Question 1
Which of the following correctly represents the components of the given URL?
The IIT Madras BS Modern Application Development I (MAD 1) End Term paper sat on 7 Aug 2022, in the May 2022 term: 32 questions for 100 marks in 180 minutes. Every question is below with its answer. Take it as a timed mock test to be marked, or read it through first.
Which of the following correctly represents the components of the given URL?
Correct answer
Consider the following flask app and an HTML file in templates folder:
Python file: app.py
from flask import Flask, render_templateapp = Flask(__name__)
my_list = ['Web development','onlinedegree','cs2003', 'MAD-I','Data_science']
@app.route('/')def render(): return render_template('index.html', my_list = my_list)
app.run(debug = True)Template file:
<!DOCTYPE html><head> <style> body{width: 200px; border: 2px solid black} #one{color:red;} #two{color:blue;} </style></head><body> {% for item in my_list %} {% set Length = item|length %} {% if Length%2 == 0 %} <h3 id = "one">{{ item }}</h3> {% else %} <h3 id = "two">{{ item }}</h3> {% endif %} {% endfor %}</body>If the above flask app is running locally on http://127.0.0.1:5000/, what will be rendered by the browser for the base URL?
Correct answer
Consider the flask code given below.
Python file: app.py
from flask import Flask, jsonify, request
app = Flask(__name__)
my_shops= [ { 'name of the shop' : 'Grocery', 'items' : [ { 'item1' : 'Toothpaste', 'item2' : 'Snacks', 'item3' : 'Biscuits', 'item4' : 'Soaps' } ] }]@app.route('/')def show_shop(): return jsonify({"shops" : my_shops})
#====================== CODE HERE#======================
if __name__ == '__main__': app.run()Which of the following code snippets must be added in the given space of above application, in order to create a new shop in ‘my_shops’ list on the server side apart from the existing one?
Correct answer
A table ‘person’ is created in the database using model class “Person” with fields and their properties given in the table below.
Correct answer
A flask app and a template files are given below.
Python file: app.py
from flask import Flask, render_templateapp = Flask(__name__)
@app.route('/home')def HomePage(): return "Welcome, folks! This is the Home Page!"
@app.route('/about')def AboutPage(): users = [ {"user":"Shobhit","gender" : "Male" , "age" : 23, "score": 90}, {"user":"Deepak", "gender" : "Male" , "age" : 17, "score": 88}, {"user":"Nikita", "gender" : "Female" , "age" : 20, "score": 87} ] return render_template('home.html', condition=True, users=users)
if __name__ == "__main__": app.run(debug=True)Templates file: home.html
<!DOCTYPE html><html><body> <p><a href="{{ url_for('HomePage') }}">Go back to home page?</a></p><h2>About page</h2>{% if condition %} <h3> You are landed on about page.</h3> {% for user in users %} <ul> <li>Username : {{user.user}}, Age : {{user.age}}, Gender : {{user.gender}}, Score : {{user.score}}</li> </ul> {% endfor %}{% else %} <h3> Please Go back.</h3>{% endif %}</body></html>If the above flask application is running locally on “http://127.0.0.1:5000”, which of the following statement is true?
Correct answer
An HTML document is given below.
<!DOCTYPE html><html> <body> <h1 id="id1">Welcome to IITM</h1> <h3 class="class1">Welcome to the world's first online degree program.</h3> <a href="">Go back to main link</a> <p class="class1">Lorem ipsum dolor sit amet consectetur adipisicing elit. Earum, rerum?</p> <p class="class1">Have you enrolled in BSC in Data science and Programming? </p> <p id="id2">Go to the IITM online degree website and enroll now!</p> </body></html>Suppose, if we want to give red color to the text within the heading element having id="id1” and green color to the text within the heading element having class="class1”, what will be the correct way to do that?
Correct answer
Consider the following HTML document with internal CSS.
<!DOCTYPE html><html> <head> <style type="text/css"> *{ margin: 0px; width: 253px; } div{ margin: 10px; padding: 20px; border-style: dotted; border-width: 10px; font-size: 30px; color: blue; background-color: pink; border-color: red; } </style> <title>End Sem</title> </head> <body> <div>This is my content</div> <div>Another content</div> </body></html>How will the browser render the above HTML document?
Correct answer
Correct answers
How will the browser render the output of the following Python code snippet?
from jinja2 import Template
styles=[ '.text{color: purple}\n #heading{color:red}\n #subhead{color:blue}', '.text{color: purple}\n #subhead{color:green}\n #main{color:blue}', '.text{color: purple}\n #main{color:red}\n #heading{color:blue}' ]
template = """ <!DOCTYPE html> <style> div{border: 2px solid black; width: 300px; background-color: rgb(247, 247, 230)} {{styles[0]}} </style> <body> <div> <h2 style="color:brown;" class="text" id="heading">Programming Degree</h2> <h3 class="text" id="subhead">Modern Application 1</h3> <p class="text" id="main">This is a course on ApplicationDevelopment</p> </div> </body> """test_render = Template(template)output = test_render.render(styles = styles)print(output)Correct answer
What will be the output of the following python code if method test_request_context() allows flask app to print statements on the terminal?
from flask import Flask, url_for
app = Flask(__name__)
@app.route('/')def home(): return 'base url'
@app.route('/subscribe')def subscribe(): return 'Please subscribe to this page.'
@app.route('/new_course/<coursename>')def course(coursename): return f'The course {coursename} gives basics of web development.'
with app.test_request_context(): print(url_for('home')) print(url_for('subscribe')) print(url_for('subscribe', username = 'user_one')) print(url_for('course', coursename = 'MAD_I'))Correct answer
An internet connection with certain bandwidth is able to serve 10,000 requests of 150 Kilobytes each. What should be the increase in bandwidth (in Gbps) if this internet connection is to handle 12,500 requests of 180 Kilobytes each? (Use relations: 1 Byte = 8 bits, 1 MB = 1000 B, 1 GB = 1000 M and so on)
600
6
0.6
12
Correct answer
6
Consider the code snippet given below.
Python file: test_app.py
import pytest
@pytest.fixturedef items(): return "Books"
@pytest.fixturedef order(): return "Pens"
@pytest.fixturedef order_items(order, items): return [order, items]
@pytest.fixturedef expected_list(): return ["Books", "Pencils", "Pens"]
def test_1(order_items, expected_list): order_items.append("Pencils") assert order_items == expected_list
def test_2(order_items): order_items.append("Pencils") assert order_items == ["Pens", "Books", "Pencils"]Which of the following statement is true about the above code snippet?
After running pytest, test_1 will fail, whereas test_2 will pass.
After running pytest, test_2 will fail, whereas test_1 will pass.
Both the test cases, test_1 and test_2 will pass successfully.
Both the test cases, test_1 and test_2 will fail.
Correct answer
After running pytest, test_1 will fail, whereas test_2 will pass.
Consider the Python code snippet given below.
Python file: app.py
from flask import Flask, requestfrom flask_restful import Api, Resource, reqparse
app = Flask(__name__)api = Api(app)
class Add(Resource): def post(self): data_args = reqparse.RequestParser() data_args.add_argument('Name', help='Name is required', required =True) data_args.add_argument('Age', help='Age is required', required =True) args = data_args.parse_args() return { "Your Name": args['Name'], "Your Age" : args['Age']}
api.add_resource(Add, '/add')
if __name__ == '__main__': app.run(debug=True)If this flask application is running on http://127.0.0.1:5000, which of the following is the correct output when a POST request is sent to URL "http://127.0.0.1:5000/add"?
Correct answer
Consider the Python code given below.
import pytest
@pytest.fixturedef first_entry(): return "Apple"
@pytest.fixturedef order(first_entry): return [first_entry]
def test_string(order): order.append("Kiwi") assert order == ["Banana", "Apple"]
def test_int(order): order.append("Banana") assert order == ["Banana", "Apple", "Kiwi"]Which of the following statement is true?
After running pytest, both test cases will pass successfuly.
After running pytest, the first test case will fail, whereas the second test case will pass.
After running pytest, both test cases will show a failure report.
None of these
Correct answer
After running pytest, both test cases will show a failure report.
Consider the flask app given below.
from flask import Flask, abortfrom flask_restful import Resource, Apiapp = Flask(__name__)api = Api(app)
item_list=[{"item1": "Cloths"}, {"item2" : "Shoes"}, {"item3" : "Sunglasses"}]
class ItemList(Resource): def get(self, item_no, item_name): this_item = {'item'+item_no : item_name} if this_item in item_list: return item_list, 200 else: abort('400')
def post(self, item_no, item_name): my_item = {'item'+item_no : item_name} item_list.append(my_item) return my_item, 201
api.add_resource(ItemList, '/items/<item_no>/<item_name>')app.run(debug=True)If the above flask application is running locally on “http://127.0.0.1:5000”, what will be the output of a GET request sent to the URL: ‘http://127.0.0.1:5000/items/4/watch’ just after a POST request that is sent on the same URL?
Correct answer
Consider the following flask application.
from flask import Flask, render_template
app=Flask(__name__)
@app.route('/')def home(): my_items = ['Cake', 'Apple', 'Ice Cream', 'DarkChocolate','Donut', 'Grape'] l1 = [] for i in range(len(my_items)): if i>2: l1.append(my_items[i])
return render_template('index.html', list=l1)
app.run(debug=True)Template File - index.html
{% macro display(list) %} {% for item in list %} <p>{{ item }}</p> {% endfor %}{% endmacro %}<html> <body> {{ display(list) }} </body></html>suppose the application is running locally on the ‘http://127.0.0.1:5000', then what will be rendered by the browser?
Cake
Apple
Ice Cream
Cake
Apple
Ice Cream
DarkChocolate
Donut
Grape
DarkChocolate
Donut
Grape
Ice Cream
DarkChocolate
Donut
Grape
Correct answer
DarkChocolate
Donut
Grape
Consider the following Python code snippets.
File 1: main.py
import sysfrom new import funa = sys.argv[1]b = sys.argv[2]c = sys.argv[3]result = fun(a, b, c)print(result + " is greater")File 2: new.py
def fun(num1,num2,num3): if (num1 > num2) and (num1 > num3): return num1 elif (num2 > num1) and (num2 > num3): return num2 else: return num3suppose the main.py file is executed in the terminal. What will be the output?
python main.pypython main.py 8 10 5IndexError: list index out of range
NameError: name 'fun' is not defined
3 is greater
NameError: name 'fun' is not defined
IndexError: list index out of range
10 is greater
NameError: name 'fun' is not defined
8 is greater
Correct answer
IndexError: list index out of range
10 is greater
Consider a server that has an Intel i5 processor, 64 GB RAM, 1 TB Hard disk with 3 Gbps network connection. If a client accesses a web page, it requires 1.5 MB. Calculate the maximum number of such requests per second the server can handle. (Use relations: 1 Byte = 8 bits, 1 MB = 1000 B, 1 GB = 1000 M and so on).
25
32
250
200
Correct answer
250
Consider the following Python code snippet.
from flask import Flask, abort, redirect, url_for, render_template
app = Flask(__name__)
weekday_users = ['user_1','user_3','user_4','user_6','user_7']weekend_users = ['user_2','user_5']
@app.route('/weekday/<username>')def user_weekday(username): if username in weekday_users: return redirect(url_for('login', username = username)) else: abort(401)
@app.route('/weekend/<username>')def user_weekend(username): if username in weekend_users: return redirect(url_for('login', username = username)) else: abort(401)
@app.route('/login/<username>')def login(username): return f"<h2>Correct User Found! {username}</h2>"@app.errorhandler(401)def page_not_found(error): return "<h2>You are not authorized for this day.</h2>", 401
app.run()If the above flask app is running locally on “http://127.0.0.1:5000”, Which of the following statements is/are correct?
Correct answers
Consider the code given below.
from flask import Flask, requestfrom flask_restful import Api, Resource, reqparse
app = Flask(__name__)
api = Api(app)
Mytasks = { 1: {"mytask": "Studying"}, 2: {"mytask": "Exercise"}, 3: {"mytask": "Eating"}, 4: {"mytask": "Sleeping"}}class Display(Resource): def get(self): return Mytasks
class DisplayAll(Resource): def get(self, MytaskList_id): return Mytasks[MytaskList_id]
def post(self, MytaskList_id): data_args = reqparse.RequestParser() data_args.add_argument("mytask",help='This is requiredfield', required =True) args = data_args.parse_args() Mytasks[MytaskList_id] = {"mytask" : args["mytask"]} return Mytasks[MytaskList_id]api.add_resource(Display, '/mytask')api.add_resource(DisplayAll, '/task/<int:MytaskList_id>')
if __name__ == '__main__': app.run(debug=True)If the above flask application is running locally on “http://127.0.0.1:5000”, which of the following statements is/are true?
Correct answers
What will be the decimal representation of binary number 0101011000000112?
Correct answer: 11011
A machine takes a minimum of 100 seconds to sort 500 entries in a database. What will be the approximate minimum time taken by the machine to sort 1200 entries if the sorting method employs an algorithm with time complexity of O(nlog(n)). Where “n” is the number of entries?
173 seconds
273 seconds
373 seconds
473 seconds
Correct answer
273 seconds
The speed vs. throughput characteristics of a typical HDD is shown in the figure below. If this HDD is to be used as a replacement of an SSD whose read/write speed is 450 MB/s. At what speed (in RPM) should the disk of HDD rotate with to deliver the same performance as that of the SSD?
3600 RPM
7200 RPM
16,200 RPM
28,800 RPM
Correct answer
28,800 RPM
Consider the following Flask app and an HTML file.
Flask app: app.py
from flask import Flask, render_template, requestapp = Flask(__name__)users = { '3':{'name': 'Ram', 'Designation': 'Teacher'}, '2':{'name': 'Dilip', 'Designation': 'student'}, '5':{'name': 'Sonu', 'Designation': 'computer operator'}, '1':{'name':'Guru', 'Designation': 'clerk'} }@app.route('/')def country(): id = request.args.get('id') authenticated_users_id = [3, 2, 5] user = users.get(id) name = user.get('name') if user is not None else None Designation = user.get('Designation') if user is not None elseNone user = {'is_authenticated': False, 'name': name, 'Designation':Designation} if int(id) in authenticated_users_id: user['is_authenticated'] = True return render_template('index.html', data = user) if int(id) not in authenticated_users_id: user['is_authenticated'] = False return render_template('index.html', data = user)
app.run(debug = True)HTML File: index.html
<!DOCTYPE html><html lang="en"> <head> <title>Document</title> </head> <body> {% if data.name == None %} User not found {% elif data.is_authenticated == True %} Hello {{data.name}} you can enter into this site:{{data.Designation}} {% else %} Hello {{data.name}} you have no access to this site:{{data.Designation}} {% endif %} </body></html>Suppose the application is running locally on the ‘http://127.0.0.1:5000', then what will be rendered by the browser for ‘http://127.0.0.1:5000/?id=5', http://127.0.0.1:5000/?id=1’ and ‘http://127.0.0.1:5000/?id=4' respectively?
Hello Sonu you can enter into this site: computer operator
Hello Guru you have no access to this site: clerk
User not found
Hello Ram you can enter into this site: Teacher
Hello Guru you have no access to this site: Student
Hello Sonu you can enter into this site: computer operator
User not found
Hello Dilip you have no access to this site: Student
Hello Sonu you can enter into this site: computer operator
Hello Ram you can enter into this site: Teacher
Hello Dilip you have no access to this site: Student
User not found
Correct answer
Hello Sonu you can enter into this site: computer operator
Hello Guru you have no access to this site: clerk
User not found
Consider the following Python code snippet.
from jinja2 import Template
temp = """{% set numbers = studs | map(attribute = "mark") | list %} {{numbers | min}} {{numbers | max}}"""studs = [{"stud_name":"Reeta","mark":"92"},{"stud_name":"Veena","mark":"88"},{"stud_name":"Meena","mark":"62"},{"stud_name":"uma","mark":"98"}]t1 = """{% for i in studs -%} {{i}} {%- endfor%}"""
output = Template(temp)out = Template(t1)print(output.render(studs = studs))print(out.render(studs = studs))What will be the output of the above program?
98 62
{'stud_name': 'Reeta', 'mark': '92'}
{'stud_name': 'Veena', 'mark': '88'}
{'stud_name': 'Meena', 'mark': '62'}
{'stud_name': 'uma', 'mark': '98'}
62 98
{'stud_name': 'Reeta', 'mark': '92'}{'stud_name': 'Veena', 'mark': '88'}{'stud_name':
'Meena', 'mark': '62'}{'stud_name': 'uma', 'mark': '98'}
62 98
{'stud_name': 'uma', 'mark': '92'}
{'stud_name': 'Meena', 'mark': '88'}
{'stud_name': 'Veena', 'mark': '62'}
{'stud_name': 'Reeta', 'mark': '98'}
98 62
{'stud_name': 'uma', 'mark': '92'}{'stud_name': 'Meena', 'mark': '88'}{'stud_name':
'Veena', 'mark': '62'}{'stud_name': 'Reeta', 'mark': '98'}
Correct answer
62 98
{'stud_name': 'Reeta', 'mark': '92'}{'stud_name': 'Veena', 'mark': '88'}{'stud_name':
'Meena', 'mark': '62'}{'stud_name': 'uma', 'mark': '98'}
Consider the following table “newtable” in SQLite database.
Correct answer
Consider the following Python snippet.
Introduction
Code
Java is a powerful general purpose …
Java works on different platforms(windows)
Introduction - Java is a powerful general purpose …
learn python - Python is a powerful general purpose …
Basics - Java works on different platforms(windows)
Introduction
Basics
Correct answer
Introduction
Basics
Consider the HTML code given below.
To obtain the output as given in figure above, which of the following snippets of CSS code must be used?
Correct answers
Consider the following model classes “State” and “City” corresponding to tables “state” and “city” respectively in the SQLite database.
class State(db.Model): state_id = db.Column(db.Integer(), primary_key = True) state_name = db.Column(db.String(50), nullable = False) cities = db.relationship("City", backref = "stateof")
class City(db.Model): city_id = db.Column(db.Integer(), primary_key = True) city_name = db.Column(db.String(50), nullable = False) state = db.Column(db.Integer(), db.ForeignKey("state.state_id"))Based on the above data, answer the given subquestions.
Correct answers
Consider the following model classes “State” and “City” corresponding to tables “state” and “city” respectively in the SQLite database.
class State(db.Model): state_id = db.Column(db.Integer(), primary_key = True) state_name = db.Column(db.String(50), nullable = False) cities = db.relationship("City", backref = "stateof")
class City(db.Model): city_id = db.Column(db.Integer(), primary_key = True) city_name = db.Column(db.String(50), nullable = False) state = db.Column(db.Integer(), db.ForeignKey("state.state_id"))Based on the above data, answer the given subquestions.
If “s1” and “c1” are existing objects in the tables “state” and “city” respectively where:
Which of the following statements is/are correct?
Correct answers
Consider the following resource API for the employee information given below and answer the given subquestions.
from flask import Flaskfrom flask_restful import Resource, Api, reqparse, fields, marshal_with
app = Flask('__main__')api = Api(app)
parser = reqparse.RequestParser()parser.add_argument("first_name")parser.add_argument("last_name")parser.add_argument("role")parser.add_argument("salary", type=int, help='Salary must be an integer')out_fields_1 = {"first_name": fields.String,"role": fields.String}out_fields_2 = {"first_name": fields.String,"last_name": fields.String}out_fields_3 = {"first_name": fields.String, "salary": fields.Integer}
class MyApi(Resource): @marshal_with(out_fields_2) def get(self): info = parser.parse_args() return info
@marshal_with(out_fields_1) def post(self): info = parser.parse_args() return info
@marshal_with(out_fields_3) def put(self): info = parser.parse_args() return info
api.add_resource(MyApi, '/myinfo')app.run(debug = True)Correct answer
Consider the following resource API for the employee information given below and answer the given subquestions.
from flask import Flaskfrom flask_restful import Resource, Api, reqparse, fields, marshal_with
app = Flask('__main__')api = Api(app)
parser = reqparse.RequestParser()parser.add_argument("first_name")parser.add_argument("last_name")parser.add_argument("role")parser.add_argument("salary", type=int, help='Salary must be an integer')out_fields_1 = {"first_name": fields.String,"role": fields.String}out_fields_2 = {"first_name": fields.String,"last_name": fields.String}out_fields_3 = {"first_name": fields.String, "salary": fields.Integer}
class MyApi(Resource): @marshal_with(out_fields_2) def get(self): info = parser.parse_args() return info
@marshal_with(out_fields_1) def post(self): info = parser.parse_args() return info
@marshal_with(out_fields_3) def put(self): info = parser.parse_args() return info
api.add_resource(MyApi, '/myinfo')app.run(debug = True)If the flask application is running locally on URL “http://127.0.0.1:5000/myinfo”, what will be the output of the following Python code snippet?
import requests
data = {"first_name":"Rajnish", "last_name":"Dey", "role":"Manager", "salary":"10 thousand"}
response = requests.put('http://127.0.0.1:5000/myinfo', data = data)print(response.json())Correct answer