Computer Science - Class 12
Chapter 15 - Interface of Python with SQL Database

Consider the following table STUDENT:

Question2-Consider the following table - Teachoo.JPG

TABLE STUDENT MCQ Case Based Question - 2.jpg

Table: STUDENT

Stud. Name Course Result
1101 Anubhav Singh BCom Awaited
1102 Riya Sharma BSc Passed
1103 Kartikey Rana BSc Passed
1104 Archana Kapoor BBA Awaited
1105 Aarti Sharma BCom Awaited
1106 Sanchit Kumar BCom Passed

 

Question 2(i)

What will be the query statement to insert a new record with Student id = 1107, name = Sumit Saxena, Course = BBA and result = passed

(A) sql = “INSERT INTO student (stuid, name, course, result) VALUES (‘1107’, ‘Sumit Saxena’, ‘BBA’, ‘Passed’)”

(B) sql = “INSERT INTO student VALUES (‘1107’, ‘Sumit Saxena’, ‘BBA’, ‘Passed’)”

(C) Both (a) and (b)

(D) None of these

 

Answer:

Question 2 (i) Option A-What will be - Teachoo.JPG

Imports the required python module - Teachoo.JPG
Question 2 (i)Option B-What will be the query - Teachoo.JPG Question 2 (i) Option B - Teachoo.JPG

Checking the options

(A) sql = “INSERT INTO student (stuid, name,  course, result) VALUES (‘1107’, ‘Sumit Saxena’,  ‘BBA’, ‘Passed’)”  MCQ Case Based Question - 2(i).jpg

The query given above inserts the new record to table STUDENT. 

(B) sql = “INSERT INTO student VALUES (‘1107’,  ‘Sumit Saxena’, ‘BBA’, ‘Passed’)”  

MCQ Case Based Question - 2(i)-1.jpg

The query given above inserts the new record to table STUDENT. 

(C) Both (a) and (b)  

(D) None of these 

 

Both option A and B , add the given record to the table.

So, the correct answer is (C)

 

Question 2(ii)

The following sql command has been given: Sql = “UPDATE student SET result = ‘Passed’ WHERE course = ‘BCom’ ” The result of which of the following STUID will be changed to “Passed”?

(A) 1101

(B) 1101, 1104

(C) 1101, 1105

(D) 1101, 1104, 1105

 

Answer:

Question 2 (ii)-The following sql - Teachoo.JPG

Table STUDENT after executing the given query:

MCQ Case Based Question - 2(ii).jpg

On comparing the updated table with the original table, it is clear that the value in the column result has been changed for 2 records , the records with Stud as 1101 and 1105 .

 

Checking the options:

  • (A) 1101 
  • (B) 1101, 1104  
  • (C) 1101, 1105 
  • (D) 1101, 1104, 1105 

So, the correct answer is (C)

 

Question 2(iii)

Consider the following code: sql = '''SELECT * from STUDENT''' cursor.execute(sql) result = cursor.fetchone(); print(result) What will be the output of the above code in reference to the STUDENT table?

(A) 1101, Anubhav Singh, BCom, Awaited

(B) 1106, Sanchit Kumar, BCom, Passed

(C) 1103, Kartikey Rana, BSc, Passed

(D) Both (A) and (B)

 

Answer:

Question 2 (iii)-Consider the following - Teachoo.JPG Question 2 (iii) - Teachoo.JPG

MCQ Case Based Question - 2(iii).jpg

The given code will display the first record from the table STUDENT as the fetchone( ) function retrieves one row from the result set.

 

Checking the options

  • (A) 1101, Anubhav Singh, BCom, Awaited  
  • (B) 1106, Sanchit Kumar, BCom, Passed  
  • (C) 1103, Kartikey Rana, BSc, Passed  
  • (D) Both (A) and (B) 

In the options given above, option A is the first record of the table STUDENT.

So, the correct answer is (A)

 

Question 2(iv)

Consider the following code:

result = cursor.fetchmany(2);

print(result)

What will be the result if it is executed after the code given in (iii)?

(A) 1101, Anubhav Singh, BCom, Awaited 1102, Riya Sharma, BSc, Passed

(B) 1102, Riya Sharma, BSc, Passed 1103, Kartikey Rana, BSc, Passed

(C) 1103, Kartikey Rana, BSc, Passed 1104, Archana Kapoor, BBA, Awaited

(D) None of these

 

Answer:

Question 2 (iv)-Consider the following - Teachoo.JPG Question 2 (iv) Teachoo.JPG

MCQ Case Based Question - 2(iv).jpg

The given code will display the second and third record from the result set. The fetchmany( ) function retrieves the given number of records from the result set from the current cursor position.

 

Checking the options

  • (A) 1101, Anubhav Singh, BCom, Awaited 
    1102, Riya Sharma, BSc, Passed  
  • (B) 1102, Riya Sharma, BSc, Passed 
    1103, Kartikey Rana, BSc, Passed
    (C) 1103, Kartikey Rana, BSc, Passed 
    1104, Archana Kapoor, BBA, Awaited
  • (D) None of these 

In the options given above, option B has both the 2nd and 3rd record of table STUDENT.

So, the correct answer is (B)

 

Question 2(v)

Consider the following code:

cursor=db.cursor()

query = "DELETE FROM student WHERE course

= ‘BCom’ AND result = ‘Awaited’ “

cursor.execute(query)

del_rows= cursor.rowcount

print(del_rows)

What will be the output of the above code?

(A) 1

(B) 2

(C) 3

(D) 4

 

Answer:

Question 2 (v)-Consider the following - Teachoo.JPG Question 2 (v) - Teachoo.JPG

MCQ Case Based Question - 2(v).jpg

The given code will display the number of records that match the criteria mentioned in the query and is deleted from the table.

Checking the options:

  • (A) 1 
  • (B) 2  
  • (C) 3 
  • (D) 4 

Since records with Stud 1101 and 1105 match the criteria mentioned in the query, it is deleted from the table,so the output of the code will be 2 .

So, the correct answer is (B)


Transcript

Case Base Question 2 Consider the following table STUDENT: Question 2 (i) Option A What will be the query statement to insert a new record with Student id = 1107, name = Sumit Saxena, Course = BBA and result = passed (A) sql = “INSERT INTO student (stuid, name, course, result) VALUES (‘1107’, ‘Sumit Saxena’, ‘BBA’,‘Passed’)” (B) sql = “INSERT INTO student VALUES (‘1107’, ‘Sumit Saxena’, ‘BBA’, ‘Passed’)” (C) Both (a) and (b) (D) None of theseImports the required python module creating the cursor object for executing the queries The query assigned to the variable sql is executed All the rows affected by the previous query ie., to display all rows from the table, is assigned to the variable data Creates a connection between MySQL database & Python application The query to be executed is assigned to the variable sql Confirms changes made by user to the database Output Question 2 (i) Option B What will be the query statement to insert a new record with Student id = 1107, name = Sumit Saxena, Course = BBA and result = passed (A) sql = “INSERT INTO student (stuid, name, course, result) VALUES (‘1107’, ‘Sumit Saxena’, ‘BBA’,‘Passed’)” (B) sql = “INSERT INTO student VALUES (‘1107’, ‘Sumit Saxena’, ‘BBA’, ‘Passed’)” (C) Both (a) and (b) (D) None of theseImports the required python module creating the cursor object for executing the queries The query assigned to the variable sql is executed All the rows affected by the previous query ie., to display all rows from the table, is assigned to the variable data Creates a connection between MySQL database & Python application The query to be executed is assigned to the variable sql Confirms changes made by user to the database Output Question 2 (ii) The following sql command has been given: Sql = “UPDATE student SET result = ‘Passed’ WHERE course = ‘BCom’ ” The result of which of the following STUID will be changed to “Passed”? (A) 1101 (B) 1101, 1104 (C) 1101, 1105 (D) 1101, 1104, 1105 Imports the required python module Creating the cursor object for executing the query The query assigned to the variable sql is executed Creates a connection between the MySQL database and Python application The query to be executed is assigned to the variable sql Confirms the changes made by the user to the database Question 2 (iii) Consider the following code: sql = '''SELECT * from STUDENT'‘’ cursor.execute(sql) result = cursor.fetchone(); print(result) What will be output of above code in reference to the STUDENT table? (A) 1101, Anubhav Singh, BCom, Awaited (B) 1106, Sanchit Kumar, BCom, Passed (C) 1103, Kartikey Rana, BSc, Passed (D) Both (A) and (B) Imports the required python module Creating the cursor object for executing the query The query assigned to the variable sql is executed One row from the result set is fetched and assigned to the variable result Creates a connection between the MySQL database and Python application The query to be executed is assigned to the variable sql Prints the value stored in variable result Output Question 2 (iv) Consider the following code: result = cursor.fetchmany(2); print(result) What will be the result if it is executed after the code given in (iii)? (A) 1101, Anubhav Singh, BCom, Awaited 1102, Riya Sharma, BSc, Passed (B) 1102, Riya Sharma, BSc, Passed 1103, Kartikey Rana, BSc, Passed (C) 1103, Kartikey Rana, BSc, Passed 1104, Archana Kapoor, BBA, Awaited (D) None of these Imports the required python module Creating the cursor object for executing the query The query assigned to the variable sql is executed 1 row from the result set is fetched and the cursor positions moves to the beginning of the next row Creates a connection between the MySQL database and Python application The query to be executed is assigned to the variable sql 2 rows are fetched from the result set from the current position of the cursor and assigned to the variable result Output Question 2 (v) Consider the following code: cursor=db.cursor() query = "DELETE FROM student WHERE course = ‘BCom’ AND result = ‘Awaited’ “ cursor.execute(query) del_rows= cursor.rowcount print(del_rows) What will be the output of the above code? (A) 1 (B) 2 (C) 3 (D) 4 Imports the required python module Creating the cursor object for executing the query The query assigned to the variable sql is executed The number of rows affected by the execution of the query is assigned to the variable del_rows Creates a connection between the MySQL database and Python application The query to be executed is assigned to the variable sql Prints the value stored in variable result Output

Ask a doubt
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science, Social Science, Physics, Chemistry, Computer Science at Teachoo.