Class 12 Computer Science Set 3 With Solutions
Class 12 Computer Science Set 3 With Solutions
Class 12 Computer Science Set 3 With Solutions
import random
AR=[20,30,40,50,60,70]
FROM=random.randint(1,3)
TO=random.randint(2,4)
for K in range(FROM,TO+1):
print (AR[K],end=”#”)
(i) 10#40#70#
(ii) 30#40#50#
(iii) 50#60#70#
(iv) 40#50#70#
OR
What will be the output of the following Python code?
V = 25 def Fun(Ch):
V=50
print(V, end=Ch)
V *= 2
print(V, end=Ch)
print (V, end=”*”)
Fun(“!”)
print(V)
Answer: (ii) 30#40#50#
Maximum value for FROM,TO are 3,4 respectively.
OR
25*50!100!25.
Commonly Made Error:
Students sometimes misunderstand concept of global and local variables.
Answering Tip: Remember that anything global is available everywhere. And anything that is local is available
in a particular area. The same applies to the local and global variables.
Question 25. What is the use of wildcard ?
OR
Consider the structure of table Doctor given below
Doctor_id(p)
Doctor_Name
Hospital_id(F)
Joining_Date
Speciality
Salary
Experience.
Write Python code to create the above table. [2]
Answer: The wildcard operators are used with the LIKE operator to search a value similar to a specific pattern
in a column. There are two wildcard operators.
% – represents 0,1 or many characters — represents a single number or character
OR
import MySQLdb
db = MySQLdb.connect(‘localhost’,’Hos Admin’,1HosAdmin@123′,’XYZHospital’)
cursor=db.cursor()
cursor-execute(“DROP TABLE IF EXISTS DOCTORS”)
sql=”””Create Table Doctor (‘Doctor_ Id’ INT NOTNULL, ‘Doctor_Name1 Char(50) NOTNULL,
‘Hospital_Id’ INT NOTNULL, ‘Joining_Date’ Date NOTNULL, ‘Speciality’ char(50), ‘Salary’ Float,
‘Experience’ Float
PrimaryKey (‘Doctor_Id1 ) )'””’
cursor.execute(sql)
cursor.close()
db. close ()
Section – C
Question 26. (a) Consider the following tables and give output of the query given below.
Table: TRAINS
Table: PASSENGERS
TABLE: ADMIN
Code Gender Designation
1001 Male Vice Principal
1009 Female Coordinator
1203 Female Coordinator
1045 Male Hod
1123 Male Senior Teacher
1167 Male Senior Teacher
1215 Male Hod
Answer:
(a)
END COUNT(*)
Habibganj 2
Amritsar juction 2
New Delhi 4
Note: Values may be written in any order
(b) (i)
VICE PRINCIPAL 01
(ii) 16
(iii)
UMESH
YASHRAJ
(iv)
5 MALE
2 FEMALE
Question 27. Write a method/function DISPLAYWORDS() in python to read lines from a text file
STORY.TXT, and display those words, which are less than 4 characters. [3]
OR
Write a function CountHisHer() in Python which reads the contents of a text file “Story.txt” and counts the
words His and Her (not case sensitive).
Answer:
def DISPLAYWORDS():
C=0
file=open (1 STORY. TXT' , 'r')
1 ine=file . read ()
word = line, split ()
for w in word:
if len(w)<4:
print(w)
file . close ()
OR
def CountHisHer():
for line in open('story.txt'):
# Searching for a word in a file
wordlist=line.strip()
count =0
for w in wordlist:
words = word.split(" ")
for w in words:
# Remove all leading and trailing white spaces
w =w.strip().lower()
if w == 'his' or w=='her':
count = count + 1
if count == 0:
print ("not found in file")
else:
print ("count=",count)
Question 28. (a) Write SQL queries for (i) to (ii) based on the table EMPLOYEE and DEPARTMENT given
below:
Table: EMPLOYEE
Table- DEPARTMENT
DEPTID DEPTNAME FLOORNO
D001 Personal 4
D002 Admin 10
D003 Production 1
D004 Sales 3
(i) To display the average salary of all employees, department wise.
(ii) To display name and respective department name of each employee whose salary is more than 5000.
(iii) To display the name of employees whose salary not known, in alphabetical order.
(iv) To display DEPTID from the table EMPLOYEE without repetition.
(b) Which clause is used to remove the duplicating rows of the table? [3]
Answer:
(a)(i) SELECT AVG (SALARY)
FROM EMPLOYEE GROUP BY DEPTID;
(ii) SELECT NAME, DEPTNAME FROM EMPLOYEE, DEPARTMENT WHERE EMPLOYEE.DEPTID
=DEPARTMENT.
DEPTID AND SALARY>5000;
(iii) SELECT NAME FROM EMPLOYEE WHERE SALARY IS NULL ORDER BY NAME;
(iv) SELECT DISTINCT DEPTID FROM EMPLOYEE;
(b) DISTINCT
Question 29. Write the definition of a method ZeroEnding (SCORES) to add all those values in the list of
SCORES, which are ending with zero (0) and display the sum.
For example:
If the SCORES contain [200,456, 300,100,234,678]
The sum should be displayed as 600 [3]
Answer:
def ZeroEnding(SCORES) :
SZero = 0
for i in SCORES:
if i % 10 == .0:
SZero += i
print ("sum of numbers ending with zero: ", SZero)
Question 30. Write a function to push any student’s information to stack. [3]
OR
Write a program to input n numbers and to search any number from the list.
Answer:
def push(stack):
s= []
print ("STACK BEFORE PUSH") display(stack)
s.append(input ("Enter student rollno?"))
s.append(raw_input ("Enter student name"))
s.append (raw_input ("Enter student grade"))
stack.append(s)
def display (stack);
l=len (stack)
print ("STACK CONTENTS")
for i in range (1-1,-1,-1):
print stack [i] stack= []
print "Creating stack"
n = input ("Enter the number of students")
for i in range (n) :
push (stack)
display (stack)
OR
n=input("Enter no. of values")
num= []
flag=0
for i in range (n) :
number=input("Enter the number")
num.append(number)
search = input("Enter number to be searched")
(b)
import MySQLdb
db = MySQLdb.connect('localhostHRM an','HRman@pwd','compvtLtd')
cursor=db.cursor()
cursor.execute("Drop Table IF Exists Employee")
sql="'"'Create Table Employee('Emp_ id' INT NOTNULL, 'Emp_name' char(50)
NOTNULL, 'Dept' char(20)
NOTNULL, 'Age1 INT NOTNULL)"""
cursor.execute(sql)
cursor.execute("""create index eid on user,(Emp_Id)""")
cursor.commit()
cursor.close ()
db. close ()
OR
(a) Output: 3 Working:
x+=x-x will be calculated as x=3+3-3
Hence, final value of x=3
Commonly Made Error
Students sometime get confused in unary and ! binary operators.
Answering Tip
Students should learn the proper working of unary and binary operators properly.
(b)
import MySQLdb
db=MySQLdb.connect ("localhost", "Admin", "Adl23", "HMD")
cursor=db.cursor()
cursor.execute("DROP TABLE IF EXISTS Location")
sql="""Create Table Location (id Numeric (5) PRIMARY KEY, bldg_code varchar(lO) Not Null, room
varchar(6) Not Null, capacity Numeric (5) Not Null)"""-
cursor.execute(sql)
db. close ()
Question 33. Write the full form of CSV.
Write a function AMCount() in Python, which should read each character of a text file STORY.TXT, should
count and display the occurrence of alphabets A and M (including small cases a and m too). A i
Example:
If the file content is as follows:
Updated information
As simplified by official websites.
The AMCount() function should display the output as:
A or a:4
M or m:2 [5]
OR
Write any two needs for a data file.
A file sports.dat contains information about a formal Event Participant.
Write a function that would read contents from file sports.dat and creates a file named Athletics.dat copying
only those records from sports.dat where the event name is “Athletics” A.
Answer: CSV – Comma Separated Value
PROGRAM
def AMCount:
f = open("story.txt", "r")
A,M = 0, 0
r = f.read()
for x in r:
if x[0] == "A" or x[0] == "a":
A=A+1
elif x[0] == "M" or x[0] == "m":
M=M+1
f.close ()
print("A or a: ", A)
print("M or m: ", M)
OR
Need for a data file:
(i) It is a convenient way to deal with large quantities of data.
(ii) To avoid input of data multiple times during program execution.
PROGRAM
def athletics ( ) :
file1 = open ( "sports . dat" r')
file2 = open ( "Athletics . dat" w')
rec = filel. readlne ()
while rec! = " ":
sport = rec.split('n')
if sport[0] == "Athletics":
file2. write (rec)
file2 .write ( '\n' )
else:
pass
rec = file1. readlne ()
file1. close ()
file2 . close ()
return
Commonly Made Error
Indentation is missing while programming by some students.
Answering Tip
Indentation must be taken care of and should be ’ checked on completion of the code.
Section – E
Question 34. A departmental store MyStore is considering to maintain their inventory using SQL to store the
data. As a database administer, Abhay has decided that: [1+1+2]
Name of the database – mystore
Name of the table – STORE
The attributes of STORE are as follows:
ItemNo- numeric
ItemName – character of size 20
Scode – numeric ,
Quantity – numeric
Table: STORE
(i) Identify the attribute best suitable to be declared as a primary key. [1]
(ii) Write the degree and cardinality of the table STORE. [1]