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.
Based on the provided question paper for "Diploma Level: Modern Application Development I", the core topics, concepts, and principles revolve around web development (Flask, Jinja2, HTML/CSS, JavaScript), debugging, and syntax validation. Below is a structured summary of the key areas to focus on for the exam, followed by a Mermaid knowledge graph to visualize relationships.
@app.route), handling HTTP methods (GET, POST).Templates (Jinja2):
{{ variable }}), loops ({% for %}), conditionals ({% if %}).{{ courses|length }} to show the number of courses).{% extends %}, {% block %}).Error Handling:
app.run(), incorrect template paths, undefined variables).url_for('static', filename='style.css')).Request/Response Cycle:
request.form), query parameters (request.args).render_template, redirect, jsonify).Syntax:
{{ variable }}{{ name|upper }}Control structures:
{% if user %} ... {% endif %}
{% for item in items %} ... {% endfor %}Common Use Cases:
width, height, padding, border, margin.px, %, em, rem, vw/vh.DOM Manipulation:
document.getElementById, querySelector).addEventListener).Asynchronous Operations:
fetch() for API calls.then, catch).ES6 Features:
let/const, template literals.Flask Errors:
TemplateNotFound: Missing template file.NameError: Undefined variable in template.SyntaxError: Incorrect Python/Jinja2 syntax.Terminal Output:
app = Flask(__name__)).Browser DevTools:
Matching Code to Output:
Correcting Syntax:
Algorithmic Thinking:
Since this is a web development exam, there are no mathematical equations. Instead, focus on code syntax and logical structures:
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def home():
courses = ["Math", "Science", "History"]
return render_template("index.html", courses=courses)
if __name__ == "__main__":
app.run(debug=True)
<!-- Display number of courses -->
<p>Total courses: {{ courses|length }}</p>
<!-- Loop through courses -->
<ul>
{% for course in courses %}
<li>{{ course }}</li>
{% endfor %}
</ul>
.box {
width: 200px; /* Content width */
padding: 20px; /* Space inside box */
border: 5px solid black; /* Border around padding */
margin: 10px; /* Space outside box */
/* Total width = 200 + 2*20 + 2*5 + 2*10 = 250px */
}
fetch('/api/data')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
Flask/Jinja2:
{% %}, {{ }}).Debugging:
TemplateNotFound, NameError).HTML/CSS:
JavaScript:
fetch() for API calls.Code Snippets: