Templating & HTML Generation: past questions

10 questions · 33.5 marks · 7 papers. Try each one before checking.

Solve them and check answers →Read the notes first

24 Mar 2024

Question 7 MCQ · 3 marks

Consider the following Python code snippet.

from string import Template
text = "The $color car is parked in front of the $building."
template = Template(text)
print(=== OUTPUT ===)

Which of the following statements, when substituted in place of === OUTPUT ===, will throw a KeyError?

  1. template.substitute({'color':'red'})
  2. template.substitute({'color': 'blue', 'building':'office', 'place': 'Chennai'})
  3. template.safe_substitute({'color':'green'})
  4. template.safe_substitute({'color':'yellow', 'building':'apartment', 'place': 'Chennai'})

16 Mar 2025

Question 6 MCQ · 3 marks

Consider the HTML code given below: File name: base_layout.html

<!DOCTYPE html>
<html lang="en">
<head>
    <title>ABC App</title>
</head>
<body>
    <h3>Welcome to ABC App</h3>
    {% block abc %}{% endblock %}
</body>
</html>

Which of the following is the correct implementation of template inheritance?

  1. {% extends "base_layout.html" %} {% block abc %} <h4>Company Directors</h4> <ul> <li>Ram</li> <li>Rahim</li> <li>David</li> </ul> {% endblock %}
  2. {% extends "base_layout.html" %} {% block %} <h4>Company Directors</h4> <ul> <li>Ram</li> <li>Rahim</li> <li>David</li> </ul> {% endblock %}
  3. {% link template "base_layout.html" %} {% block abc %} <h4>Company Directors</h4> <ul> <li>Ram</li> <li>Rahim</li> <li>David</li> </ul> {% endblock %}
  4. {% block "base_layout.html" %} <h4>Company Directors</h4> <ul> <li>Ram</li> <li>Rahim</li> <li>David</li> </ul> {% endblock %}

03 Aug 2025

Question 8 MCQ · 3 marks

Consider the following Python code snippet.

from string import Template

msg = "The $lang programming language is $desc and $useful for web development."

template = Template(msg)

print(=== OUTPUT ===)

Which of the following statements, when substituted in place of === OUTPUT ===, will throw a KeyError?

  1. template.substitute({'lang': 'Python'})
  2. template.substitute({'lang': 'Python', 'desc': 'popular', 'useful': 'useful'})
  3. template.safe_substitute({'lang': 'Python'})
  4. template.safe_substitute({'lang': 'Python', 'desc': 'versatile'})

Question 6 MCQ · 3 marks

Consider the following Flask and HTML implementations. Flask file: main.py

from flask import Flask, render_template
app = Flask(__name__)

@app.route("/text/size/<int:size>/<color>")
def set_size(size, color):
    style = {"font-size": str(size) + "px", "color": color}
    return render_template("index.html", css_style=style)

app.run(debug=True)

HTML file: index.html

<!DOCTYPE html>
<html>
    <div style="color: {{css_style['color']}}; font-size:{{css_style['font-size']}};">
        IITM BS Degree Program
    </div>
</html>

Assume that the Flask code is running on the server http://127.0.0.1:5000 without any errors. Choose the correct URL to render the output below.

[Image Description: Rendered text "IITM BS Degree Program" displayed in red font color at approximately 20px font size.]

  1. http://127.0.0.1:5000?text=red&size=20
  2. http://127.0.0.1:5000?size=20&text=red
  3. http://127.0.0.1:5000/text/size/red/20
  4. http://127.0.0.1:5000/text/size/20/red

14 Aug 2026

Question 7 MCQ · 3 marks

Consider the following Python jinja2 templating code. File name: app.py

from jinja2 import Template

t = Template("""
{% for i in range(3) %}
    {% set x = i %}
{% endfor %}
{{ x }}
""")

print(t.render())

What text output is generated when the command python app.py is executed?

  1. Nothing will be rendered in place of x
  2. 0
  3. 1
  4. 2

Question 9 MCQ · 5 marks

Consider the following Flask application structure and code.

my_app/
├── app.py
├── views/
│   └── index.html
├── templates/
│   ├── base.html
│   └── page.html
└── my_templates/
    ├── public/
    │   └── home.html
    └── private/
        └── dashboard.html

app.py:

from flask import Flask, render_template
app = Flask(__name__, template_folder='my_templates')

@app.route('/')
def home():
    return render_template('public/home.html')

@app.route('/dashboard')
def dashboard():
    return render_template('private/dashboard.html')

@app.route('/view')
def view():
    return render_template('index.html')

@app.route('/page')
def page():
    return render_template('page.html')

if __name__ == '__main__':
    app.run(debug=True)

Which of the following statements about the template rendering behavior is correct?

  1. All routes will work correctly as Flask searches both templates/ and my_templates/
  2. Only /dashboard and /view routes work, while / and /page routes fail with TemplateNotFound Error
  3. The / and /dashboard routes work, while /view and /page fail with TemplateNotFound Error
  4. All routes fail because Flask requires templates to be in a default folder

July 2022

Question 15 MCQ · 4.5 marks

Consider the following PyHTML program. Identify the corresponding HTML file which will be generated on running the PyHTML code.

from pyhtml import *

my_html = html (head (title("Quiz 2")),
                body (h2("Second level heading"),
                      div("This is my first div"),
                      p("Paragraph1"),
                      p("Paragraph2")
                )
          )

output = my_html.render()
print (output)

  1. <!DOCTYPE html> <html> <head> <title>Quiz 2</title> </head> <body> <h2>Second level heading</h2> <div>This is my first div</div> <p>Paragraph1</p> <p>Paragraph2</p> </body> </html>
  2. <!DOCTYPE html> <html> <head> <h2>Second level heading</h2> </head> <body> <title>Quiz 2</title> <div>This is my first div</div> <p>Paragraph1</p> <p>Paragraph2</p> </body> </html>
  3. <!DOCTYPE html> <html> <head> <title>Quiz 2</title> <div>This is my first div</div> </head> <body> <h2>Second level heading</h2> <p>Paragraph1</p> <p>Paragraph2</p> </body> </html>
  4. <!DOCTYPE html> <html> <head> <title>Quiz 2</title> </head> <body> <h2>Second level heading</h2> <div>This is my first div <p>Paragraph1</p> <p>Paragraph2</p> </div> </body> </html>

Question 7 MCQ · 3 marks

If the below flask application is running locally on the URL: ‘http://127.0.0.1:5000’, what will be rendered by the browser? Python file: main.py

from flask import Flask, render_template

app=Flask(__name__)

@app.route('/')
def home():
    my_course = ['App1', 'App2', 'python', 'Data science', 'Database', 'machine learning']
    return render_template('index.html', list=my_course)

if __name__ == '__main__':
    app.run(debug=True)

Template: index.html

<html>
<head>
    <title>template file</title>
</head>
<body>
    {% macro display (list) %}
        {% for course in list %}
            <p>{{ course }}</p>
        {% endfor %}
    {% endmacro %}
    {{ display(list) }}
</body>
</html>

  1. ['App1', 'App2', 'python', 'Data science', 'Database', 'machine learning']
  2. ['App1', 'App2', 'python', 'Data science', 'Database', 'machine learning']
  3. App1 App2 python Data science Database machine learning
  4. App1 App2 python Data science Database machine learning

Dec 2023

Question 7 MCQ · 2 marks

Consider the following python code snippet.

from string import Template as T1
from jinja2 import Template as T2

temp = "A student needs to complete $c1 level to get to the {{c2}} level."
temp = T1(temp)
out = temp.substitute({'c1': 'foundation', 'c2': 'diploma'})
print(out)
out = T2(out)
print(out.render({'c2':'diploma'}))

What is the generated output on python console?

  1. A student needs to complete foundation level to get to the diploma level.A student needs to complete foundation level to get to the diploma level.
  2. A student needs to complete $c1 level to get to the {{c2}} level.A student needs to complete foundation level to get to the diploma level.
  3. A student needs to complete foundation level to get to the {{c2}} level.A student needs to complete foundation level to get to the diploma level.
  4. A student needs to complete foundation level to get to the {{diploma}} level.A student needs to complete foundation level to get to the diploma level.

06 Apr 2026

Question 11 MCQ · 4 marks

Consider the following HTML and Python Flask code. Filename: app.py

from flask import Flask, render_template
app=Flask(__name__)

@app.route("/")
def home():
    return render_template("index.html")

app.run(debug=True)

Filename: index.html

<!DOCTYPE html>
<html lang="en">
<body>
    <div>
        Output is:
        {% for i in [1,2,3] %}
            {% set x = i %}
        {% endfor %}
        {{ x }}
    </div>
</body>
</html>

Assume that the Flask app is running at http://127.0.0.1:5000. What will be the rendered output in the browser for the URL: http://127.0.0.1:5000?

  1. Output is: 3
  2. Output is:
  3. Output is: 1 2 3
  4. Output is: 1 2 3 3

Check your answers →

Weeks covering this topic

Other MAD 1 topics