Chapter 3: Data File Handling | class 12th | Important question Computer Science
Important Questions for Class 12 Computer Science – Data File Handling
Question 1:
Find the output of the following C++ code considering that the binary file CLIENTS.DAT exists on the hard disk with a data of 200 clients: All India 2017
class CLIENTS
{
int CCode;char CName[20];
public:
void REGISTER(); void DISPLAY();
}:
void main()
(
fstream File;
File.open("CLIENTS.DAT",ios::binary|ios::in);
CLIENTS C;
File.seekg(6*sizeof(C));
File.read!(char*)&C, sizeof(C)):
cout<<"Client Number:"<<File.tellg()/sizeof(C)+1;
File.seekg(0,ios::end);
cout«"of"<<File.tellg()/sizeof(C)<<endl;
File.close();
}
Answer:
Output
Client Number:8 of 200
Question 2:
Find the output of the following C++ code considering that the binary file MEM.DAT exists on the hard disk with a data of 1000 members: Delhi 2016
class MEMBER
{
int Mcode;char MName[20];
public:
void Register();void Display();
};
void main()
{
fstream MFile;
MFile.openCMEM.DAT", ios ::binary| ios :: in); .
MEMBER M;
MFile.read((char*) &M. sizeof(M));
cout<<"Rec :"<<MFile.tellg()/sizeof(M)<< endl;
MFile.read!(char*) &M, sizeof(M));
MFile.read((char*) &M, sizeof(M));
cout<<"Rec :"<<MFile.tellg()/sizeof(M)<<endl;
MFile. close();
}
Answer:
Output
Rec : 1 Rec : 3
Question 3:
Find the output of the following C++ code considering that the binary file CLIENT.DAT exists on the hard disk with a data of 1000 clients: All India 2016
class CLIENT
{
int Ccode; char CName[20];
public:
void Register(); void Display():
};
void main()
{
fstream CFile;
CFile.open("CLIENT.DAT",ios::binary|ios::in);
CLIENT C;
CFile.read((char*)&C, sizeof(C));
cout<<"Rec: "<<CFile.tel 1 g()/sizeof (C)<<endl;
CFile.read((char*)&C, sizeof(C));
CFile.read((char*)&C, sizeof(C)):
cout<<"Rec:"<<CFile.tellg()/sizeof(C)<<endl;
CFile.close():
}
Answer:
Output
Rec: 1 Rec: 3
Question 4:
Find the output of the following C++ code considering that the binary file CLIENT.DAT exists on the hard disk with records of 100 members. Delhi 2016
class CLIENTS
{
int Cno; char Name[20];
public:
void In(); void Out();
}:
void main()
{
fstream CF;
CF.open("CLIENTS.DAT", ios::binary|ios: :in);
CLIENTS C;
CF.read((char*)&C,sizeof(C));
CF.read((char*)&C,sizeof(C));
CF.read((char*)&C,sizeof(C));
int POS=CF.tellg()/sizeof(C);
cout<<"PRESENT RECORD: "<<P0S<<endl;
CF.close();
}
Answer:
Output
PRESENT RECORD:3
Question 5:
Find the output of the following C++ code considering that the binary file MEMBER.DAT exists on the hard disk with records of 100 members. All India 2015
class MEMBER
{
int Mno; char Name[20];
public:
void In(); void Out();
};
void main()
{
fstream MF;
MF.openC"MEMBER.DAT", ios::binary|ios::in);
MEMBER M;
MF.read((char*)&M. sizeof(M));
MF.read((char*)&M, sizeof(M));
MF.read((char*)&M, sizeof(M));
int P0SITI0N=MF.tel1g()/sizeof(M);
cout<<”PRESENT RECORD: ”<<P0SITI0N<<endl ;
MF.close();
}
Answer:
Output
PRESENT RECORD:3
Question 6:
Find the output of the following C++ code considering that the binary file GAME. DAT exist on the hard disk with information of around 200 games. All India 2015 c
class GAME
{
int Gno; char GName[20];
public:
void Getln(); void ShowName();
};
void main()
{
fstream GF;
GF.open("GAME.DAT",ios::binary | ios::in);
GAME G;
GF.seekg(sizeof(G)*5) ;
GF.read((char*)&G, sizeof(G)):
GF.readC(char*)&G, sizeof(G));
int REC0RDN0=GF.tel 1g()/sizeof(G);
cout<<"REC0RDN0: "<<REC0RDN0<<endl;
GF.close();
}
Answer:
Output
REC0RDN0:7
Question 7:
Fill in the blanks marked as Statement 1 and Statement 2 in the program segment given below with appropriate functions for the required task. Delhi 2014
class Medical
{
int RNo; //Representative Code
char Name[20]; //Representative Name
char Mobile[12]; //Representative Mobile
public:
void Input(); //Function to enter all details
void Show(); //Function to display all details
int RRno()
{
return RNo;
}
void ChangeMobile() //Function to change Mobile
{
cout<<”Changed Mobile:";
gets(Mobile);
}
};
void RepUpdate()
{
fstream F;
F.open("REP.DAT", ios::binary|ios::in|ios::out);
int Change=0;
int URno;
cout<<"Rno(Rep No-to update Mobile):";
cin>>URno;
Medical M;
while(!Change && F.read((char*)&M,sizeof(M)))
{
if(M.RRno() == URno)
{
//Statement l:To call the function to change Mobile No.
____________________________________;
//Statement 2:To reposition file pointer to re-write
//The updated object back in the file
____________________________________;
F.write!(char*)&M,sizeof(M));
Change++;
}
}
if(Change)
cout<<"Mobile Changed for Rep"<<URno<<endl;
else
cout<<"Rep not in the Medical "<<endl;
F.close();
}
Answer:
Statement 1
M.ChangeMobile()
Statement 2
F.seekp(-l*sizeof(M),ios::cur)
Question 8:
Fill in the blanks marked as Statement 1 and Statement 2 in the program segment given below with appropriate functions for the required task. All India 2014
class Agency
{
int ANo; //Agent code
char AName[20]; //Agent name
char Mobile[30]; //Agent mobile
public:
void Enter(); //Function to enter details of agent
void Disp(); //Function to display details of agent
int RAno() {return ANo;
}
void UpdateMobileO //Function to update Mobile
{
cout<<"Updated Mobile:";
gets(Mobile);
}
};
void AgentUpdateC)
{
fstream F;
F.open("AGENT.DAT",ios::binary|ios::in|ios::out);
int Updt=0;
int UAno;
cout<<"Ano (Agent No-to update Mobile):";
cin>>UAno;
Agency A;
whileUUpdt && F.read((char*)&A,sizeof(A)))
{
if (A.RAnoO == UAno)
{
//Statement 1:To call the function to Update Mobile No.
____________________________________;
//Statement 2:To reposition file pointer to re-write
//The updated object back in the file
___________________________________;
F.write((char*)&A,sizeof(A));
Updt++;
}
}
if(Updt)
cout<<"Mobile Updated for Agent"<<UAno<<endl;
else ,
cout<<"Agent not in the Agency"<<endl;
F.close();
}
Answer:
Statement 1
A.UpdateMobile()
Statement 2
F.seekpC-l*sizeof(A),ios::cur)
Question 9:
Fill in the blanks.marked as Statement 1 and Statement 2 in the program segment given below with appropriate functions for the required task. All India 2013
class Club
{
long int MNo; //Member number
char MName[20]; //Member name
char Email[30]; //Email of member
public:
void Register(); //Function to register member
void Disp(); //Function to display details
void ChangeEmail() //Function to change Email
{
cout<<"Enter Changed Email:";
cin>>Email;
}
long int GetMno()
{
return MNo;
}
};
void ModifyData()
{
fstream File;
File.open("CLUB.DAT",ios::binary|ios::in|ios::out);
int Modify = 0, Position;
long int ModiMno;
cout<<MMno-whose email required to be modified:";
cin>>ModiMno;
Club CL;
whi1e(!Modify&&Fi1e.read((char*)&CL,sizeof(CL)))
{
if(CL.GetMno() == ModiMno)
{
CL.ChangeEmail();
Position = File.tellgO-sizeof(CL);
//Statement 1:To place file pointer to the required position
____________________________________;
//Statement 2:To write the object CL onto the binary file
____________________________________;
Modify++;
}
}
if(Modify)
cout<<"Email Changed..."<<endl;
else
cout<<"Member not found.. ."<<endl;
File.close();
}
Answer:
Statement 1
File.seekp(Position)
Statement 2
File.write((char*)&CL,sizeof(CL))
Question 10:
Fill in the blanks marked as Statement 1 and Statement 2 in the program segment given below with appropriate functions for the required task. Delhi 2013
class Customer
{
long int CNo; //Customer number
char CName[20]; //Customer name
char Email[30]; //Email of customer
public:
void Allocate(); //Function to allocate member
void Show(); //Function to show customer data
void ModifyEmail() //Function to modify Email
{
cout<<Enter Modified Email:";
gets(Email);
}
long int GetCno()
{
return CNo;
}
};
void ChangeData()
{
fstream File;
File.open("CUST.DAT", ios::binary|ios::in|ios::out);
int Change = 0, Location;
long int ChangeCno;
cout<<"Cno-whose Email required to be modified:";
cin>>ChangeCno;
Customer CU;
while(!Change && File.read((char*)&CU,sizeof(CU)))
{
if(CU.GetCno() = ChangeCno)
{
CU.ModifyEmai1();
Location = File.tellg()==sizeof(CU);
//Statement 1:To place file pointer to the required position
______________________________;
//Statement 2:To write the object CU on to the binary file
______________________________;
Change++;
}
}
if(Change)
cout<<”Email Modified..."<<endl;
else
cout<<"Customer not found..."<<endl;
File.close();
}
Answer:
Statement 1
File.seekp(Location)
Statement 2
File.write((char*)&CU,sizeof(CU))
Question 11:
Fill in the blanks marked as Statement 1 and Statement 2 in the program segment given below with appropriate functions for the required task. Delhi 2013C
class Agent
{
long ACode; //Agent Code
char AName [20]; //Agent Name
int Commission;
public:
void Enter(); //Function to enter details of Agent
void Display(); //Function to display details of Agent
void Update(int C) //Function to modify commission
{
Commission = C;
}
int GetCommO {return Commission;}
long GetAcodeO {return ACode;}
};
void ChangeCommissiondong AC, int CM)
//AC -> Agent Code, whose commission needs to change
//CM -> New commission
{
fstream F;
F.open("AGENT.DAT",ios::binary|ios::in|ios::out);
char Changed = ' N';
Agent A;
while(Changed == 'N' && F.read((char*)&A,sizeof(A)))
{
if(A.GetAcode()==AC)
{
Changed = 'Y’;
A.Update(CM);
//Statement l:To place file pointer to the required position
___________________________________;
//Statement 2:To write the object A onto the binary file
___________________________________;
}
}
if(Changed=='N')
cout <<"Agent not registered...";
F.close ();
}
Answer:
Statement 1
F.seekp(-1*sizeof(A),ios::cur)
Statement 2
F.write((char*)&A,sizeof(A))
Question 12:
Fill in the blanks marked as Statement 1 and Statement 2 in the program segment given below with appropriate functions for the required task. Delhi 2012C
#include<fstream.h>
class Movie
{
long MNo; //Movie Hall Number
char MName[20]; //Movie Name
int Seats; //Number of Vacant Seats
public:
void MovieIn();
void MovieOut();
void Booking(int N) //Function to Book Seats
{
Seats _=N;
}
int RSeats()
{
return Seats;
}
long RMno()
{
return MNo;
}
};
void BookMySeat(long MH,int S) //MH stands for Hall Number
//S stands for Number of Tickets to purchase
{
fstream File;
File.open("M0VIE.DAT", ios::binary|ios::in|ios::out);
int Found = 0, Booked = 0, Rec;
Movie M;
whileUFound && File.read((char*)&M,sizeof(M)))
{
if(M.RMnoC)==MH)
{
Found = 1;
if((M.RSeats()-S)> 0)
{
M.Booking(S);
Booked = 1;
}
else
cout<<"House Full";
}
}
if(Found && Booked)
Rec = File.tellg()-sizeof(M);
//Statement 1 : To place file pointer to the required position
______________________;
//Statement 2 : To write the object M on to the binary file
______________________;
}
if(! Found)
cout<<"No updation done as Hall not found.";
File.close();
}
Answer:
Statement 1
File.seekpCRec,ios: :beg)
Statement 2
File.write((char*)&M,sizeof(M))
Question 13:
Observe the program segment given below carefully and answer the questions that follow: All India 2012
class Stock
{
int Ino.Qty;
char Item[20];
public:
void Enter()
{
cin>>Ino;
gets(Item):
cin>>Qty;
}
void Issue(int Q) (Qty += Q;}
void Purchase(int Q) (Qty-= Q;)
int GetlnoO {return Ino;}
};
void Purchaseltem(int Pino,int PQty)
{
fstream File;
File.open("STOCK.DAT", ios::binary|ios::in|ios::out);
Stock S;
int Success = 0;
while(Success == 0 && File.read((char*)&S,sizeof(S)))
{
if (Pino == S.Getlno())
{
S.Purchase(PQty);
____________ //Statement 1
____________//Statement 2
Success++;
}
}
if(Success == 1)
cout<<"Purchase Updated"<<endl;
else
cout<<"Wrong item No."<<endl;
File.closeO;
}
(i) Write Statement 1 to position the file pointer to the appropriate place, so that the data updation is done for the required item.
(ii) Write Statement 2 to perform the write operation, so that updation is done in the binary file.
Answer:
Statement 1
File.seekp(-1*sizeof(S),ios::cur);
Statement 2
File.write((char*)&S,sizeof(S));
Question 14:
Observe the program segment given below carefully and answer the questions that follow: Delhi 2012
class Inventory
{
int Ano, Qty; char Article[20];
public:
void Input!()
{
cin>>Ano;
gets(Article);
cin>>Qty;
}
void Issue(int Q)HQty 4= Q;)
void Procure(int Q) (Qty -= Q;)
int GetAno()(return Ano:)
};
void ProcureArticle(int TAno, int TQty)
{
fstream File;
File.open("ST0CK.DAT", ios::binary|ios::in|ios::out);
Inventory I;
int Found = 0;
while(Found == 0 && File.read((char*)&I.sizeof(I)))
{
if (TAno == I.GetAno())
{
I.Procure(TQty);
__________________ //Statement 1
__________________ //Statement 2
Found++;
}
if (Found == 1)
cout<<"Procurement Updated"<<endl;
else
cout<<"Wrong Article No"<<endl;
File.close();
}
}
(i) Write Statement 1 to position the file pointer to the appropriate place, so that the data updation is done for the required Article.
(ii) Write Statement 2 to perform the write operation, so that the updation is done in the binary file.
Answer:
Statement 1
File.seekp(-1*sizeof(I),ios::cur);
Statement 2
File.write((char*)&I,sizeof(I));
Question 15:
Observe the program segment given below carefully and fill in the blanks marked as Statement 1 and Statement 2 using seekg() or seekp() or tellg() or tellp() functions for performing the required task. Delhi 2011
#include<fstream.h>
class product
{
int pno;char pname[20];int qty;
public:
void modifyqty(); //Function is to modify quantity of product
};
void product::modifyqty()
{
fstream fil;
file.open("product.dat",ios::binary|ios::in|ios::out);
int mpno;
cout<<”product number to modify quantity
cin>>mpno;
while(fil.read((char*)this.sizeof(product)))
{
if(mpno == pno)
{
cout<<"present quantity:"<<qty<<endl;
cout<<"changed quantity:";
cin>>qty;
int position =__________; //Statement 1
___________________; //Statement 2
fil.writet(char*)this,sizeof(product));
}
}
fil.close();
}
Answer:
Statement 1
fil.tellg( )-sizeof(product)
Statement 2
fil.seekp(Position) or fil.seekp(-1*sizeof(product),ios::cur)
Question 16:
Observe the program segment given below carefully and fill in the blanks marked as
Statement 1 and Statement 2. You can use any function from seekg(), seekp(), tellp() and tellg() for performing the required task. Delhi 2011C
#include<fstream.h>
class Country
{
int Code;
char Name[20];
int Population;
public:
//Function to search and display the content from a particular record number
void SearchFor(int);
//Function to modify the content of a particular record number void Update(int);
};
void Country::SearchFor(int Record)
{
Country C;
fstream File;
File.open("COUNTRY.DAT",ios::binary|ios::in);
File.read((char*)&C,sizeof(C));
_________ //Statement1
cout<<C.Code<<"==>"<<C.Name<<"==>"<<C.Population<<endl;
File.closet();
}
void Country::Update(int Record)
{
Country C;
fstream File;
Fi le.open("COUNTRY.DAT",ios::binary |ios::in|ios::out);
cin>>C.Code;
cin.getline(C.Name,20);
cin>>C.Population;
_________ //Statement 2
File.write((char*)&C,sizeof(C));
File.closet();
}
}
Answer:
Statement 1
if(C.Code == Record);
Statement 2
File.seekp(-1*sizeof (C), ios:: cur);
Question 17:
Observe the program segment given below carefully and fill in the blanks marked as Statement 1 and Statement 2 using seekg(), seekp(), tellp() and tellg() functions for performing the required task. All India 2011
#include<fstream.h>
class ITEM
{
int Ino; char Iname[20];
float Price;
public:
void ModifyPrice(); //The function to modify price of a particular ITEM
};
void ITEM::ModifyPrice()
{
fstream File;
File. open("ITEM.DAT",ios::binary|ios::in|ios::out);
int Cino;
cout<<"Item No to modify price:";
cin>>Cino;
while(File.read((char*)this,sizeof(ITEM)))
{
if(Cino == Ino)
{
cout<<"Present Price:"<<Price<<endl;
cout<<"Changed Price:";
cin>>Price;
int FilePos =___________; //Statement 1
__________________; //Statement 2
File.write((char*)this,sizeof(ITEM)); //Re-writing the record
}
}
File.close();
}
Answer:
Statement 1
File.tellg()-sizeof (ITEM)
Statement 2
File.seekp(FilePos,ios::beg) or File.seekp(-1*sizeof(ITEM),ios::cur)
Question 18:
Observe the program given below carefully and fill in the blanks marked as Statement 1 and Statement 2 using tellg() and seekp() functions for performing the required task. All India 2010
#include<fstream.h>
class Customer
{
long Cno; char Name[20], Mobi1e[12];
public:
//function to allow user to enter the Cno, Name , Mobile
void Enter();
//function to allow user to enter (modify) Mobile number
void Modify();
//function to return value of Cno
long GetCno() {return Cno;}
};
void ChangeMobile()
{
Customer C;
fstream F;
F.open("CONTACT.DAT". ios::binary|ios::in|ios::out);
long Cnoc; //Customer number whose mobile number needs to be changed
cin>>Cnoc;
while(F.read((char*)&C,sizeof(C)))
{
if(Cnoc == C.GetCno())
C.Modify();
//Statement 1 to find the current position of file pointer int Pos = _________________
//Statement 2 to move the file pointer to write the
//modified record back onto the file for desired Cnoc
F.write((char*)&C,sizeof(C));
}
F.close();
}
Answer:
Statement 1
F.tellg():
Statement 2
F.seekp(Pos-sizeof(C),ios::beg): or F.seekp(-1*sizeof(C),ios::cur);
Question 19:
Observe the program segment given below carefully and fill in the blanks marked as Statement 1 and Statement 2 using tellg() and seekp() functions for performing the required task. Delhi 2010
#include<fstream.h>
class Client
{
long Cno;
char Name[20], Email[30];
public:
void Enter(); //Function to allow user to enter the Cno.Name,Email
void ModifyO; //Function to allow user to enter(modify) Email
long ReturnCnoO (return Cno;}
};
void ChangeEmail ()
{
Client C;
fstream F;
F.open("INF0.DAT",ios::binary|ios::in|ios::out);
long Cnoc; //Client’s number whose Email needs to be changed
cin>>Cnoc;
while(F.read((char*)&C,sizeof(C)))
{
if(Cnoc == C.ReturnCno())
{
C.Modify();
//Statement 1 to find the current position of file pointer int Pos = ______________
//Statement 2 to move the file pointer to write the
//modified record back onto the file for the desired Cnoc
______________
F.write((char*)&C,sizeof(C));
}
}
F.close();
}
Answer:
Statement 1
F.tellg():
Statement 2
F.seekp(Pos-sizeof(C),ios::beg); or F.seekp(-1*sizeof(C) ,ios: :cur);
Discover more from EduGrown School
Subscribe to get the latest posts sent to your email.