Skip to content
Question

Q.(a) What possible output(s) are expected to be displayed on screen at the time of execution of the following program: import random M=[5,10,15,20,25,30] for i in range(1,3): first=random.randint(2,5)-1 sec=random.randint(3,6)-2 third=random.randint(1,4) print(M[first],M[sec],M[third],sep="#")

(i) 10#25#15 20#25#25
(ii) 5#25#20 25#20#15
(iii) 30#20#20 20#25#25
(iv) 10#15#25# 15#20#10#
(b) The code given below deletes the record from the table employee which contains the following record structure: E_code - String E_name - String Sal - Integer City - String Note the following to establish connectivity between Python and MySQL: Username is root Password is root The table exists in a MySQL database named emp The details (E_code,E_name,Sal,City) are the attributes of the table. Write the following statements to complete the code: Statement 1 - to import the desired library. Statement 2 - to execute the command that deletes the record with E_code as 'E101'. Statement 3 - to delete the record permanently from the database. import ____________ as mysql #Statement 1 def delete(): mydb=mysql.connect(host="localhost",user="root", passwd="root",database="emp") mycursor=mydb.cursor() ____________ # Statement 2 ____________ # Statement 3 print("Record deleted")
(OR)
(a) Predict the output of the code given below: def makenew(mystr): newstr=" " count=0 for i in mystr: if count%2!=0: newstr=newstr+str(count) else: if i.lower(): newstr=newstr+i.upper() else: newstr=newstr+i count+=1 print(newstr) makenew("No@1")
(b) The code given below reads the following records from the table employee and displays only those records who have employees coming from city 'Delhi': E_code - String E_name - String Sal - Integer City - String Note the following to establish connectivity between Python and MySQL: Username is root Password is root The table exists in a MySQL database named emp. The details (E_code,E_name,Sal,City) are the attributes of the table. Write the following statements to complete the code: Statement 1 - to import the desired library. Statement 2 - to execute the query that fetches records of the employees coming from city 'Delhi'. Statement 3 - to read the complete data of the query (rows whose city is Delhi) into the object named details, from the table employee in the database. import ___________ as mysql # Statement 1 def display(): mydb=mysql.connect(host="localhost",user="root", passwd="root",database="emp") mycursor=mydb.cursor() ___________ # Statement 2 details = ___________ # Statement 3 for i in details: print(i)
CBSECBSE Class XII Board 2023Subjective· 5mImportance★★★★★
🔒 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 →

First (a): random indices are all 1..4 so only option (i) is possible; delete code = import mysql.connector as mysql / mycursor.execute("DELETE FROM employee WHERE E_code='E101'") / mydb.commit(). Second (a): makenew("No@1") prints " N1@3" (leading space); fetch code = same import / mycursor.execute("SELECT * FROM employee WHERE City='Delhi'") / details = mycursor.fetchall().

Both alternatives are labelled (a) and each has an output-prediction part plus a Python-MySQL part.

Part (a)

First alternative.

(a) Possible output. With M=[5,10,15,20,25,30]:

  • first = random.randint(2,5) - 1 yields 1,2,3 or 4.
  • sec = random.randint(3,6) - 2 yields 1,2,3 or 4.
  • third = random.randint(1,4) yields 1,2,3 or 4.

Every index lies in 1..4, so only M[1]=10, M[2]=15, M[3]=20, M[4]=25 can be printed — never 5 (M[0]) or 30 (M[5]). The loop range(1,3) runs twice, giving two lines, and sep="#" puts # between the three items with no trailing #.

Evaluating the options: (i) 10#25#15 and 20#25#25 uses only allowed values and the right format → possible; (ii) contains 5 = M[0] → impossible; (iii) contains 30 = M[5] → impossible; (iv) has a trailing # → impossible. Only (i).

(b) Delete code. Connectivity uses mysql.connector; a DELETE ... WHERE removes the target row and commit() makes it permanent.

import mysql.connector as mysql            # Statement 1
def delete():
    mydb=mysql.connect(host="localhost",user="root",passwd="root",database="emp")
    mycursor=mydb.cursor()
    mycursor.execute("DELETE FROM employee WHERE E_code='E101'")  # Statement 2
    mydb.commit()                          # Statement 3
    print("Record deleted") …

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.