Skip to content
Question 28 of 29

Q.(a)

(i) What possible output(s) are expected to be displayed on screen at the time of execution of the following code ? import random S=["Pen","Pencil","Eraser","Bag","Book"] for i in range (1,2): f=random.randint(i,3) s=random.randint(i+1,4) print(S[f],S[s],sep=":") Options : (I) Pencil:Book (II) Pencil:Book Eraser:Bag (III) Pen:Book Bag:Book (IV) Bag:Eraser
(ii) The table Bookshop in MySQL contains the following attributes : B_code Integer B_name String Qty Integer Price Integer Note the following to establish connectivity between Python and MySQL on a 'localhost' : Username is shop Password is Book The table exists in a MySQL database named Bstore. The code given below updates the records from the table Bookshop in MySQL. Statement 1 - to form the cursor object. Statement 2 - to execute the query that updates the Qty to 20 of the records whose B_code is 105 in the table. Statement 3 - to make the changes permanent in the database. import mysql.connector as mysql def update_book(): mydb=mysql.connect(host="localhost", user="shop",passwd="Book",database="Bstore") mycursor=__________ # Statement 1 qry= "update Bookshop set Qty=20 where B_code=105" ___________________ # Statement 2 ___________________ # Statement 3
(OR)
(b)
(i) Predict the output of the code given below : text="LearningCS" L=len(text) ntext="" for i in range (0,L): if text[i].islower(): ntext=ntext+text[i].upper() elif text[i].isalnum(): ntext=ntext+text[i-1] else: ntext=ntext+'&&' print(ntext)
(ii) The table Bookshop in MySQL contains the following attributes : B_code Integer B_name String Qty Integer Price Integer Note the following to establish connectivity between Python and MySQL on a 'localhost' : Username is shop Password is Book The table exists in a MySQL database named Bstore. The code given below reads the records from the table Bookshop and displays all the records : Statement 1 - to form the cursor object. Statement 2 - to write the query to display all the records from the table. Statement 3 - to read the complete result of the query into the object named B_Details, from the table Bookshop in the database. import mysql.connector as mysql def Display_book(): mydb=mysql.connect(host="localhost", user="shop",passwd="Book",database="Bstore") mycursor=___________ # Statement 1 mycursor.execute("_______") # Statement 2 B_Details=________ # Statement 3 for i in B_Details: print(i)
CBSECBSE Class XII Board 2023Subjective· 5mImportance★★★★★
97% · 28/29 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Part (a): possible outputs are (I) Pencil:Book and (IV) Bag:Eraser; update code = cursor()/execute(qry)/commit(). Part (b): output is SEARNINGgC; read code = cursor()/execute("select * from Bookshop")/fetchall().

Part (a)

(i) random output

import random
S=["Pen","Pencil","Eraser","Bag","Book"]
for i in range (1,2):
    f=random.randint(i,3)   # i=1 -> randint(1,3): 1,2,3
    s=random.randint(i+1,4) # randint(2,4): 2,3,4
    print(S[f],S[s],sep=":")

range(1,2) yields only i=1, so the loop body executes exactly once -> a single line of output. Index map: S[1]="Pencil", S[2]="Eraser", S[3]="Bag", S[4]="Book"; f is never 0, so "Pen" can never appear.

  • (I) Pencil:Book needs f=1, s=4 -> valid.
  • (IV) Bag:Eraser needs f=3, s=2 -> valid.
  • (II) and (III) each print two lines, which is impossible for a one-iteration loop; (III) also demands "Pen" (index 0), impossible.

So the possible outputs are (I) and (IV).

(ii) update connectivity

mycursor = mydb.cursor()   # Statement 1: create cursor
mycursor.execute(qry)      # Statement 2: run the UPDATE
mydb.commit()              # Statement 3: make change permanent
``` …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.