Home / Past papers / MAD1 End Term / this paper
If the flask app is running locally on http://127.0.0.1:5000. What will be the output on the browser for the base URL?app.py
```python
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template("index.html", items=['DaShBoard', 'SETtings',
'HELP'])
app.run()
```
index.html
```html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Menu</title>
</head>
<body>
{% macro nav_list(elements) %}
<ol>
{% for element in elements %}
<li><a href="/{{ element|lower }}">{{ element|upper }}</a></li>
{% endfor %}
</ol>
{% endmacro %}
{{ nav_list(items) }}
</body>
</html>
```
1. DaSHBoard 2. SETtings 3. HELP

1. dashboard 2. settings 3. help

1. DASHBOARD 2. SETTINGS 3. HELP

1. DASHBOARD 2. SETTINGS 3. HELP
If the application is running locally on http://127.0.0.1:5000, select the correct options.```python
from flask import Flask, abort, redirect, url_for
app = Flask(__name__)
@app.route('/home/<path:directory>')
def home(directory):
if "DBMS" in directory:
return "Welcome to DBMS course!"
else:
abort(404)
@app.route('/home/course/<path:directory>')
def course(directory):
if "DBMS" in directory:
return redirect(url_for('home', directory=directory))
else:
abort(404)
@app.errorhandler(404)
def page_not_found(error):
return "<h2>Sorry, No course found!!<h2>"
app.run(debug=True)
```
For the URL: ``` http://127.0.0.1:5000/home/course/data_science?course=DBMS ``` The browser will render: **Sorry, No course found!**

For the URL: ``` http://127.0.0.1:5000/home/IITM/DBMS?course=PM ``` The browser will render: Welcome to DBMS course!

For the URL: ``` http://127.0.0.1:5000/home/course/programming?course=DBMS ``` The browser will render: Welcome to DBMS course!

For the URL: ``` http://127.0.0.1:5000/home/IITM/DBMS?course=Data_structures ``` The browser will render: **Sorry, No course found!**


Student not found!

Student found: Dhawan

``` IndexError: list index out of range ```

Student found: Anant

Student found: Veda

Invalid command!

```python
def generate_series(n):
seq = []
for i in range(1, n+1):
seq.append(i * 3)
return seq
def test_case1():
assert 9 in generate_series(3)
def test_case2():
assert 21 in generate_series(6)
def test_case3():
assert 30 in generate_series(8)
```
What will be the summary of the output for the command `pytest file.py` on the terminal?
``` =========== 1 failed, 2 passed in 0.30s =========== ```

``` ========== 2 failed, 1 passed in 0.30s ========== ```

``` =================================== 3 failed in 0.30s =================================== ```

```text ============================= 1 failed, 1 passed in 0.30s ============================= ```
Which of the following flask_sqlalchemy models will create exactly the same table as created by the above SQL command?```sql
CREATE TABLE employee (
emp_id INTEGER NOT NULL,
emp_code VARCHAR() NOT NULL,
name VARCHAR(),
salary INTEGER NOT NULL,
join_date DATE NOT NULL,
PRIMARY KEY (emp_id),
UNIQUE (emp_code),
UNIQUE (name)
)
```
```python
class Employee(db.Model):
emp_id = db.Column(db.Integer(), unique=True)
emp_code = db.Column(db.String(), primary_key=True)
name = db.Column(db.String(), unique=False)
salary = db.Column(db.Integer())
join_date = db.Column(db.Date())
```
```python
class Employee(db.Model):
emp_id = db.Column(db.Integer(), primary_key=True)
emp_code = db.Column(db.String(), nullable=False, unique=True)
name = db.Column(db.String(), unique=True)
salary = db.Column(db.Integer(), nullable=False)
join_date = db.Column(db.Date(), nullable=False)
```
```python
class Employee(db.Model):
emp_id = db.Column(db.Integer(), primary_key=True)
emp_code = db.Column(db.String(), unique=True)
name = db.Column(db.String(), unique=True, nullable=False)
salary = db.Column(db.Integer())
join_date = db.Column(db.Date())
```
```python
class Employee(db.Model):
emp_id = db.Column(db.Integer(), nullable=False)
emp_code = db.Column(db.String(), unique=True)
name = db.Column(db.String())
salary = db.Column(db.Integer(), nullable=False)
join_date = db.Column(db.Date(), nullable=False)
```
```python
from jinja2 import Template
products = [
{"category": "Groceries", "price": 40, "name": "Rice"},
{"category": "Electronics", "price": 300, "name": "Headphones"},
{"category": "Groceries", "price": 10, "name": "Soap"},
]
t = """
<ul>
{% for group in products|groupby('category') %}
<li>{{ group.grouper }}
<ul>
{% for item in group.list %}
<li>{{ item.name }} costs {{ item.price }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
"""
temp = Template(t)
print(temp.render(products=products))
```
* Groceries
* Rice costs 40
* Electronics
* Headphones costs 300
* Groceries
* Soap costs 10
* Electronics
* Headphones costs 300
* Groceries
* Rice costs 40
* Groceries
* Soap costs 10
* Groceries * Rice costs 40 * Groceries * Soap costs 10 * Electronics * Headphones costs 300

- Electronics - Headphones costs 300 - Groceries - Rice costs 40 - Soap costs 10
Based on the above data, answer the given subquestions.


HELLO USER

HELLO USER

WELCOME USER

WELCOME USER

Match the type of update with the correct analogy/description: | Update Type | Analogy/Description | | :---------------------- | :----------------------------------------------------------------------- | | 1. Synchronous Updates | A. Runs tasks one after another, without parallel execution | | 2. Asynchronous Updates | B. Freezes the UI until the operation is complete | | 3. Blocking Execution | C. Ordering food at a restaurant and waiting in line until it is ready | | 4. Non-blocking Execution | D. Ordering food, receiving a buzzer, and waiting at a table until it rings | | | E. Allows other tasks to continue while waiting for a response |

Given the following test code:
```python
def test_about(client):
response = client.get('/about')
assert response.status_code == 200
assert b'This is the about page.' in response.data
```
Assume the client fixture works correctly and you are testing a Flask route `/about` that should return the text "This is the about page.". Which of the following options would cause this test to fail?
The route `/about` is missing in `app.py`.

The route `/about` exists but returns "About us page" instead.

The route `/about` returns `<p>this is the about page.</p>` instead of plain text.


The goal is to print "Even" for even numbers and "Odd" for odd numbers in the sequence. Which of the following if-else blocks should replace <!-- Missing code goes here --> so that the code works correctly?

```jinja2
{% if num / 2 == 0 %}
Even
{% else %}
Odd
{% endif %}
```
```jinja2
{% if num % 2 == 0 %}
Even
{% else %}
Odd
{% endif %}
```
```jinja2
{% if num is even %}
Even
{% else %}
Odd
{% endif %}
```
```jinja
{% if num % 2 %}
Even
{% else %}
Odd
{% endif %}
```
Even Even Even

Odd Even Odd

``` 2 5 8 ```

Even Odd Even
The Requests table represents service bookings made by customers. Based on the above data, answer the given subquestions.


```sql SELECT c.name, s.service_name FROM customers c, services s WHERE c.id = s.id; ```

```sql SELECT c.name, r.status FROM customers c JOIN services s ON c.id = s.id; ```

```sql SELECT c.name, s.service_name FROM customers c JOIN requests r ON c.id = r.customer_id JOIN services s ON r.service_id = s.id; ```

```sql SELECT c.name, s.service_name FROM services s LEFT JOIN customers c ON s.id = c.id; ```

```sql SELECT r.customer_id, COUNT(*) AS total_requests FROM requests r; ```

```sql SELECT c.name, COUNT(r.service_id) AS total_requests FROM customers c JOIN requests r ON c.id = r.customer_id GROUP BY c.name; ```

```sql SELECT c.name, COUNT(s.id) AS total_requests FROM customers c JOIN services s ON c.id = s.id GROUP BY c.name; ```

```sql SELECT c.name, r.service_id FROM customers c JOIN requests r ON c.id = r.customer_id; ```

```python customer = Customer.query.filter_by(id=1).first() services = Service.query.all() ```

```python services = Service.query.filter_by(customer_id=1).all() ```

```python customer = Customer.query.get(1) services = [r.service for r in customer.requests] ```

```python services = Request.query.filter_by(id=1).all() ```

```javascript
document.getElementById("myBtn").onclick = function() {alert("Clicked!");
};
```
```javascript
document.getElementById("myBtn").addEventListener("click", function() {
alert("Clicked!");
});
```
```html
<button onclick="alert('Clicked!')">Click Me</button>
```
What will be the output of the following code?
```html
<script>
function greet(name = "Guest") {
return "Hello, " + name;
}
console.log(greet());
console.log(greet("Alice"));
</script>
```
Hello, Guest Hello, Alice

``` Hello, undefined Hello, Alice ```

``` Hello, Guest '[object console]' ```

You want to change the text of a `<p>` element with `id="msg"` to "Welcome to JavaScript!" when a button is clicked. Which code snippet correctly does this?
```html
<p id="msg">Old Message</p>
<button id="btn">Change</button>
<script>
// Code goes here
</script>
```
```javascript
document.getElementById("btn").onclick = function() {
document.getElementById("msg").innerHTML = "Welcome to JavaScript!";
};
```
```javascript
document.getElementById("btn").addEventListener("click", function() {
msg.text = "Welcome to JavaScript!";
});
```
```javascript
document.querySelector("#btn").addEventListener("click", function() {
document.querySelector("#msg").innerText = "Welcome to JavaScript!";
});
```
```javascript
document.getElementById("btn").onclick = function() {
document.getElementById("msg").innerHTML = "Welcome to JavaScript!";
};
```
Table: products | pid | name | price | | :-- | :---- | :---- | | 101 | Pen | 10 | | 102 | Bag | 450 | | 103 | Watch | 800 | | 104 | Shoes | 1300 | Which query lists all products sorted from expensive to cheap?
Which of the following assertion test case throws the exception?```python
def max_value(nums):
if not nums:
return None
return max(nums)
def test_max_value():
##Assertions to test max_value function##
```
``` curl -X POST -d "username=student&password=1234" https://student/login ```

Consider the following HTML5 form:
```html
<form method="POST" action="/submit">
<input type="email" name="email" required>
<input type="password" name="password" required minlength="8">
<input type="number" name="age" min="18" max="65" required>
<button type="submit">Submit</button>
</form>
```
Which of the following attributes are HTML5 native validation features that work without JavaScript?
Consider the following Flask-RESTful code:
```python
class UserResource(Resource):
def get(self, user_id=None):
if user_id:
return {"message": "User details", "id": user_id}, 200
return {"message": "All users"}, 200
def post(self):
return {"message": "User created"}, 201
api.add_resource(UserResource, '/users', '/users/<int:user_id>')
```
Which of the following requests will execute successfully?
Consider this logging setup:
```python
import logging
logging.basicConfig(level=logging.INFO,
format='%(levelname)s:%(name)s:%(message)s')
logging.debug("Debug message")
logging.info("Info message")
logging.warning("Warning message")
```
What will be displayed in the output?
Given the following pytest test structure:
```python
@pytest.fixture
def sample_data():
return {"name": "John", "age": 30}
def test_name(sample_data):
assert sample_data["name"] == "John"
def test_age(sample_data):
assert sample_data["age"] == 30
```
When running `pytest -v`, how many times will the sample_data fixture be executed?
Consider test file with markers:
```python
@pytest.mark.slow
def test_database():
pass
@pytest.mark.unit
def test_calculation():
pass
@pytest.mark.slow
@pytest.mark.unit
def test_complex():
pass
```
Running `pytest -m "unit and not slow"` will execute which tests?
``` git merge feature1 then git merge feature2 ```

``` git checkout feature1 then git merge feature2 then git merge main ```

``` git checkout feature1 then git merge feature2 then git checkout main then git merge feature1 ```

``` git merge feature1 then git checkout feature2 then git merge main ```

In a Flask-Login protected route below:
```python
@app.route('/dashboard')
@login_required
def dashboard():
return f"Welcome {current_user.username}"
```
What happens when an unauthenticated user tries to access /dashboard?