Important Questions

CBSE Guess > Papers > Important Questions > Class XII > 2010 > Computer Science > Computer Science By Ravi Kiran

CBSE CLASS XII

OUTSIDE DELHI : 2007

Q. 4. a. Observe the program segment given below carefully,and answer the question that follows:
class Labrecord
{ int Expno ;
char Experiment[20] ;
char Checked ;
int Marks ;
public :
void EnterExp( ) ; //function to enter Experiment details
viod ShowExp( ) ; //function to display Experiment details
char RChecked( ) //function to return Expno
{return Checked ;}
void Assignmarks (int M) //function to assign Marks
{ Marks = M ; }
} ;
void ModifyMarks( )
{ fstream File ;
File.open (“Marks.Dat”, ios :: binary l ios :: in l ios :: out) ;
Labrecord L ;
int Rec=0 ;
while (File.read ( (char*) &L,sizeof (L) ) )
{ if (L.RChecked( )= =’N’)
L.Assignmarks (0)
else
L.Assignmarks (10)

File.seekp(File.tellp( )-sizeof(L)); //Statement 1
//File.seekp(Rec*sizeof(L));
File.write((char*)&L,sizeof(L)); //Statement 2
//File.write((char*)&L,sizeof(Labrecord));

Rec++ ;
}
File.close( ) ;
}
If the function ModifyMarks ( ) is supposed to modify marks for the records in the file
MARKS.DAT based on their status of the member Checked (containg value either ‘Y’ or
‘N’).Write C++ statements for the statement 1 and statement 2,where, statement 1 is required
to position the file write pointer to an appropriate place in the file and statement 2 is to perform
the write operation with the modified record.

Q. 4.b. Write a function in C++ to print the count of the word the as an independent word in a text file STORY.TXT.
For example,if the content of the file STORY.TXT is
There was a monkey in the zoo.The monkey was very naughty.
Then the output of the program should be 2.

Q. 4.c. Given a binary file SPORTS.DAT,containg records of the following structure type :
struct Sports
{ char Event[20] ;
char Participant[10][30] ;
} ;
Write a function in C++ that would read contents from the file SPORTS.DAT and creates a file named
ATHLETIC.DAT copying only those records from SPORTS.DAT where the event name is “Athletics”.

Solution:

void AthletsList( )
{ ifstream fin(“SPORTS.DAT’,ios::in,ios::binary););
ofstream fout(“ATHLETIC.DAT”,ios::out|ios::binary);
Sports S;
while(fin) // or while(!fin.eof( ))
{ fin.read((char*)&S,sizeof(Sports));
if(strcmp(S.Event,”Athletics”)= = 0)
fout.write((char*)&S,sizeof(S));
}
fin.close( );
fout.close( );
}

DELHI : 2006

Q. 4a. void main( )
{ char ch = ‘A’ ;
fstream fileout(“data.dat”, ios::out) ;
fileout<<ch ;
int p = fileout.tellg( )
cout<<p ;
}
What is the output if the file content before the execution of the program is the string “ABC”
(Note that “ “ are not part of the file).
Ans) 1 (Since, the file is opened in out mode, it looses all the previous content, if the file mode is app, then result will be 4)
4.b)Write a function to count the number of words present in a text file named “PARA.TXT”.
Assume that each word is separated by a single blank/space character and no blanks/spaces in the
beginning and end of the file.

Solution:

void WordsCount( )
{ clrscr( );
ifstream fin("PARA.TXT",ios::in);
char ch;
int Words=1;
if(!fin)
{ cout<<”No words at all in the file”;
exit(0);
}
while(fin)
{fin.get(ch);
if(ch= =’ ‘)
Words++;
}
cout<<"\nTotal number of Words in the file = "<<Words;
getch( );
}

Q. 4c. Following is the structure of each record in a data file named “COLONY.DAT”
struct COLONY
{ char Colony_Code[10] ;
char Colony_Name[10]
int No_of_People ;
} ;
Write a function in C++ to update the file with a new value of No_of_People. The value of
Colony_Code and No_of_People are read during the execution of the program.

Solution:

void Update( )
{ fstream finout(“COLONY.DAT”,ios::in|ios::out);
COLONY C;
finout.seekg(0);
while(finout)
{ finout.read((char *)&C, sizeof(C));
cout<<”\nThe Colony Code is “<<C.Colony_Code;
cout<<”\nThe Colony Name is”<<C.Colony_Name;
cout<<”\nEnter the Number of People”;
cin>>C.No_of_People;
finout.seekp(finout.seekp( )-sizeof(C));
finout.write((char *)&C,sizeof(C));
}
}

 


Paper By Mr. Ravi Kiran
Email Id : [email protected]