You are on page 1of 15

Delhi Public School, R K Puram

COMPUTER SCIENCE
Half Yearly Examination 2015 Class 12 [Set 1]Marking Scheme

Max marks: 70
1.

Time: 3
Hrs
Find invalid variable names from the following alongwith reason for being invalid.
[2]

(a) MyMarks
(d) while

(b)Class
(e) 12thClass

Ans.(a)MyMarkscontainsspace
(d)whilekeyword
(e)12thClassstartswithdigit
(f)Sales+Taxcontains+

(c)_YourMarks
(f ) Sales+Tax
[]
[]
[]
[]

Note: Deduct [] mark for every incorrect option not exceeding [2] marks
2.

Differentiate between the following with suitable example.


(a) Static and Dynamic data Structures
(b) Stack and Queue

Ans.

Refer Handout:
(a) REF:DPSR/COMP12/20152016/11 Page 1
(b) REF: DPSR/COMP12/20152016/12 Page 5 and 6

[2x2=4]

[1] mark for correct difference in each question


[1] marks for correct supporting example in each question
3.Which header files are required for the following code:

[2]

voidmain()
{
intN[5]charS[5][10]
cout<<Enter5integers
for(inti=0i<5i++)
{
cin>>N[i]
itoa(N[i],S[i],10)
}
for(i=0i<5i++)
{
intL=strlen(S[i])
cout<<S[i][L1]<<setw(10)<<N[i]<<endl
}
}

1/15
DPSR/20152016/HY/CS_MS

iostream.h
stdlib.h
string.h
iomanip.h

4.

Find the outputs of the following (Assume all necessary header files are included):
(i)
[2]

voidEnCode(charMSG[])
{
for(intI=0MSG[I]!=\0I++)
{
if(I%2)
MSG[I]=(islower(MSG[I]))?toupper(MSG[I]):%
else
MSG[I]=(isupper(MSG[I]))?tolower(MSG[I]):@
}
}

voidmain()
{
charTXT[]=HalFYearLY
EnCode(TXT)
cout<<TXT<<endl
}

Ans hA@%yE@Rl%
[1] for all correct changes in odd locations
[1] for all correct changes in even locations

[]
[]
[]
[]

Ans.

cout, cin
itoa()
strlen()
setw()

(ii)

[2]

#defineChangeDig(X)((X48)+3)%10+48
#defineChangeAlp(X)((X65)+2)%26+65
voidmain()
{
charMSG[]="YEAR2015"
for(inti=0MSG[i]!='\0'i++)
{
if(isdigit(MSG[i]))

MSG[i]=ChangeDig(MSG[i])

else

MSG[i]=ChangeAlp(MSG[i])
}
cout<<MSG<<endl
}

2/15
DPSR/20152016/HY/CS_MS


Ans

AGCT5348

[]
[]
[]
[]

for
for
for
for

AG
CT
53
48

(iii)

voidSwap(int&X,int&Y)
{
intT=X
X=Y
Y=T
}
voidmain()
{
intA[3][3]=
{{1,2,3},{4,5,6},{7,8,9}}
for(intI=0I<=1I++)
for(intJ=I+1J<=2J++)
Swap(A[I][J],A[J][I])//1
//
for(I=0I<=2I++)
Swap(A[I][0],A[I][2])//2
//
for(I=0I<=3I++)
{
for(J=0J<=3J++)
cout<<A[I][J]
cout<<endl
}
}

Ans 741

852
963

[1]
[1]
[1]

(iv)

voidmain()
{
intArr[]={10,20,30,40}
int*P=Arr,I,J
for(I=0I<3I++)
{
(*P)+=(*P)%2
for(J=0J<=IJ++)

cout<<Arr[J]<<:
P++
cout<<endl
}
3/15
DPSR/20152016/HY/CS_MS

Ans

10:
10:20:
10:20:30:

[]
[] + []
[] + [] + []

Note: Deduct [] marks for not mentioning : after any or all values
5.

Rewrite the following program after correcting all the syntactical errors.
Underline each correction made.
[4]
#include<iostream.h>
voidMain()
{
intNumcharWord
gets(Word)
Reverse(Word)
}
voidReverse(String)
{
Num=strlen(String)
for(intI=Num1I>=0I)
cout<<String(I)
}

Ans
#include<iostream.h>
#include<stdio.h>
[]
#include<string.h>
[]
voidReverse(char[])
[]
voidmain()
[]
{
intNumcharWord[20]
[]
gets(Word)

Reverse(Word)

}
voidReverse(charString[])
[]
{
intNum=strlen(String)
[]
for(intI=Num1I>=0I)

cout<<String[I]
[]
}

6.

Given the following code, choose the correct option(s) from (a) to (d) for
output of the code. Write Minimum and Maximum values that can be
assigned to variable L.
[2]
#include<iostream.h>
#include<stdlib.h>

4/15
DPSR/20152016/HY/CS_MS


voidmain()
{constintMax=3
randomize()
intA[Max]={10,20,30}
intL=random(Max)
if(L%2)
A[L]=A[L]+1
else
A[L]=A[L]1
for(intI=0I<MaxI++)
cout<<A[I]<<#
}

(a)9#21#29# (b)9#20#29#
(c)10#21#30# (d)11#20#31#
Ans L(Min)=0

[]

L(Max)=2
CorrectOption(c)

[]
[1]

Justification
L
0
1
2
7.

Output
Option
9#20#30#
None
10#21#30# (c)
10#20#29# None

Answer the questions (i) to (iv) after going through the following class:
classWorkshop
{
intSession
public:
Workshop(intN)
//Function1
{
Session=N
cout<<"Workshopssession"<<Session<<end1
}
voidLecture()
//Function2
{
cout<<"Lecturesintheworkshopon"<<end1
}
~Workshop()
//Function3
{
cout<<"Voteofthanks"<<end1
}

[4]

5/15
DPSR/20152016/HY/CS_MS

voidmain()
{
____________________ //statement1
____________________
//statement2
}
(i)
In Object Oriented Programming, what is Function 3 referred as and
when does it get invoked/ called?
(ii)
Write statement 1 to declare an object W of class Workshop.
(iii)
Write statement 2 to invoke Function 2 for object W.
(iv)
For correctly written statements 1 and 2, what will be the output of
the above code?
Ans

(i)
(ii)
(iii)
(iv)

8.

Destructor
It gets invoked when the scope of an object gets over
Workshop W(1);
W.Lecture();
Workshopssession1
Lecturesintheworkshopon
Voteofthanks

[]
[]
[]
[]
[1]
[]
[]

Define a class Olympiad in C++ with the following specifications :


[4]
Private members
Ono
integer
Odesc
20 characters
Score
integer
Status
char
Public members
A constructor to initialize:
Ono as 1001, Odesc as None, Score as 0 and Status as X
Get( ) function to input Ono, Odesc and Score
Assign(int PassMarks) to assign Status as Y if Score is more than
PassMarks (passed as argument), else assign N
Show( ) function to display all the data members on the screen

Ans

classOlympiad
{
intOno
charOdesc[20]
intScore
6/15
DPSR/20152016/HY/CS_MS

charStatus
public:
Olympiad()
voidGet()
voidAssign(int)
voidShow()
}

[1]

Olympiad::Olympiad()
{
Ono=1001
strcpy(Odesc,None)
Score=0
Status=X
}

voidOlympiad::Get()
{
cin>>Ono
gets(Odesc)
cin>>Score
}

voidOlympiad::Assign(intPassMarks)
{
if(Score>PassMarks)
Status=Y
else
Status=N
}

voidOlympiad::Show()
{
cout<<Ono<<Odesc<<Score<<Status<<endl
}

[1]

[]

[1]

[]

7/15
DPSR/20152016/HY/CS_MS

9.

Given the following class declarations:


[4]
classBank
{
intCapital
public:
voidGet_data()
voidDisplay()
}
classLoans:protectedBank
{
protected:
charType[20]
public:
floatCalc()
voidShow()
}
classFunds:publicLoans
{
floatValue
public:
floatInterest(int)
}

voidmain()
{
FundsF
_________
_________
}

(i)
What type of inheritance is illustrated above?
(ii)
Write the names of members accessible to Interest() of class Funds.
(iii)
Is the function Get_data( ) of class Bank accessible to objects of class
Loans? Justify your answer.
(iv)
Write the names of members accessible to Object F of class Funds in
the main().

Ans

(i)
(ii)

(iii)

(iv)

Multilevel Inheritance
[1]
Value of class Funds
Calc(), Show(), Type of class Loans
Get_data( ,)Display( ) of class Bank
[1]
No
[]
Get_data() of class Bank is inherited as protected member in class
Loans
[]
Interest() of class Funds
8/15
DPSR/20152016/HY/CS_MS

Calc(), Show() of class Loans


10.

[1]

Assuming that a text file named STORY.TXT contains some text written into
it, write definition for a function named ALTERTEXT(), that reads the file
STORY.TXT and creates a new file named NEWSTORY.TXT, which shall
contain only those lines from the file STORY.TXT which start with a
lowercase vowel (i.e. with a,e,i,o,u).
For example if the file STORY.TXT contains:
[2]
Anelephantwasplayingwithaball
andanantwentforwalk.

Then the file NEWSTORY.TXT shall contain :


andanantwentforawalk.

Ans

voidALTERTEXT()
{
ifstreamfile1(STORY.TXT)
ofstreamfile2(NEWSTORY.TXT)
charLine[80]
while(file1.getline(Line,80))
{
switch(Line[0])
{
casea:
casee:
casei:
caseo:
caseu:
file2<<Line<<endl
}
}
file1.close()
file2.close()
}
11.

[]
[]
[]

[]

Write a function EconomyDoc() to search and display details of those


Doctors whose Speciality is ENT and whose Fee is less than 500, from a
binary file DOCTORS.DAT that contains objects of class Doctor, which is

9/15
DPSR/20152016/HY/CS_MS

defined below:

[3]

classDoctor
{
charDName[10]
charSpeciality[16]
floatFee
public:
voidGetdata()
{gets(DName)gets(Speciality)cin>>Fee}
voidShowdata()
{cout<<DName<<:<<Speciality<<:<<Fee<<endl}
char*RSpeciality(){returnSpeciality}
floatRFee(){returnFee}
}
Ans

12.

voidEconomyDoc()
{
ifstreamfile(DOCTORS.DAT,ios::binary) []
DoctorD
while(file.read((char*)&D,sizeof(D)))
[1]
{
if(strcmp(D.RSpeciality(),ENT)==0&&D.RFee()<500)[1]
D.Showdata()
[]
}
file.close()
}
Assuming that the binary file DOCTORS.DAT as mentioned in the previous

question contains the following 4 records of class Doctor:


DName

Speciality

Fee

R.Kumar

Pediatric

S.Singh
M.Rao

Dental
Skin

500
700

S.Sharma

ENT

450

1200

Write the output of the following program segment:


fstreamFileDoctorD
File.open(DOCTORS.DAT,ios::binary|ios::in)
for(intRec=1Rec<=4Rec++)
{
File.seekg((Rec1)*sizeof(D))

[2]

10/15
DPSR/20152016/HY/CS_MS

File.read((char*)&D,sizeof(D))
if(D.RFee()<=500)
cout<<Found@<<File.tellg()sizeof(D)<<endl
else
cout<<Oops...<<endl
}
Ans

13.

Ans

Oops...
Found@30
Oops...
Found@90

[]
[]
[]
[]

Write definition for a function intIspalin(charS[]), which returns 1


if the string S passed to the parameter is a palindrome otherwise it should
return 0.
[3]
Note:
A palindrome is a word which is same as its reverse eg. MADAM
Do not use any predefined library function of C++
Do not use any other array for manipulation

intIspalin(charS[])
{
for(intL=0S[L]!=\0L++)
intR=1
for(inti=0i<L/2i++)
if(S[i]!=S[L1i])
R=0
returnR
}
[1] mark for correct loop(s)
[1] mark for correct check
[1] mark for returning correct value

14.

Write definition for a function int SumEnd2(int A[], int N), which
returns the accumulated sum of all those elements, which are ending with 2
of the array A. For example if the array contains.
[3]
52

52 +

13

42

12

42+ 12+

=106

Then the function should SumEnd2 should return 106:

11/15
DPSR/20152016/HY/CS_MS

Ans.
intSumEnd2(intA[],intN)
{
intS=0
for(inti=0i<Ni++)
{
if(A[i]%10==2)
S+=A[i]
}
returnS
}

[1] mark for correct loop


[1] mark for correct check
[1] mark for returning correct value
15.

Write definition for a function intSumNonD(intA[4][4]) which returns


the sum of non diagonal elements of the array.
[3]
Example:
Array

Function returns 68

5+

8+

9
13

10
14

11
15

12
16

9+

12+

2+ 3+

14+ 15
=68

Ans
intSumNonD(intA[4][4])
{
intS=0
for(inti=0i<4i++)
for(intj=0j<4j++)
if(i!=j&&(i+j)!=3)
S+=A[i][j]
returnS
}

[1] mark for correct loop(s)


[1] mark for correct check
[1] mark for returning correct value

12/15
DPSR/20152016/HY/CS_MS

16.

n array P[2..4][3..6] is stored in the memory along the row with each of the
A
element occupying 2 bytes. Find out the address of element P[3][2], if the base
address of P is 25000.
[3]

Ans
Loc(A[i][j])Row=BA+W[(iLBR)*C+(jLBC)]
Where,
BA=25000
W=2
LBR=2
LBC=3
C=6[3]+1=10

Loc(A[3][2])Row
=
25000+2[(3(2))*10+(2(3))]
=
25000+2[50+1]
=
25000+102
=
25102

[1] mark for writing correct formula/ substituting correct values


[1] mark for calculations
[1] mark for correct final result
17.

An array T[10][5] is stored in the memory in column major form, with each
element occupying 4 bytes of memory. If the address of T[2][3] is 15000,
then calculate the address of the location T[6][2].
[3]

Ans
Loc(A[i][j])Col=BA+W[(iLBR)+(jLBC)*R]
Where,

W=4
LBR=0
LBC=0
R=10
Loc(A[2][3])Col
=15000
=BA+4[2+3*10]
=BA+4[32]
=BA+128

BA=15000128=14872
Loc(A[6][2])Col
=14872+4[6+2*10]
=14872+4[26]
=14872+104
=14976

[1] mark for writing correct formula/ substituting correct values


[1] mark for calculations

13/15
DPSR/20152016/HY/CS_MS

18.

[1] mark for correct final result


Evaluate the following Postfix Notation of Expression (Show status of stack
after each operation)
[2]
45,20,10,+,,12,10,,*

Ans
Element

StackContents

45

45

20

45,20

10

45,20,10

45,30

15

12

15,12

10

15,12,10

15,2

30

[]
[]
[]
[]
19.

Ans

Mark
Mark
Mark
Mark

till+
till
till(second)
till*

Convert the following to Postfix notation of expression (Show status of stack after
each operation)
PQ/(R+S)*T

[2]
PQ/(R+S)*T
=
P(Q/(R+S))*T
=
P((Q/(R+S))*T)
=
(P((Q/(R+S))*T))

Element

StackofOperators

PostfixExpression

PQ

PQR

/+

PQRS

PQRS+

PQRS+/

PQRS+/T

14/15
DPSR/20152016/HY/CS_MS

PQRS+/T*

PQRS+/T*

[ ] mark forRS+
+ [ ] mark tillQRS+/
+ [ ] mark tillQRS+/T*
+ [ ] mark tillPQRS+/T*
20.(a) Define a function in C++ to display and remove an integer from a Static
Circular Queue, which can contain a maximum of 10 integers:
[4]
Ans
Refer [REF: DPSR/COMP12/20152016/12] Page 6
[1] mark for correct function header
[1] mark for checking empty queue
[1] mark for incrementing front
[1] mark for displaying correct queue content removed
(b) Define a function in C++ to push a Book into a static stack of Books, which
can contain maximum 25 Books. The structure of Book is as follows:
[4]
structBook
{
intBNo
charBName[20]
}

Ans
Refer [REF: DPSR/COMP12/20152016/12] Page 5
[1] mark for correct function header
[1] mark for checking full stack
[1] mark for incrementing top
[1] mark for inserting correct stack content at top

15/15
DPSR/20152016/HY/CS_MS

You might also like