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.
http:// in URLs.URL Structure:
http://localhost:5000/ → http (protocol), localhost (host), 5000 (port), / (path).5000 for Flask), custom ports (e.g., app.run(port=5008)).HTTP Methods:
GET, POST, PUT, DELETE (Flask route handling).POST to a GET-only route).Flask route with methods:
@app.route('/api/data', methods=['GET', 'PUT', 'DELETE'])404 Trigger Example:
@app.route('/profile/<username>/active') # Missing `/<year>` → 404 for `/profile/user123/active/2024`Routing:
@app.route('/path')).@app.route('/user/<username>')).?key=value accessed via request.args).Request Handling:
request.method: Check if GET, POST, etc.request.form: Access form data (for POST).request.args: Access URL query parameters.Template Rendering:
render_template(): Render HTML templates (Jinja2).render_template_string(): Render a template from a string.Running the App:
app.run(port=5008, debug=True): Start the server on a custom port.Query Parameters:
@app.route('/user')
def profile():
username = request.args.get('username') # Access ?username=mad1Form Handling:
@app.route('/', methods=['GET', 'POST'])
def home():
if request.method == 'POST':
name = request.form['name'] # From <input name="name">Template Rendering:
return render_template_string("Hello, {{ name }}", name=name)Syntax:
{{ variable }}.{{ list|length }} (not len()).{% if %}, {% for %}.String vs. Jinja2 Templates:
{{ }} for variables.$var or ${var}.Jinja2 Filter:
{{ courses | length }} # Correct (not `len` or `len()`)String Template:
from string import Template
t = Template("Hello, $name")
t.substitute(name="Alice")Block vs. Inline Elements:
<div>, <p> (new line, full width).<span>, <strong>, <em> (flow with text).Box Model:
width + padding + border (exclude margin).Example:
div {
width: 200px;
padding: 15px;
border: 5px solid black;
}
→ Total width = 200 + (15*2) + (5*2) = 240px.
Box Model Calculation:
Total Width = width + (padding × 2) + (border × 2)sys.argv:
sys.argv[0]: Script name.sys.argv[1:]: Command-line arguments.Module Imports:
utils.py prints when imported).Command-Line Args:
# python main.py data.txt output.txt
print(sys.argv[0]) # "main.py"
print(sys.argv[1]) # "data.txt"
print(sys.argv[2]) # "output.txt"Flask Errors:
method='GET' → Should be methods=['GET']./user/<id> but no id passed).Template Errors:
len() vs. |length).Correct Route Syntax:
@app.route('/login', methods=['GET']) # Not `method='GET'`Flask:
@app.route, dynamic routes).request object (args, form, method).render_template, render_template_string).methods= vs method=).Jinja2:
{{ }}, {% %}, filters like |length).string.Template.HTML/CSS:
Python:
sys.argv for command-line args.HTTP:
GET, POST, etc.).