Important Questions for Class 12 Computer Science (C++) – Data File Handling
Previous Years Examination Questions
1 Marks Questions
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 "<<Fi 1 e.tellg()/sizeof(C)<<endl; File.close(); }
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(); }
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(): }
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(); }
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(); }
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(); }
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(); }
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(); }
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(); }
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(); }
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 (); }
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(); }
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.
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.
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 fi 1;
fil .open(“product.dat”,ios::binary|ios::in|ios::out); int mpno;
cout«”product number to modify quantity cin»mpno;
whi1e(fi1.read((char*)this.sizeof(product)))
(
if(mpno == pno)
1
cout«”present quantity:”«qty«endl; cout«”changed quantity:”; cin»qty;
int position = ; //Statement 1
; //Statement 2
fil .writet(char*)this,sizeof(product));
}
)
fil.close();
1
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
#i nclude<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);
1;
void Country::SearchFor(int Record)
{
Country C; fstream File;
File.open(“COUNTRY.DAT”,ios::binary|ios::in);
_ File.read((char*)&C,sizeof(C)); ■
//Statementl
■ 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);
l 1
i
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
1;
void ITEM::ModifyPrice()
{
fstream File;
Fi 1 e. open(“ITEM.DAT”,ios::binary|ios::in|ios::out); int Cino;
cout«”Item No to modify price:”; cin»Cino;
whi 1 e(Fi 1 e.read((char*)this,sizeof(ITEM)))
{
if(Cino == Ino) ‘
{
cout«”Present Price:”«Price«endl;
‘ cout<<“Changed Price:”; cin»Pri ce;
int FilePos = ; //Statement 1
; //Statement 2
File.write((char*)this,sizeof(ITEM)); //Re-writing the record
File.close!);
I
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
#i nclude<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 GetCnoO {return Cno;}
void ChangeMobileC)
{
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.GetCnoO)
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.writeC(char*)&C,sizeof(C));
1
F.close();
}
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.ModifyO;
//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));
}
1
F.closeO;
}
Question 20:
Observe the program segment given below carefully and fill in the blanks marked as Line 1 and Line 2 using fstream function for performing the required task. All India 2009
#1nclude<fstream.h> class Library { .
long Ano; char Title[20]; int Qty;
public: ‘
void Enter(int): //Function to allow user to enter the data
void Display(); //Function to display the content
void Buy(int Tqty) //Function to increment qty
{
Qty += Tqty:
}
long GetAnoO {return Ano;}
1;
void BuyBookdong BAno,int BQty) //BAno : Ano of number of book purchased
//QBty : Number of books purchased
Library L; fstream File:
File.open(“STOCK.DAT”.ios::bina ry|ios::in|ios::out); int Position = -1:
whileCPosition «** – 1 && File.read((char*)&L,sizeof(L)))
{
if(L.GetAno( ) = = BAno)
{
L.Buy(BQty): //To update the number of books
Position = File.tellgO-sizeof(L):
//Line 1 : to place the file pointer to the required position //Line 2 : to write the object L on to the binary file
1
i f (P o s i t i o n == -1)
cout«”No updation done as required Ano not found:”; File.close();
Question 21:
Observe the program segment given below carefully and fill in the blank marked as Statement 1 using seekg() or seekp() functions for performing the required task. Delhi 2009C
#i nclude<fstream.h> class File_Object {
int No;
char Name[20]; public:
//Function to read Nth record from the file void Goto_Record(int N);
1;
void File_Object::Goto_Record(int N)
I .
fstream File;
File_Object Record;
. Fi1e.open(“STOCK.DAT”.ios::bina ry|ios::in);
//Statement 1
File.read((char*)&Record,sizeof(Record)); cout<<Record. No<<Record. Name«endl;
}
Question 22:
Observe the program segment given below carefully and fill in the blanks marked as Line 1 and Line 2 using fstream function for performing the required task. Delhi 2009
#i nclude<fstream.h> class Stock
long Ino; char ITem[20]; int Qty; public:
void Get(int); //Function to allow user to enter the Ino, Item, Qty
voidshowO; //Function to display the content ‘
void PurchaseCint Tqty) //Function to increment qty {
Qty += Tqty;
1
long KnowInoO (return Ino;}
void Purchaseitem(long PIno.int Pqty) //Pino : Ino of item purchased
//Pqty : Number of item purchased
{
Stock S; fstream File;
Fi 1 e.open(“ITEMS.DAT”,ios:;binary|ios::in|ios:;out); int Pos = -1;
whileCPos 1 && File.read((char*)&S,sizeof(S)))
if(S.KnowIno() == Pino)
{
S.Purchase(Pqty); //to update the number of items
Pos = File.tel 1 g()-s i zeofC S);
/*Line 1: to place the file pointer to
the required position*/
/*Line 2; to write the object S on to
the binary file*/
1
i f (P o s == -1)
cout«”No updation done as required Ino Not found:”;
File.close!);
Computer ScienceImportant Questions for Computer ScienceNCERT Solutions
The post Important Questions for Class 12 Computer Science (C++) – Data File Handling appeared first on Learn CBSE.