Databases, SQL & Flask-SQLAlchemy: past questions

32 questions · 101 marks · 10 papers. Try each one before checking.

Solve them and check answers →Read the notes first

24 Mar 2024

Question 15 MSQ · 4.5 marks

Consider the below schema.

Field Description
emp_id Integer and primary key
employee_name String with maximum 255 length, mandatory field and can be duplicated
basic_salary Real number is an optional field that can be duplicated

Which of the below is the correct syntax to create a data model using flask_sqlalchemy?

  1. class Employee(db.Model): emp_id=db.Column(db.Integer, unique=True) employee_name=db.Column(db.String, nullable=False) basic_salary=db.Column(db.Float, nullable=True)
  2. class Employee(db.Model): emp_id=db.Column(db.Integer, primary_key=True) employee_name=db.Column(db.String, nullable=True) basic_salary=db.Column(db.Float, nullable=False)
  3. class Employee(db.Model): emp_id=db.Column(db.Integer, primary_key=True) employee_name=db.Column(db.String, nullable=False) basic_salary=db.Column(db.Float)
  4. class Employee(db.Model): emp_id=db.Column(db.Integer, primary_key=True) employee_name=db.Column(db.String, nullable=False) basic_salary=db.Column(db.Float, nullable=True)

Question 9 MCQ · 3 marks

Consider the "loans" table below, and it's model class "Loans" associated with the table "loans" in the SQLite db.

ID application_date approved_status Remarks
1 27/01/2023 Yes All docs verified
2 29/03/2023 No The mortgage doc is missing
3 13/04/2023 No The payslip doc is not enclosed
4 03/08/2023 No TDS doc is not enclosed
6 10/12/2023 Yes All docs verified
7 17/12/2023 Yes All docs verified

What is the correct output of the flask_sqlalchemy commands below?

loans=Loans.query.filter_by(approved_status="No").all()
for l in loans:
    print(l.ID, ", ", l.approved_status, ", ", l.remarks)

  1. 1, Yes, All docs verified 6, Yes, All docs verified 7, Yes, All docs verified
  2. 2, No, All docs verified 3, No, All docs verified 4, No, All docs verified
  3. 1, No, All docs verified 6, No, All docs verified 7, No, All docs verified
  4. 2, No, The mortgage doc is missing 3, No, Payslip doc is not enclosed 4, No, TDS doc is not enclosed

04 Aug 2024

Question 3 MCQ · 2 marks

Consider the below flask_sqlalchemy data models "Product", "Supplier" and "Supply" given in the code below.

class Product(db.Model):
    __tablename__ = "product"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, nullable=False)

class Supplier (db.Model):
    __tablename__ = "supplier"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, nullable=False)
    address = db.Column(db.String, nullable=True)

class Supply(db.Model):
    __tablename__ = "supply"
    id = db.Column(db.Integer, primary_key=True)
    product_id = db.Column(db.Integer, db.ForeignKey("product.id"))
    supplier_id = db.Column(db.Integer, db.ForeignKey("supplier.id"))

What will be the cardinality of the "Product" and "Supplier" relationship?

  1. One to One
  2. One to Many
  3. Many to Many
  4. Many to One

Question 16 MCQ · 4.5 marks

Two data models "Student" and "Sport" are to be created in the database with one-to-many relationship using flask_sqlalchemy. Which of the following is the correct implementation of the above two data models?

  1. class Student(db.model): id=db.Column(db.Integer, primary_key=True) Name=db.Column(db.String, nullable=False) stud_id=db.Column(db.Integer, db.Foreignkey('sport.id')) sports=db.relationship('Sport', backref='student') class Sport(db.model): id=db.Column(db.Integer, primary_key=True) sport_name=db.Column(db.String, nullable=False)
  2. class Student(db.model): id=db.Column(db.Integer, primary_key=True) Name=db.Column(db.String, nullable=False) sports=db.relationship('Sport', backref='sport') class Sport(db.model): id=db.Column(db.Integer, primary_key=True) sport_name=db.Column(db.String, nullable=False) stud_id=db.Column(db.Integer, db.ForeignKey('sport.id'))
  3. class Student(db.model): id=db.Column(db.Integer, primary_key=True) Name=db.Column(db.String, nullable=False) sports=db.relationship('Sport', backref='student') class Sport(db.model): id=db.Column(db.Integer, foreign_key=True) sport_name=db.Column(db.String, nullable=False) stud_id=db.Column(db.Integer, db.ForeignKey('student.id'))
  4. class Student(db.model): id=db.Column(db.Integer, primary_key=True) Name = db.Column(db.String, nullable=False) sports=db.relationship('Sport', backref='student') class Sport(db.model): id=db.Column(db.Integer, primary_key=True) sport_name=db.Column(db.String, nullable=False) stud_id=db.Column(db.Integer, db.ForeignKey('student.id'))

Question 5 MCQ · 3 marks

Consider the following Python code. app.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import os

app_dir = os.path.dirname(os.path.abspath(__file__))
db_file = "sqlite:///course_database.sqlite3"
app = Flask(__name__)
app.app_context().push()
app.config["SQLALCHEMY_DATABASE_URI"] = db_file
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)

class MaxScores(db.Model):
    __tablename__ = "max_scores"
    course_id = db.Column(db.String(10), nullable=False, primary_key=True)
    high_score = db.Column(db.Integer)
    term_year = db.Column(db.String(10))

Using MaxScores data model in the above "app.py". What is the correct sequence of python commands to insert MaxScores record?

  1. >>> from app.py import * >>> db.create_all() >>> s1 = MaxScores(course_id='cs2002', high_score=86, term_year='Jan-2023') >>> db.session.add(s1) >>> db.session.commit()
  2. >>> from app.py import * >>> db.create_all() >>> s1 = MaxScores('cs2002', 86, 'Jan-2023') >>> db.session.add(s1) >>> db.session.commit()
  3. >>> from app.py import * >>> db.create_all() >>> s1 = MaxScores('cs2002', 86, 'Jan-2023') >>> db.commit() >>> db.add(s1)
  4. >>> from app.py import * >>> db.create_all() >>> s1 = MaxScores(course_id='cs2002', high_score=86, term_year='Jan-2023') >>> db.session.commit() >>> db.session.add(s1)

01 Dec 2024

Question 11 MCQ · 3 marks

We are trying to create two models 'Student' and 'Assignment' and they are related by one-to-many relationship(i.e., one student will attempt multiple assignments). Assuming that flask_sqlalchemy is to be used to create data models, which of the following will correctly achieve the requirements?

  1. class Student(db.Model): id=db.Column(db.Integer, primary_key=True) name=db.Column(db.String(50)) class Assignment(db.Model): id=db.Column(db.Integer, primary_key=True) type=db.Column(db.String(4)) marks=db.Column(db.Integer) student_id=db.Column(db.Integer)
  2. class Student(db.Model): id=db.Column(db.Integer, primary_key=True) name=db.Column(db.String(50)) assignments=db.relationship('Assignment', backref='student') class Assignment(db.Model): id=db.Column(db.Integer, primary_key=True) type=db.Column(db.String(4)) marks=db.Column(db.Integer) student_id=db.ForeignKey('student.id')
  3. class Student(db.Model): id=db.Column(db.Integer, primary_key=True) name=db.Column(db.String(50)) assignments=db.relationship('Assignment', backref='student') class Assignment(db.Model): id=db.Column(db.Integer, primary_key=True) type=db.Column(db.String(4)) marks=db.Column(db.Integer) student_id=db.Column(db.Integer, db.ForeignKey('student.id'))
  4. class Student(db.Model): id=db.Column(db.Integer, primary_key=True) name=db.Column(db.String(50)) assign_id=db.Column(db.Integer, db.ForeignKey('Assignment.id')) class Assignment(db.Model): id=db.Column(db.Integer, primary_key=True) type=db.Column(db.String(4)) marks=db.Column(db.Integer)

Question 3 MCQ · 4.5 marks

Consider the following table "product" created in SQLite database corresponding to model class "Product" using flask_sqlalchemy.

id name type form quantity
1001 Cold Drinks edible liquid 100
1002 Detergent non-edible liquid 126
1005 Rugs non-edible solid 345
1008 LPG Cylinders accessories solid 236
1009 Curd edible semi-solid 520

Which of the following code snippets will change the type of edible liquids to "perishable" and will increase their quantities by 450 correctly when typed in the Python console?

  1. >>> prod = Product.query.filter_by(type = 'edible').all() >>> for item in prod: ... item.type = "perishable" ... item.quantity = 450 ... >>> db.session.commit()
  2. >>> prod = Product.query.filter_by(type='edible', form="liquid").all() >>> for item in prod: ... item.type = "perishable" ... item.quantity += 450 ... >>> db.session.commit()
  3. >>> prod = Product.query.filter_by(type='edible', form="liquid").all() >>> for item in prod: ... item.form = "perishable" ... item.quantity = 450 ... >>> db.session.commit()
  4. >>> prod = Product.query.filter_by(type = 'edible').all() >>> for item in prod: ... item.form = "perishable" ... item.quantity += 450 ... >>> db.session.commit()

16 Mar 2025

Question 2 MCQ · 2 marks

IIT Madras runs a BS Degree Program in Data Science & AI, which has thousands of students enrolled across multiple levels (Foundational, Diploma, and Degree). The institution needs a database system to manage:

Considering the above requirements, which type of database system is most appropriate?

  1. NoSQL (Document-based database like MongoDB)
  2. Graph Database (e.g., Neo4j)
  3. RDBMS (e.g., PostgreSQL, MySQL, SQLite)
  4. Key-Value Store (e.g., Redis, DynamoDB)

Question 11 MCQ · 4.5 marks

Consider the table "students" given below. The model class "Student" corresponds to the table "students" in the SQLite database.

Table: students

ID First_name Last_name Email
1 Arjun Kumar arjun@example.com
2 Karthik Kumar karthik@example.com
3 Pranav Kumar pranav@example.com
4 Surya surya@example.com
5 Harish Vignesh harisj@example.com
6 Karthik Ramesh ramesh@example.com
7 Karthik Kumar kumar@example.com

What will be the output of the following Flask-SQLAlchemy commands?

student1 = Student.query.filter_by(First_name="Karthik").first()
student1.First_name = "Vikram"
db.session.commit()
students_list = Student.query.all()
for student in students_list:
    print(student.First_name, student.Last_name)

  1. Arjun Kumar Karthik Kumar Pranav Kumar Surya Harish Vignesh Karthik Ramesh Karthik Kumar
  2. Arjun Kumar Vikram Kumar Pranav Kumar Surya Harish Vignesh Vikram Ramesh Vikram Kumar
  3. Arjun Kumar Vikram Kumar Pranav Kumar Surya Harish Vignesh Karthik Ramesh Karthik Kumar
  4. Vikram Kumar Karthik Ramesh Karthik Kumar

Question 14 MSQ · 3 marks

Consider the following Salesman table/model and answer the following question. Table/model name: Salesman

sid name sales_amount
101 Ram 7800
102 Rahim 4560
103 Krishna 8900
104 David 12300
105 Kittu 16700

Which of the following Flask SQLAlchemy code will retrieve Salesman data, where sales_amount is between 5000 and 10000(both inclusive)?

  1. sales_man= Salesman.query.filter((Salesman.sales_amount >= 5000) & (Salesman.sales_amount <= 10000)).all()
  2. sales_man= Salesman.query.filter(and_(Salesman.sales_amount >= 5000, Salesman.sales_amount <= 10000)).all()
  3. sales_man= Salesman.query.filter(sales_amount >= 5000 and sales_amount <= 10000)
  4. sales_man= Salesman.query.filter_by(sales_amount >= 5000, sales_amount <= 10000)

03 Aug 2025

Question 13 MSQ · 3 marks

Which of the following is/are correct with respect to storing data in graphs?

  1. Data on social networks can be stored with the help of graphs.
  2. Graphs support storing attributes such as node degree, edge weights ,andother metadata.
  3. Data is stored as a key-value pair.
  4. In graphs, path finding is not as important as just searching the data.

Question 4 MCQ · 2 marks

Consider the following table “ExamScores” in the SQLite database.

id score
1 57
2 98
3 83

Which of the following Python statements will print the maximum score value?

  1. >>> print(db.session.query.max(ExamScores.score))
  2. >>> print(db.session.query(func.max(ExamScores.score)).scalar())
  3. >>> print(db.session.query(func.max(ExamScores.score)))
  4. >>> print(db.session.query.max(ExamScores.score).scalar())

Question 17 COMP · 4.5 marks

You need to obtain a list of (id, days_to_fix) where days_to_fix is the number of days between broken_at and fixed_at, Using Flask-SQLAlchemy, which of the following approaches will work?

  1. repairs = GlassRepair.query.all() result = [ (r.id, (r.fixed_at - r.broken_at).days) for r in repairs ]
  2. repairs = db.session.query(GlassRepair).all() result = [] for r in repairs: delta = r.fixed_at - r.broken_at result.append((r.id, delta.days))
  3. repairs = GlassRepair.query( GlassRepair.id, (GlassRepair.fixed_at - GlassRepair.broken_at).label('days_to_fix') ).all()
  4. from datetime import timedelta repairs = GlassRepair.query.all() result = [ (r.id, timedelta(days=(r.fixed_at - r.broken_at)).days) for r in repairs ]

Question 18 COMP · 3 marks

You want to retrieve all repairs that took more than 7 days to fix. Using Flask-SQLAlchemy, which of the following approaches will correctly yield a list of (id, days_to_fix) only for those records where days_to_fix > 7?

  1. repairs = GlassRepair.query.filter_by(status='fixed').all() result = [ (r.id, (r.fixed_at - r.broken_at).days) for r in repairs if (r.fixed_at - r.broken_at).days > 7 ]
  2. from sqlalchemy import and_ repairs = GlassRepair.query.filter( GlassRepair.status=='fixed', and_( (GlassRepair.fixed_at - GlassRepair.broken_at) > 7 ) ).all() result = [(r.id, (r.fixed_at - r.broken_at).days) for r in repairs]
  3. repairs = db.session.query(GlassRepair).all() result = [] for r in repairs: days = (r.fixed_at - r.broken_at).days if days > 7 and r.status == 'fixed': result.append((r.id, days))
  4. repairs = GlassRepair.query( GlassRepair.id, (GlassRepair.fixed_at - GlassRepair.broken_at).label('days_to_fix') ).filter_by(status='fixed').filter( GlassRepair.fixed_at - GlassRepair.broken_at > 7 ).all()

14 Aug 2026

Question 3 MCQ · 3 marks

Consider the following models: Course and Enrollment corresponding to tables course and enrollment in SQLite database.

class Course(db.Model):
    id = db.Column(db.Integer(), primary_key = True)
    title = db.Column(db.String(), unique = True)

class Enrollment (db.Model):
    id = db.Column(db.Integer(), primary_key = True)
    student_id = db.Column(db.String(), nullable = False)
    course_id = db.Column(db.Integer(), db.ForeignKey("course.id"))

Based on the model schemas, what relationship do the classes Course and Enrollment share?

  1. Many-to-Many
  2. One-to-Many
  3. One-to-One
  4. The tables are not at all related

Question 1 MCQ · 3 marks

Consider the Flask SQLAlchemy data model.

class User(db.Model):
    __tablename__ = "user"
    id = db.Column(db.Integer, primary_key=True)
    mail_id = db.Column(db.String, unique=True)
    password = db.Column(db.String, nullable=False)

What is the output after the following Python code is executed ? (Assume all entries are successfully committed to the database)

>>> u1=User(mail_id='abc@gmail.com',password='12345')
>>> u2=User(mail_id='pxyz@gmail.com',password='abcd1234')
>>> u3=User(mail_id='lmn@gmail.com',password='54321')
>>> u4=User(mail_id='opqrst@gmail.com',password='xyz123')
>>> db.session.add_all([u1,u2,u3,u4])
>>> db.session.commit()
>>> result=User.query.filter(User.mail_id.like('p%')).all()
>>> print(len(result))

  1. 4
  2. 3
  3. 2
  4. 1

Question 2 MCQ · 3 marks

Consider the SQLAlchemy model Order mapped to the table orders. Table name: orders

id cust_id status amount
1 C101 PAID 500
2 C102 PENDING 1200
3 C101 PAID 800
4 C103 CANCELLED 400
5 C102 PAID 1200

Choose the correct SQLAlchemy statements that is similar to the SQL query:

SELECT * FROM orders where status='PAID' and amount>500;

  1. order_records = db.session.query(Order).filter( \ Order.status == "PAID" and Order.amount > 500)
  2. order_records = db.session.query(Order) \ .filter(Order.status == "PAID") \ .filter(Order.amount > 500)
  3. order_records = db.session.query(Order).filter( \ Order.status == "PAID") and \ db.session.query(Order).filter(Order.amount > 500)
  4. order_records = db.session.query(Order.status == "PAID" and \ Order.amount > 500)

July 2022

Question 6 MCQ · 3 marks

Which of the following is the correct Python object representation of the given SQL query with declarative_base() class.

CREATE TABLE "Items" (
"ID" INTEGER,
"name" TEXT,
"cost" INTEGER,
PRIMARY KEY("ID" AUTOINCREMENT)
)

  1. Base = declarative_base() class User(Base): __tablename__ = "User" ID = Column(Integer, primary_key = True, autoincrement = True) name = Column(String) cost = Column(Integer)
  2. Base = declarative_base() class Items(Base): __tablename__ = "Items" ID = Column(Integer, primary_key = True, autoincrement = True) name = Column(String) cost = Column(Integer)
  3. Base = declarative_base() class Items(Base): __tablename__ = "Items" Item_ID = Column(Integer, primary_key = True, autoincrement = True) Item_name = Column(String) Item_cost = Column(Integer)
  4. Base = declarative_base() class Items(Base): __tablename__ = "Items" cost = Column(Integer, primary_key = True, autoincrement = True) name = Column(String) ID = Column(Integer)

Question 4 MCQ · 3 marks

Consider the table "production" in SQLite database given below:

Manufacturer Product Quantity Cost
Industry A Biscuits 3 100
Industry B Shoes 2 500
Industry A Chocolate 10 200
Industry C Pen 20 50
Industry B Shirts 5 700
Industry D Beverages 30 150
Industry C Notebook 40 250

What will be the output of the SQL query given below?

SELECT Manufacturer, COUNT(*)
FROM production
WHERE Quantity>10
GROUP BY Manufacturer
HAVING COUNT(*) = 2;

  1. Manufacturer|COUNT(*) Industry D |1
  2. Manufacturer|COUNT(*) Industry C |2
  3. Manufacturer|COUNT(*) Industry C |2 Industry D |1
  4. Manufacturer|COUNT(*) Industry C |1 Industry D |2

Question 8 MCQ · 3 marks

Consider the following table "table_A" in SQLite database.

ID First_Name Last_Name Course
1 1 Raj R Java
2 2 Riya Z Java
3 3 Sam sundar Java
4 4 praneeth k Java
5 5 Kunal T Python
6 6 vimal singh Python
7 7 sethu J Python
8 8 liza G C++
9 9 shylla chopra C++

What will be the output of the following SQL query?

SELECT Course, count(ID) AS No_Of_Students
FROM table_A GROUP BY Course
ORDER BY No_Of_Students ASC;

  1. | | Course | No_Of_Students | | ----- | ----- | ----- | | 1 | Python | 1 | | 2 | Java | 1 | | 3 | Java | 1 | | 4 | Java | 1 | | 5 | C++ | 1 | | 6 | Java | 1 | | 7 | Python | 1 | | 8 | C++ | 1 | | 9 | Python | 1 |
  2. | | Course | No_Of_Students | | ----- | ----- | ----- | | 1 | Python | 1 | | 2 | C++ | 1 | | 3 | Python | 1 | | 4 | Java | 1 | | 5 | C++ | 1 | | 6 | Java | 1 | | 7 | Java | 1 | | 8 | Java | 1 | | 9 | Python | 1 |
  3. | | Course | No_Of_Students | | ----- | ----- | ----- | | 1 | Java | 4 | | 2 | Python | 3 | | 3 | C++ | 2 |
  4. | | Course | No_Of_Students | | ----- | ----- | ----- | | 1 | C++ | 2 | | 2 | Python | 3 | | 3 | Java | 4 |

Question 12 MCQ · 2 marks

Consider the following table 'flowers' in SQLite database.

ID Name color
1 Jasmine white
2 Rose pink
3 Lily pink
4 Lotus pink
5 Red rose red

What will be the output of the following SQL query?

Select count(ID), Name, color from flowers
group by color HAVING count(color)<2;

  1. | count(ID) | Name | color | | ----- | ----- | ----- | | 3 | Rose | pink |
  2. | count(ID) | Name | color | | ----- | ----- | ----- | | 1 | red rose | red | | 1 | Jasmine | white |
  3. | count(ID) | Name | color | | ----- | ----- | ----- | | 3 | Rose | pink | | 1 | red rose | red | | 1 | Jasmine | white |
  4. | count(ID) | Name | color | | ----- | ----- | ----- | | 1 | Red rose | red | | 1 | Jasmine | white |

Question 5 MCQ · 3 marks

Consider the table "employee" given below. The model class "Employee" corresponds to table "employee" in the database.

id first_name last_name email
1 Ruhil Verma verma@gmail.com
2 Monica gulati manisha@gmail.com
3 Sandeep Sharma sharma@gmail.com
4 Taapsi Uma uma@gmail.com

What will be the output of the flask_sqlalchemy command given below?

>>> user1= Employee.query.filter_by(first_name="Monica").first()
>>> user1.first_name= "Manisha"
>>> db.session.commit()
>>> emp = Employee.query.all()
>>> for emp1 in emp:
...     print(first_name)

  1. Ruhil Monica Sandeep Taapsi
  2. Ruhil Sandeep Taapsi
  3. Ruhil Manisha Sandeep Taapsi
  4. None of these

Aug 2023

Question 10 MCQ · 3 marks

In the code snippet given below, what should come in place of code 1 and code 2 such that one parent can have multiple children and the converse does not hold true?

from sqlalchemy import ForeignKey
from sqlalchemy import Integer, Column
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import relationship

class Base (DeclarativeBase):
    pass

class Parent(Base):
    __tablename__ = "parent_table"
    id = Column(Integer, primary_key=True)
    # write your code 1 here

class Child(Base):
    __tablename__ = "child_table"
    id = Column(Integer, primary_key=True)
    # write your code 2 here

  1. code 1: parent_id=Column(Integer, ForeignKey("child_table.id")) code 2: children = relationship ("Parent")
  2. code 1: children = relationship("Child") code 2: parent_id=Column (Integer, ForeignKey("parent_table.id"))
  3. code 1: parent_id=Column (Integer, ForeignKey("parent_table.id")) code 2: children = relationship("Child")
  4. code 1: children = relationship ("Parent") code 2: parent_id=Column (Integer, ForeignKey("child_table.id"))

Question 11 MCQ · 4.5 marks

Consider the following models Brand and Cellphone corresponding to tables brand and cellphone in SQLite database.

class Brand(db.Model):
    id = db.Column (db.Integer(), primary_key = True)
    name = db.Column(db.String(), unique = True)

class Cellphone (db.Model):
    id = db.Column (db.Integer(), primary_key = True)
    name = db.Column (db.String(), unique = True)
    brand = db.Column (db.Integer(), unique = True, db.ForeignKey("brand.id"))

Based on the model schemas, what relationship do the classes Brand and Cellphone share?

  1. One-to-One
  2. One-to-Many
  3. Many-to-Many
  4. The tables are not at all related

Question 12 MCQ · 4.5 marks

Consider the schema for the Class Student.

CREATE TABLE "student" (
    "s_id" INTEGER,
    "roll_number" TEXT NOT NULL UNIQUE,
    "first_name" TEXT NOT NULL,
    "last_name" TEXT NOT NULL,
    PRIMARY KEY("s_id" AUTOINCREMENT)
);

What will be the output of the flask_sqlalchemy command given below?

>>> s1 = Student(roll_number = "M01", first_name = "Yash", last_name = "Raj")
>>> db.session.add(s1)
>>> s2 = Student(roll_number = "M02", first_name = "Yash", last_name = "Maurya")
>>> db.session.add(s2)
>>> s3 = Student(roll_number = "M03", first_name = "Ansh", last_name = "Raj")
>>> db.session.add(s3)
>>> db.session.commit()
>>> user1= Student.query.filter_by(first_name="Yash").first()
>>> user1.first_name= "Ansh"
>>> user1.last_name= "Maurya"
>>> db.session.commit()
>>> s1 = Student.query.all()
>>> for student in s1:
        print(student.first_name)
        print(student.last_name)

  1. Yash Raj Yash Maurya Ansh Raj
  2. Ansh Maurya Yash Maurya Ansh Raj
  3. Ansh Maurya Ansh Maurya Ansh Raj
  4. None

Dec 2023

Question 6 MCQ · 2 marks

In the context of in-memory databases, what is the primary function of an index?

  1. To encrypt sensitive data
  2. To optimize query performance by speeding up data retrieval
  3. To manage data replication
  4. To store metadata about the database structure

Question 10 COMP · 2 marks

What relationship do the tables "channel" and "video" share.

  1. One-to-one
  2. One-to-many
  3. Many-to-one
  4. Many-to-many

Question 11 COMP · 3 marks

If "ch1" is a channel object created in "channel" table with primary key 1, then the correct way(s) of creating a video object "vid1" in the video table that belongs to "ch1" object is/are (let "name" of the video be "Intro to Python")

  1. >>> vid1 = Video (name = "Intro to Python", channel = ch1) >>> db.session.add(vid1) >>> db.session.commit()
  2. >>> vid1 = Video (name = "Intro to Python", channel = 1) >>> db.session.add(vid1) >>> db.session.commit()
  3. >>> vid1 = Video(name = "Intro to Python", playlist = ch1) >>> db.session.add(vid1) >>> db.session.commit()
  4. >>> vid1 = Video(name = "Intro to Python", playlist = 1) >>> db.session.add(vid1) >>> db.session.commit()

Question 2 MCQ · 4.5 marks

Consider the schema for the 'student' table created in SQLite database using flask-sqlalchemy.

CREATE TABLE "student" (
    "s_id" INTEGER,
    "roll_number" TEXT NOT NULL UNIQUE,
    "first_name" TEXT NOT NULL,
    "last_name" TEXT NOT NULL,
    PRIMARY KEY("s_id" AUTOINCREMENT)
);

What will be the output of the flask_sqlalchemy command given below?

>>> s1 = Student(roll_number = "M01", first_name = "John", last_name = "Doe")
>>> db.session.add(s1)
>>> s2 = Student(roll_number = "M02", first_name = "John", last_name = "Luther")
>>> db.session.add(s2)
>>> s3 = Student(roll_number = "M03", first_name = "Harry", last_name = "Doe")
>>> db.session.add(s3)
>>> db.session.commit()
>>> user1= Student.query.filter_by(first_name="John").first()
>>> user1.first_name= "Harry"
>>> user1.last_name= "Luther"
>>> db.session.commit()
>>> s1 = Student.query.all()
>>> for student in s1:
        print(student.first_name)
        print(student.last_name)

  1. Harry Luther Harry Luther Harry Doe
  2. Harry Luther John Luther Harry Doe
  3. Harry Luther Harry Doe Harry Doe
  4. Harry Doe John Luther Harry Doe

06 Apr 2026

Question 1 MCQ · 2 marks

You want to create an app where students can log in and download hall tickets. To make this more efficient, you want to have some very quick way to check whether a given user has already downloaded their hall ticket or not. Which kind of database will you prefer?

  1. RDBMS
  2. Doc-oriented database
  3. Graph database
  4. Key-value store

Question 16 COMP · 3 marks

Which of the following relationships exists between the "User" and "Profile" models?

  1. Many-to-Many
  2. Many-to-One
  3. One-to-Many
  4. One-to-One

Question 17 COMP · 3 marks

What will be the output if the following commands are executed in the python shell.

>>> u1 = User(uname = "Jane", pwd = "1234")
>>> p1 = Profile(name = "Analyst", email = "zoozle.com", user_id = u1.id)
>>> db.session.add_all([u1,p1])
>>> db.session.commit()
>>> user1 = User.query.filter_by(uname="Jane").first()
>>> user1.profile

  1. <User 1>
  2. [<Profile 1>]
  3. <Profile 1>
  4. 'Analyst'

Check your answers →

Weeks covering this topic

Other MAD 1 topics