Home / Past papers / MAD1 Quiz 1 / this paper

```
{{ courses | len }}
```
```jinja
{{ courses | length }}
```
```html
{{ len(courses) }}
```
None of these

Consider the following code:
app.py
```python
from flask import Flask
app = Flask(__name__)
@app.route("/")
def port():
return "This is the port question."
if __name__ == "__main__":
app.run(port=5008)
```
You are running a Flask web application locally using the command: python app.py. On which URL will the application be accessible on the browser?
http://127.0.0.1:5000/

http://localhost:0000/

http://0.0.0.0:5000/

http://localhost:5008/

Consider the following statements and select the correct option. Statement 1: The `<span>` elements will start on new lines, while the `<div>` elements will appear inline with surrounding content. Statement 2: The `<p>` and `<div>` elements will each start on new lines and take up the full width of their container, while `<span>`, `<strong>`, and `<em>` will flow inline with surrounding content.

Which of the following Flask view functions will return a 404 error for the URL: ``` http://127.0.0.1:5000/profile/user123/active/2024? ```

```python
@app.route('/profile/<username>/<status>/<int:year>')
def profile(username, status, year):
info = {'username': username, 'status': status, 'year': year}
return info
```
```python
@app.route('/profile/<string:username>/<string:status>/<string:year>')
def profile(username, status, year):
info = {'username': username, 'status': status, 'year': year}
return info
```
```python
@app.route('/profile/<username>/<status>/<year>')
def profile(username, status, year):
info = {'username': username, 'status': status, 'year': year}
return info
```
```python
@app.route('/profile/user/<username>/<status>/<year>')
def profile(username, status, year):
info = {'username': username, 'status': status, 'year': year}
return info
```
Consider two Python files, main.py and utils.py with the following code snippets.
File1: main.py
```python
import sys
import utils
print('Processing: ' + sys.argv[2])
```
File2: utils.py
```python
import sys
print('Module: ' + sys.argv[0])
```
What is the output of the following command "python main.py data.txt output.txt" when run on terminal?
``` Module: utils.py Processing: data.txt ```

``` Processing: data.txt Module: utils.py ```

``` Module: main.py Processing: output.txt ```

``` Processing: output.txt Module: main.py ```

Match the following web development concepts with their correct descriptions: | Column A | Column B | |---|---| | 1. PyHTML | A. Lightweight engine in Python for embedding variables inside strings using `$variable` style | | 2. Jinja2 | B. A Python library to build HTML structures programmatically using Python functions | | 3. Template Rendering | C. The process of generating dynamic HTML by combining templates with data | | 4. String Template Rendering | D. Templating engine used by Flask that supports control structures, filters, and inheritance |

Consider the following Python code snippet.
```python
from jinja2 import Template as JTemplate
from string import Template as STemplate
data = {'name1': 'Alice', 'name2': 'name1',
'age1': '25', 'age2': 'age1',
'job1': 'Engineer', 'job2': 'job1'}
jt = JTemplate("{{name2}} is {{age2}} years old and works as a {{job2}}.")
result1 = jt.render(data)
print(result1) #first print statement
st = STemplate(result1)
result2 = st.substitute(data)
print(result2) #second print statement
```
If the above Python code runs on the terminal, which of the following statements is/are correct?
The first print statement will output: "name1 is age1 years old and works as a job1."

The second print statement will output: "Alice is 25 years old and works as a Engineer."

Jinja2 templates use double curly braces {{}} for variable substitution
String templates use dollar signs $ for variable substitution in the substitute() method

The code will raise a KeyError exception during execution

Which of the following Flask view functions will NOT throw any error for the URL: `http://127.0.0.1:5000/user?username=mad1&role=admin?`

```python
@app.route('/user')
def profile():
creds = request.args
info = {'username': creds["username"], 'role': creds["role"]}
return info
```
```python
@app.route('/user/<username>/<role>')
def profile(username, role):
info = {'username': username, 'role': role}
return info
```
```python
@app.route('/user')
def profile():
return {'message': 'data retrieved successfully'}
```
All of these

Consider the following HTML and CSS code:
```html
<html>
<head>
<style>
div {
width: 0px;
margin: 0px;
}
.container {
width: 200px;
padding: 15px;
border: 5px solid black;
}
#box {
margin: 10px;
}
</style>
</head>
<body>
<div class="container" id="box">Hello World</div>
</body>
</html>
```
What is the **total width (approximately)** of the `div` with `class="container"` and `id="box"`, including **content, padding, and border** but excluding margin?
Consider the following code:
```python
from flask import Flask, request, render_template_string
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def home():
if request.method == 'POST':
name = request.form['name']
template = "Hello, {{ name }}!"
return render_template_string(template, name=name)
return '''
<form method="POST">
Enter Your Name: <input type="text" name="name">
<button>Submit</button>
</form>
'''
if __name__ == '__main__':
app.run(debug=True)
```
You are building a Flask web application that takes user input from an HTML form and displays it using Jinja2 templates. Suppose the user runs this app locally and submits the name **Alice** in the form.
Which of the following represents the correct sequence of concepts that occur when the user submits the form with the name **Alice** ?
HTML Form Rendering → HTTP 404 Not Found Error → User Submits Form → Flask Routing → Template Rendering

User Submits Form (HTTP POST) → Check URL Structure → Network Transfer (Bandwidth & Latency) → Flask Routing

User Accesses URL (Form at "/") → User Submits Form (HTTP POST) → Flask Request Handling (request.form) → Jinja2 Template Rendering

Flask Request Handling → Network Round Trip (RTT) → HTML Table Rendering → Jinja2 Template Rendering

Consider the web URL. http://localhost:5000/ Which of the following is/are **True**?

http is the protocol.

5000 is the port number.

The trailing / represents the base URL.

5000 is the status code.

Consider the following HTML snippet: ```html <img src="logo.png" alt="Company Logo" /> ``` Which of the following is true?

If logo.png fails to load, the text "Company Logo" will be shown.

If the image file is missing, browsers display it as a broken image.

The alt attribute is mandatory in HTML5.

Logo.png fails to load because the `<img>` tag only allows "jpeg/jpg" files.


If the given Flask app is run using the command ``` python server.py GET PUT POST ``` in one terminal, what will be the output on another terminal for the command ``` curl -X POST http://127.0.0.1:5000/api/data? ```

```html <h1>Data added successfully</h1> ```

```html <h1>Unsupported operation</h1> ```

```html <h1>Method not Allowed</h1> ```

```html <h1>404 Not Found</h1> ```

If the given Flask app is run using the command ``` python server.py GET PUT POST ``` in one terminal, what will be the output on another terminal for the command ``` curl -X DELETE http://127.0.0.1:5000/api/data? ```

```html <h1>Data deleted successfully</h1> ```

```html <h1>Unsupported operation</h1> ```

```html <h1>Method not Allowed</h1> ```

```html <h1>404 Not Found</h1> ```


method="GET" is not correct, it should be methods=["GET"]

```python def login(id) ``` can throw error because no id is passed in, /login url

name will throw error because no name will be written in the starting of app

There is no error, Flask application will run correctly.

Assume the **terminal error** (so the Flask server starts) has already been **fixed**. Now, if you hit the `/login` URL in the browser, which of the following is the correct option in this case?

method="GET" is not correct, it should be methods=["GET"]

```python def login(id) can throw error because no id is passed in /login url ```

name will throw error because no name will be written in the starting of app

There is no error, Flask application will run correctly.
Auto-generated summary from the source site. Handy as a checklist; the lessons above explain each idea from scratch.
Here’s a structured summary of the core topics, concepts, principles, and key takeaways from the exam questions, followed by Mermaid knowledge graphs to visualize relationships.
Routing: Defining URL endpoints using @app.route().
@app.route('/path', methods=['GET', 'POST', ...])<variable_type:variable_name> (e.g., <int:year>, <string:username>).HTTP Methods: GET, POST, PUT, DELETE.
GET: Retrieve data (query parameters via request.args).POST: Submit data (form data via request.form).Request Handling:
request.args.get('key').request.form['key'].Running Flask Apps:
5000.app.run(port=5008).http://localhost:<port>/route.Templates:
Jinja2: Flask’s default templating engine.
{{ variable }}, {% control %}.{{ list|length }} (not len).${variable} (via string.Template).render_template('file.html', **context).methods in @app.route (should be methods=['GET'], not method='GET').<int:month> vs. a string input)./login/<id> but no id provided).Block vs. Inline Elements:
<div>, <p> (start on new lines, full width).<span>, <strong>, <em> (flow with content).Box Model:
width + padding + border (margin excluded).Example:
.container { width: 200px; padding: 15px; border: 5px; }
Total width = 200 + (15*2) + (5*2) = 240px.
Images:
alt attribute: Fallback text if image fails to load (mandatory in HTML5).src is invalid.| Feature | Jinja2 | String Template | |
|---|---|---|---|
| Syntax | {{ variable }} | ${variable} | |
| Filters | `{{ list | length }}` | No built-in filters |
| Control Structures | {% if %}, {% for %} | No control structures | |
| Usage in Flask | Default (via render_template) | Requires string.Template |
URL Structure: protocol://hostname:port/path?query=parameters
Example: http://localhost:5000/user?username=mad1
http5000username=mad1, role=admin (accessed via request.args).HTTP Methods in Flask:
GET: Retrieve data (query strings).POST: Submit data (form data).PUT/DELETE: Update/delete resources.sys.argv: Command-line arguments.
sys.argv[0]: Script name.sys.argv[1:]: User-provided arguments.Import Order:
utils.py prints before main.py if imported first).Common Issues:
method='GET' vs. methods=['GET'])./login/<id> but no id passed).POST to a GET-only route returns 405 Method Not Allowed).Curl Testing:
curl -X POST http://localhost:5000/api/data tests POST requests.No complex math equations were present, but here are logical rules:
Flask Route Matching:
/route/<int:var> → var must be an integer, else 404./route?key=value → Access via request.args.get('key'). Total Width = width + padding-left + padding-right + border-left + border-right ``Jinja2 Filter:
{{ list|length }} → Returns the length of list.