Important Questions

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

CBSE CLASS XII

OUTSIDE DELHI : 2006

Q. 4a. void main( )
{ char ch = ‘A’ ;
fstream fileout(“data.dat”, ios :: app) ;
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)4 (Since, the file is opened in app mode, it retains the previous content also, if the file mode is out, then result will be
0 since it will loose all the old content of the file.)

Q. 4b. Write a function to count the number of blanks present in a text file named “PARA.TXT” .

Solution:

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

Q. 4c. Following is the structure of each record in a data file named “PRODUCT.DAT” .
struct PRODUCT
{ char Product_Code[10] ;
char Product_Description[10] ;
int Stock ;
} ;
Write a function in C++ to update the file with a new value of Stock. The Stock and the
Product_Code, whose Stock to be updated, are read during the execution of the program.

Solution:

void Update( )
{ fstream finout(“PRODUCT.DAT”,ios::in|ios::out);
PRODUCT P;
finout.seekg(0);
while(finout)
{ finout.read((char *)&P, sizeof(P));
cout<<”\nThe Product Code is “<<P.Product_Code;
cout<<”\nThe Product Description is ”<<P.Product_Description;
cout<<”\nEnter the Stock: “;
cin>>P.Stock;
finout.seekp(finout.seekp( )-sizeof(P));
finout.write((char *)&P,sizeof(P));
}
}

DELHI : 2005

Q. 4a. Observe the program segment given below carefully, and answer the question that
class Book
{ int Book_no :
char Book_name[20] ;
public ;
//function to enter Book details
void enterdetails( ) ;
//function to display Book details
void showdetails( ) ;
//function to return Book_no
int Rbook_no( ) {return Book_no ;}
} ;
void Modify (Book NEW)
{ fstream File ;
File.open(“BOOK.DAT”, ios :: binary l ios :: in l ios :: out) ;
Book OB ;
int Record = 0, Found = 0 ;
while (!Found && File.read((char*) &OB, sizeof(OB) ) )
{ Recordsread++ ;
if (NEW.RBook_no( ) == OB.RBook_no( ))
{ ___________ //Missing Statement
File.write((char*) &NEW, size of(NEW)) ;
Found = 1 ;
}
else
File.write((char*) &OB, sizeof(OB)) ;
}
if (!Found)
cout << “Record for modification does not exist” ;
File.close( ) ;
}
If the function Modify( ) is supposed to modify a record in file BOOK.DAT with the values of Book
NEW passed to its argument, write the appropriate statement for Missing Statement using seekp( ) or
seekg( ), whichever needed, in the above code that would write the modified record at its proper
place.
4.b)Write a function in C++ to count and display the number of lines starting with alphabet ‘A’
present in a text file “LINES.TXT”.

Example :

If the file “LINES.TXT” contains the following lines,
A boy is playing there.
There is a playground.
An aeroplane is in the sky.
Alphabets and numbers are allowed in the password.
The function should display the output as 3
4.c)Given a binary file STUDENT.DAT, containing records of the following class Student type
class Student
{ char S_Admno[10] ; //Admission number of student
char S_Name[30] ; //Name of student
int Percentage ; //Marks Percentage of student
public :
void EnterData( )
{ gets(S_Admno) ; gets(S_Name) ; cin >> Percentage ;
}
void DisplayData( )
{ cout << setw(12) << S_Admno ;
cout << setw(32) << S_Name ;
cout << setw(3) << Percentage << endl ;
}
int ReturnPercentage( ) {return Percentage ;}
} ;
Write a function in C++, that would read contents of file STUDENT.DAT and display the details
of those Students whose Percentage is above 75.

 


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