Monday

Important Questions for Class 12 Computer Science (C++) – Constructor and Destructor (Part 2)

 Question 11:

Observe the following C++ code and answer the questions (i) and (ii) : All India 2015

class Passenger
{
long PNR; 
char Name[20]; 
public:
Passenger()    //Function 1
{
cout<<"Ready"<<endl;
}
void Book(long P,char N[]) //Function 2 I
PNR = P; strcpy(Name, N);
}
void Print() //Function 3 
{
cout<<PNR<<Name<<endl;
}
∼Passenger //Function 4
{
cout<<"Booking cancelled!"<<endl;
}
};
  1. Fill in the blank statements in Line 1 and Line 2 to execute Function 2 and Function 3 respectively in the following code :
    void main( )
    {
    Passenger P;
    __________ //Line 1
    __________ //Line 2
    }//Ends here
  2. Which function will be executed at } //Ends here ? What is this function referred as?

Аnswer:

  1. Line 1 → P.Book(10, “ABC” ) :
    Line 2 →P.Print( ) :
  2. ~Passenger( ) function will be executed at } //Ends here. This function is referred as destructor.

Question 12:
Write the output of the following C++ program code: All India 2015c
NOTE: Assume all required header files are already being included in the program.

class Item 
{
int I no;  
char IName[20]; 
float Price;
public: 
Item()
{
Ino=l00;
strcpy(Iname,"None");
Price=100;
{ 
void Assign(int I, char Named, float P)
{
Ino+=I;
strcpyCIName, Name);
Price=P;
}
void RaiseP(float P)
{
Price+=P;
}
void ReduceP(float P) 
{ 
Price-=P;
}
void Disp()
{
cout<<Ino<<":"<<IName<<":"<<Price<<endl;
}
};
void main()
{ 
Item One, Two;
One.Disp();
One.Assignd(1, "Pen", 95); 
Two.Assign(2, "Penci1",55); 
One.RaiseP(lO);
Two.ReduceP(5);
Two.Disp();
One.Disp();
}

Аnswer:
Output of the given program would be:
100 : None : 100
102 : Penci1 : 50
101 : Pen : 105

Question 13:
Observe the following C++ code and answer the questions (i) and (ii): All India 201SC

class EandI
{
int Temperature, Humidity;
char City[30];
public:
EandI()    //Function 1
{
Temperature=0;Humidity=0; 
cout<<"Set to Zero"<<endl;
}
EandI(int T, int H, char C[])    //Function 2
{
Temperature=T;
Humidity=H; 
strcpy(City, C);
}
voidShow()    //Function 3
{
cout<<Temperature<<":"<<Humidity<<endl; 
cout<<City<<endl;
}
∼EandI()    //Function 4
{
cout<<"Data Removed!"<<endl; 
}
};
  1. Fill in the blank lines as Statement 1 and Satement 2 to execute Functions 2 and 3 respectively in the following code:
    void main( )
    {
    Eandl E;
    ___________ //Statement 1
    ___________ //Statement 2
    }//The end of main( ) function here
  2. Which function will be executed at the point where “//The end of main function here” is written in the above code? What is this function called and executed here is known as?

Аnswer:

  1. Statement 1 →
    E. EandI(25,39,”Delhi”);
    Statement 2 → E.Show( );
  2. Function 4 will be executed. It is known as Destructor and will be executed when object of class EandI goes out of scope.

Question 14:
Answer the question (i) and (ii) after going through the following class: All India 2014C

class schoolbag 
{
int pockets; 
public:
schoolbag()    //Function 1
{ 
pockets=30;  
cout<<"The bag has pockets”<<end1;
}
void company()    //Function 2
{
cout<<"The company of the Bag is ABC"<<endl;
}
school bag(int D)    //Function 3
{
pockets=D;
cout<<"Now the Bag has pockets"<<pockets<<endl;
}
∼schoolbag()    //Function 4
{
cout<<”Thanks"<<endl;
}
};
  1. In object oriented programming, what is Function 4 referred as and when does it get invoked/called?
  2. In object oriented programming, which concept is illustrated by Function 1 and Function 3 together?

Аnswer:

  1. Function 4 is referred as destructor and it is get invoked/called whenever an object goes out of scope.
  2. Function 1 and Function 3 combined together referred as constructor overloading, i.e. Polymorphism.

Question 15:
Write four characteristics of a constructor function used in a class. Delhi 2014
Аnswer:

Characteristics of the constructor function used in a class are as follows:

  1. Constructors are special member functions having same name as that of class name.
  2. Constructors do not need to be called explicitly. They are invoked automatically whenever an object of that class is created.
  3. They are used for initialisation of data members of a class.
  4. Constructors do not have a return type and that is why cannot return any value.

Question 16:
Answer the questions (i) and (ii) after going through the following class: Delhi 2014

class Health 
{
int PId,DId; 
public:
Health(int PPId);   //Function 1 
Health();           //Function 2 
Health(Health &H);  //Function 3 
void Entry();       //Function 4 
void Display();     //Function 5
}; 
void main()
{
Health H(20);    //Statement 1
}
  1. Which of the function out of Function 1, 2, 3, 4 or 5 will get executed, when the Statement 1 is executed in the above code?
  2. Write a statement to declare a new object G with reference to already existing object H using Function 3.

Аnswer:

  1. Function 1 will execute, when Statement 1 will be executed in the given code.
  2. Health G(H) ;

Question 17:
Obtain the output of the following C++ program, which will appear on the screen after its execution.
Delhi 2014
Important Note All the desired header files are already
included in the code, which are required to run the code.

class Player 
{
int Score, Level; 
char Game; 
public:
Player(char GGame='A')
{
Score=0;
Level=1;
Game=GGame; 
}
void Start(int SC); 
void Next(); 
void Disp()
{
cout<<Game<<"@”<<Level<<endl; 
cout<<Score<<endl;
}
};
void main()
{
Player P,Q('B');
P.Disp();
Q.Start(75);
Q.Next();
P.Start(120);
Q.Disp();
P.Disp();
}
void Player::Next()
{
Game=(Game=='A')?'B':'A';
}
void Player::Start(int SC)
Score+=SC;
if(Score>=100)
Level=3;
else if(Score>=50)
Level=2;
else
Level=1;
}

Аnswer:
Output of the given program would be:
A@1
0
A@2
75
A@3
120

Question 18:
Obtain the output of the following C++ after its execution. All India 2014
Important Note All the desired header files are already included in the code, which are required to run the code.

class Game 
{
int Level, Score; 
char Type; 
public:
Game(char GType='P')
{
Level=1;Score=0;
Type=GType;
}
void Play(int GS); 
void Changer(); 
void Show()
{
cout<<Type«"@"<<Level<<endl;
cout<<Score<<endl;
}
};
void main()
{
Game A('G'),B; 
B.Show(); 
A.Play(11);
A.Changer);
B.Play(25);
A.Show();
B.Show(); 
}
void Game::Change()
{
Type=(Type=='P')? 'G’:'P’;
}
void Game::Play(int GS)
{
Score+=GS; 
if(Score>=30)
Level=3;
else if(Score>=20) 
Level=2; 
else 
Level=1;
}

Аnswer:
Output of the given program would be:
P@1
0
P@1
11
P@2
25

Question 19:
Answer the questions (i) and (ii) after going through the following class:

class Hospital
{
int Pno.Dno; 
public:
Hospital(int PN):    //Function 1
Hospital();    //Function 2
Hospital(Hospital &H); //Function 3
void In();   //Function 4
void Disp();    //Function 5
};
void main()
{
Hospital H(20); //Statement 1
}
  1. Which of the function out of Functions 1,2,3, 4 or 5 will get executed, when the Statement 1 is executed in the above code?
  2. Write a statement to declare a new object G with reference to already existing object H using Function 3.

Аnswer:

  1. Function 1 will be called, when Statement 1 will be executed.
  2. Hospital G(H);

Question 20:
Write any two similarities between constructors and destructors. Write the function headers for constructor and destructor of a class Flight. All India 2013
Аnswer:

Similarities between constructor and destructor

  1. Both have same name as the class in which they are declared.
  2. If not declared by user both are available in a class by default but now they can only allocate and deallocate memory from the objects of a class, when an object is declared or deleted.

e.g.

class Flight 
{
public:
Flight()    //Constructor
{
cout<<"Constructor for class Flight";
}
∼Flight()    //Destructor
{
cout<<"Destructor for class FIight";
}
};


Wednesday

Important Questions for Class 12 Computer Science (C++) – Constructor and Destructor

 

Previous years Examination Questions
2 and 3 Marks Questions

Question 1:
Observe the following C++ code and answer the questions (i) and (ii).
NOTE Assume all necessary files are included.

class TEST 
{
long TCode; 
char TTitle[20]; 
float Score; 
public:
TESTO //Member Function 1 
{
TCode = 100;
strcpy(TTitle,"FIRST Test"); 
Score=0;
}
TEST(TEST &T) //Member Function 2
{
TCode=E.TCode+l; 
strcpy(TTitle,T.TTitle); 
Score=T.Score;
}
};
void main()
{
_____________     //Statement 1
_____________     //Statement 2
}
  1. Which Object Oriented Programming feature is illustrated by the Member Function 1 and the Member Function 2 together in the class TEST?
  2. Write Statement 1 and Statement 2 to execute Member Function 1 and Member Function 2 respectively.
    All Indio 2017

Аnswer:

  1. Constructor overloading feature is illustrated by the Member Function 1 and the Member Function 2 together in the class TEST.
  2. Statement 1
    TEST T1; //To execute Member Function 1
    Statement 2
    TEST T2 = T1; //To execute Member Function 2

Question 2:
Find and write the output of the following C++ program code: Delhi 2016
NOTE Assume all required header files are already being included in the program.

class Stock 
{
long int ID; 
float Rate; 
int Date; 
public:
Stock(){ID=1001 ; Rate=200; Date=l;}
void RegCode(long int I, float R)
{
ID = 1;
Rate=R;
}
void Change(int New,int DT)
{
Rate+=New;
Date=DT;
}
void Show()
{
cout<<"Date:”<<Date<<endl;
cout<<ID<<"#"<<Rate<<endl;
}
};
void main()
{
Stock A,B,C;
A.RegCode(1024,150);
B.RegCode(2015,300); 
B.Change(100,29);
C.Change(-20,20);
A.Show();
B.Show();
C.Show();
}

Аnswer:
Output of the given program would be:
Date : 1
1024#150
Date : 29
2015#400
Date : 20
1001#180

Question 3:
Observe the following C++ code and answer the questions (i) and (ii). Assume all necessary files are included:

class FICTION Delhi 2016 
{
long FCode; 
char FTitle[20]; 
float FPrice; 
public:
FICTION() //Member Function 1 
{
cout<<"Bought"<<endl;
FCode = 100; 
strcpy(FTitle,"Noname");
FPrice=50;
}
FICTION(int C,char T[],float P) //Member Function 2
{
FCode = C;
strcpy(FTitle, T);
FPrice=P;
}
void Increase(float P) //Member Function 3
{
FPrice+=P;
}
void Show() //Member Function 4 
{
cout<<FCode<<":"<<FTitle<<":"<<FPrice<<endl;
}
∼FICTION() //Member Function 5
{
cout<<"Fiction removed!"<<endl;
}
};
void main() //Line 1
{           //Line 2
FICTION F1, F2(101, "Dare”,75); //Line 3
for(int 1=0;I<4;I++)    //Line 4
{    //Line 5
F1. Increase(20);F2.Increase(15); //Line 6
F1. Show();F2.Show(); //Line 7
} //Line 8
} //Line 9
  1. Which specific concept of object oriented programming out of the following is illustrated by Member Functionl and Member Function 2 combined together ?
    • Data Encapsulation
    • Data Hiding
    • Polymorphism
    • Inheritance
  2. How many times the message “Fiction removed!” will be displayed after executing the above C++ code ? Out of Line 1 to Line 9, which line is responsible to display the message “Fiction removed!” ?

Аnswer:

  1. Polymorphism or constructor overloading
  2. 2 times the message “Fiction removed!” will be displayed. Line 9 is responsible to display the message “Fiction removed!”.

Question 4:
Find and write the output of the following C++ program code: All India 2016 NOTE Assume all required header files are already being included in the program.

class Share 
{
long int Code; 
float Rate; 
int DD; 
public:
Share(){Code=1000;Rate=100;DD=1;} 
void GetCode(long int C, float R)
{
Code=C;
Rate=R;
}
void Update(int Change, int D)
{
Rate+=Change;
DD=D;
}
void Status()
{
cout<<"Date: "<<DD<<endl; 
cout<<Code<<"#”<<Rate<<endl;
};
void main()
{
Share S,T,U;
S.GetCode(1324,350);
T.GetCode(1435,250);
S.Update(50,28);
U.Update(-25,26);
S.Status();
T.Status();
U.Status();
}

Аnswer:
Output of the given program would be:
Date: 28
1324#400
Date: 1
1435#250
Date: 26
1000#75

Question 5:
Observe the following C++ code and answer the questions (i) and (ii). All India 2016
NOTE Assume all necessary files are included :

class BOOK 
{
long Code;
char Title[20];
float Price; 
public:
BOOK()//Member Function 1
{
cout<<"Bought"<<endl; 
code=10;
strcpy(Title,"NoTitle"); 
Price=100;
}
BOOK(int C.char T[],float P) //Member Function 2
{
Code=C;
strcpy(Title,T);
Price=P;
}
void Update(float P) //Member Function 3
{
 Price+=P;
}
void Display() //Member Function 4
{
cout<<Code<<":"<<Title<<”:”<<Price<<endl;
}
∼BOOK() //Member Function 5 
{
cout<<"Book Discarded!"<<endl;
}
};
void main() //Line 1
{     //Line 2
BOOK B.C(101,"Truth",350); //Line 3
for(int I=0;I<4;I++) //Line 4
{  //Line 5
B.Update(50);C.Update(20); //Line 6
B.Display();C.Display(); //Line 7
} //Line 8
} //Line 9
  1. Which specific concept of object oriented programming out of the following is illustrated by Member Function 1 and Member Function 2 combined together?
    • Data Encapsulation
    • Polymorphism
    • Inheritance
    • Data Hiding
  2. How many times the message “Book Discarded!” will be displayed after executing the above C++ code? Out of Line 1 to Line 9, which line is responsible to display the message “Book Discarded!”

Аnswer:

  1. Polymorphism or constructor overloading.
  2. 2 times the message “Book Discarded!” will be displayed. Line 9 is responsible to display the message “Book Discarded!”.

Question 6:
Differentiate between Constructor and Destructor functions giving suitable example using a class in C++. When does each of them execute? Delhi 2016
or
Write any two differences between constructor and destructor. Write the function header for constructor and destructor of a class Member. Delhi 2013
or
Differentiate between constructor and destructor functions in a class. Give a suitable example in C+ + to illustrate the difference. Delhi 2012c
or

Differentiate between constructor and destructor function with respect to object oriented programming. All India 2011
Аnswer:

Differences between constructor and destructor :

ConstructorDestructor
Its name is same as the class name.Its name is same as the class name preceded by the tilde (-) sign.
It is automatically called whenever an object is created.It is automatically called whenever an object goes out of the scope.
e.g.
class Member 
{
public:
Member() //Constructor
{
cout<<"I am a constructor of Member class";
}
~Member()   //Destructor 
{
cout<<"I am destructor of Member class";
}
};

Question 7:
What is copy constructor? Give an example in C++ to illustrate copy constructor. All Indio 2016C, Delhi 2009
or
What is a copy constructor? Give a suitable example in C++ to illustrate with its definition within a class and a declaration of an object with the help of it. Delhi 2015, All Indio 2015
Аnswer:
Copy Constructor A copy constructor is that constructor which is used to initialise one object with the values from another object of same class during declaration.
e.g.

class Student 
{
int s; 
public:
Student() //Default constructor 
{
s = 10;
}
Student(Student &i)  //Copy constructor
{
s = i.s;
}
};
void main()
{
Student s1;      //Default constructor called 
Student s2(s1);  //Copy constructor called
}

Question 8:
Write the output of the following C++ program code: Delhi 2015
NOTE Assume all required header files are already being included in the program.

class Calc 
{
char Grade: 
int Bonus:
public:
Calc()
{ 
Grade='E';
Bonus=0;
}
void Down(int G) 
{
Grade-=G;
}
void Up(int G)
{
Grade+=G;
Bonus++;
}
void Show()
{
cout<<Grade<<"#"<<Bonus<<endl;
}
};
void main()
{
Calc C;
C.Down(2);
C.Show();
C.Up(7);
C.Show();
C.Down(2);
C.Show();
}

Аnswer:
Output of the given program would be:
C#0
J#1
H#1

Question 9:
Observe the following C++ code and answer the questions (i) and (ii):

class Traveller 
{
long PNR;
char TName[20];
public:
Traveller()  //Function 1
{
cout<<"Ready"<<endl;
}
void Book(long P, char N[]) //Function 2
{
PNR = P;
strcpy(TName,N);
}
void Print() //Function 3
{
cout<<PNR<<TName«endl;
}
∼Traveller() //Function 4
{
cout<<"Booking cancelled!"<<endl; 
}
};
  1. Fill in the blank statements in Line 1 and Line 2 to execute Function 2 and Function 3 respectively in the following code :
    void main()
    {
    Traveller T;
    _______   //Line 1
    _______   //Line 2
    } //Stops here
  2. Which function will be executed at }//Stops here? What is this function referred as? Delhi 2015

Аnswer:

  1. Line 1 → T.Book(20,”XYZ”);
    Line 2 → T.Print( );
  2. ∼Traveller( ) Function will be executed at }// Stops here. This function is referred as destructor.

Question 10:
Write the output of the following C++ program code: All India 2015
NOTE Assume all the required header files are already being included in the program.

class Eval 
{
char Level; 
int Point; 
public:
Eval() {Level= ’E' ; Point=0;} 
void Sink(int L)
{
Level -= L;
}
void Float(int L)
{
Level += L;
Point++;
}
void Show()
{
cout<<Level<<"#"<<Point<<endl;
}
};
void maint()
{
Eval E;
E.Sink(3);
E.Show();
E.Float(7);
E.Show();
E.Sink(2);
E.Show();
}

Аnswer:
Output of the given program would be:
B#0
I#1
G#1

Thursday

Important Questions and Answers of Data File Handling of Class 12 - CBSE

 Question 26:

Write a definition for function BUMPER() in C++ to read each object of a binary file GIFTS.DAT, find and display details of those gifts, which has remarks as “ON,DISCOUNT”. Assume that the file GIFTS.DAT is created with the help of objects of class GIFTS, which is defined below: Delhi 2016

class GIFTS 
{
int ID;char Gift[20]. Remarks[20]; float Price: 
public:
void Takeonstock()
{
cin>>ID;gets(Gift) ;gets (Remarks) ;cin>>Price;
}
void See()
{
cout<<ID<<":"«Gift<<":" <<Price<< ": "<<Remarks<<endl;
}
char *GetRemarks() {return Remarks;
};

Answer:

void BUMPER!)
{
GIFTS obj;
ifstream infile!"GIFTS . DAT"); 
while(infile.read!(char*)&obj, 
sizeof(obj)))
{
if(strcmp (obj.GetRemarks(),
"ON DISCOUNT") == 0) 
obj See();
}
infile.close!);
}

Question 27:
Write function definition of DISP3CHAR() in C++ to read the content of a text file KIDINME.TXT and display all those words, which has three characters in it. All India 2016
e.g. If the content of the file KIDINME.TXT is as follows:

” When I was a small child, I used to play in the garden with my grand mom.
Those days were amazingly funful and I remember all the moments of that time.”

The function DISP3CHAR() should display the following:

“was the mom and all the”

Answer:

void DISP3CHAR()
{
ifstream fin("KIDINME.TXT"); 
char word[80];
while(!fin.eof())
{
fin>>word;
if(strlen(word)==3&&(! 
(word[2]<65| |(word[2]>90&&word 
[2]<97)| | word[2]<122))) 
cout<<word<<"“; 
else if(strlen(word)==4&&
((word[3]<65| |word[3]>90)&& 
(word[3]<97| |word[3]>122)))
{
for(int i=0;i<3;i++) 
cout<<word[i];
cout<<" ”;
}
}
fin.close();
}

Question 28:
Write a definition for function ONOFFER() in C++ to read each object of a binary file TOYS.DAT, find and display details of those toys, which has status as “ON OFFER”. Assume that the file TOYS.DAT is created with the help of objects of class TOYS, which is defined below: All India 2016

class TOYS
{
int TID;char Toy[20],Status[20];float MRP;
public:
void Getinstock()
{
cin>>TID;gets(Toy);gets(Status);cin>>MRP;
}
voidView()
{
cout<<TID<<": ”<<Toy<<": "<<MRP<<n": "<<Status<<endl;
}
char*SeeOffer()(return Status;}
};

Answer:

void ONOFFER()
{
TOYS obj;
ifstream i nfileCTOYS.DAT"); 
while( infile.read((char*)&obj, 
sizeof(obj)))
{
if(strcmp(obj.SeeOffer(),
"ON OFFER")==0) 
obj.View();
}
infile.closer()
}

Question 29:
Write function definition for WORDABSTART ()in C++ to read the content of a text file JOY.TXT, and display all those words, which are starting with either ‘A’, ‘a’ or ‘B’, ‘b’. All India (C) 2016
e.g. If the content of the file JOY.TXT is as follows:

“I love to eat apples and bananas. I was travelling to Ahmedabad to buy some clothes.”

The function WORDABSTART() should display the following:

“apples and bananas Ahmedabad buy”

Answer:

void WORDABSTART()
{
ifstream fin("JOY.TXT"); 
char ch[80]; 
while (!fin.eof())
{
fin>>ch;
if(ch[0]—'A' | |ch[0]='a'| |ch[0] 
=='b' 11 chCO]—'B’) 
cout<<ch<<'"';
}
fin.close();
}

Question 30:
Write function definition for SUCCESS() in C++ to read the content of a text file STORY.TXT, count the presence of word STORY and display the number of occurrence of this word. Delhi 2015

NOTE -The word STORY should be an independent word
-Ignore type cases (i.e. lower/upper case)

e.g. if the content of the file STORY.TXT is as follows:

” Success shows others that we can do it. It is possible to achieve success with hard work. Lot of money does not mean SUCCESS.”

The function SUCCESS() should display the following:

“3”

Answer:
There is error in this question. STORY should be replaced by SUCCESS word, i.e. we have to count SUCCESS word according to output.

void SUCCESS()
{
ifstream in("STORY.TXT"); 
char ch[200];
 int count = 0; 
while(!in.eof())
{
in>>ch;
if(strcmpi(ch,"success")==0) 
count++;
}
cout<<count; 
in.close();
}

Question 31:
Write a definition for function Economic() in C++ to read each record of a binary file ITEMS.DAT, find and display those items, which cost less than 2500. Assume that the file ITEMS.DAT is created with the help of objects of class ITEMS, which is defined below: Delhi 2015

class ITEMS 
{
int ID; char GIFTC20]; float Cost:
public:
void Get()
{
cin>>ID; gets (GIFT) ;cin>>Cost;
}
void See()
{
cout<<ID<<": "<<GIFT<<": "<<Cost<<endl;
}
float GetCost(){return Cost;}
}:

Answer:

void Economic() 
{
ifstream fcinCITEMS.DAT",ios:: in|ios::binary):
ITEMS i; 
float co;
while(fcin.read((char*)&i,
 sizeof(i)))
{
co=i.GetCost(); 
if(co<2500) 
i.See();
}
fcin.close();
}

Question 32:
Write function definition for TOWER() in C++ to read the content of a text file WRITEUP.TXT, count the presence of word TOWER and display the number of occurrences of this word. All India 2015

NOTE – The word TOWER should be an independent word.
– Ignore type cases (i.e. lower/upper case).
e.g. If the content of the file WRITEUP.TXT is as follows:

“Tower, of hanoi is an interesting problem. Mobile phone tower is away from here. Views from EIFFEL TOWER are amazing.”

The function TOWER() should display the following :

“3 ”

Answer:

void TOWER()
{
ifstream in("WRITEUP.TXT"); 
char ch[200]; 
int count = 0; 
while(!in.eof())
{
i n >> c h;
if(strcmpi (ch,"Tower") == 0) 
count++;
}
cout<<count; 
in.close ();
}

Question 33:
Write a definition for function COSTLY() in C++ to read each record of a binary file GIFTS.DAT, find and display those items, which are priced more than 2000. Assume that the file GIFTS.DAT is created with the help of objects of class GIFTS, which is defined below: All India 2015

class GIFTS 
{
int CODE: char ITEMC20]: float PRICE; 
public:
void Procure()
{
cin>>CODE; gets(ITEM): cin>>PRICE; 
}
void View()
{
cout<<C0DE<<": "<<ITEM<<": "<<PRICE<<endl;
}
float GetPrice() {return PRICE:)

Answer:

void COSTLY()
{
ifstream fcinCGIFTS.DAT", 
ios :: in|ios :: binary); 
GIFTS g; 
float pr;
while(fcin.read((char*)&g, 
sizeof(g)))
{
pr=g.GetPrice(); 
if (pr>2000)
g.View();-
}
fcin.close();
}

Question 34:
Write function definition for COUNTNU( )in C++ to read the content of a text file
CONTENT.TXT, count the characters N and U (in lower case as well as upper case) present in the file. All India 2015 C
e.g. Assume the content of the file CONTENT. TXT is as follows:

“New research shows that our closest evolutionary relatives have all of the cognitive, capacities required for cooking except an understanding of how to control fire.”

The function COUNTNU( )should display the following:
“13”

Answer:

void COUNTNU()
{
ifstream fin("C0NTENT.TXT"); 
char C; 
int count=0; 
while(!fin.eof())
{
C=fin.get();
if(fin.eofO)
break;
if(C=='N'||C=='n'||C= ='U'||C= ='u'|
count++;
}
fin.close(); 
cout<<count;
}

Question 35:
Write a function EUCount() in C++, which should reads each character of a text file. “IMP.TXT”, should count and display the occurrence of alphabets E and U (including small cases e and u too). Delhi 2014
e.g. If the file contains is as follows:

Update information is simplified by official websites.

The EUCount() function should display the output as:

E : 4 
U : 1

Answer:

void EUCount() 
{
ifstream infiie("IMP.TXTn); 
char ch;
int countE=0,countU=0; 
while(infile)
{
infile.get(ch);
 if(ch=='E' | | ch=='e')
countE++;
else if(ch=='U' || ch=='u') 
countU++; '
}
cout<<"E ; "<<countE<<endl; 
cout<<"U : "<<countU;
}

Question 36:
Assuming the class GAMES as declared below, write a function in C++ to read the objects of GAMES from binary file “GAMES.DAT” and display the details of those GAMES, which are meant for children of AgeRange “8 to 13”. Delhi 2014

class GAMES
{
int GameCode; 
char GameName[10]; 
char *AgeRange; 
public:
void Enter()
{
cin>>GameCode;
gets(GameName);
gets(AgeRange);
}
void Display()
{
cout<<GameCode<<": "<<GameName<<endl; 
cout<<AgeRange<<endl;
}
char*AgeR() {return AgeRange;}
};

Answer:

void READGAMES() 
{
GAMES obj;
ifstream infileCGAMES.DAT"); 
whi1e(infile.read((char*)&obj, 
sizeof(obj)))
{
if(strcmp(obj.AgeR(),"8 to 13") == 0)
obj.Display();
}
infile.close();
}

Question 37:
Write a function AECount() in C++, which should read each character of a text file “NOTES.TXT”, should count and display the occurrence of alphabets A and E (including small cases a and e too). All India 2014
e.g. If the file content is as follows: .

CBSE enhanced its
CCE guidelines further.

The AECount() function should display the output as

A: 1 
E: 7

Answer:

void AECount()
{
ifstream infile(”NOTES.TXT"); 
char ch;
int countA=0.countE=0; 
while(infile)
{
infile.get(ch);
if(ch==' A ’ | | ch=='a') 
countA++;
else if(ch= = 'E' || ch= = 'e') 
countE++;
}
cout<<"A : "<<countA<<endl; 
cout<<"E : "<<countE;
}

Question 38:
Assuming the class TOYS as declared below, write a function in C++ to read the objects of TOYS from binary file “TOYS.DAT” and display the details of those TOYS, which are meant for children of Age Range “5 to 8”. All India 2014

class TOYS 
{
int ToyCode;
char ToyName[10]; 
char *AgeRange; 
public:
void Enter()
{
cin>>ToyCode; 
gets(ToyName); 
gets(AgeRange);
}
void Display() 
{
cout<<ToyCode<<": "<<ToyName<<endl; 
cout<<AgeRange<<endl;
}
char* WhatAge()I return AgeRange;}
};

Answer:

void READTOYS()
{
TOYS obj;
ifstream infile!"TOYS.DAT"); 
while(infile.read((char*)&obj, sizeof(obj)))
{
if(strcmp(obj.WhatAge(),
"5 to 8") == 0) 
obj .Display!);
}
infile.close();
}

Question 39:
Given a binary file “SPORTS.DAT” containing records programme following class: All India (C) 2014

class Player 
{
char PNO[10]; //Player number
char Name [20]; //Name of player
int rank; //Rank of the player
public:
void EnterData()
{
gets (PNO); gets (Name) ;cin>>rank;
}
void DisplayData ()
{
cout<<setw(12)<<PN0; 
cout<<setw(32)<<Name; 
cout<<setw(3)<rank<<endl;
}
int Ret_rank(){return rank;}
};

Write a function in C++ that would read contents of the file “SPORTS.DAT” and display the details of those players whose rank is above 500.

Answer:

void READPLAYER()
 {
Player obj;
ifstream infileCSPORTS.DAT"); 
while(infile.read(char*)&obj, 
sizeof(obj)))
{
if(obj.Ret_rank()>500)
obj.DisplayDataO;
}
infile.close();
}

Question 40:
Write a function Countaroma() to count and display the number of times “Aroma” occurs in a test file “Cook.txt”.
All India (C) 2014
NOTE  Only complete word “Aroma“should be counted. Words like “Aromatic“should not be counted.

Answer:

void Countaroma()
{
ifstream inC'Cook.TXT"); 
char ch[200]; 
int count=0; 
while(!in.eof())
{
in>>ch;
if(strcmpi (ch, "aroma")=0) 
count++;
}
cout<<count; 
in.close();
}

Question 41:
Write a function CountYouMef) in C++, which reads the content of a text file “story.txt” and counts the words You and Me (not case sensitive). All India 2013
e.g. if the file contains:

You are my best friend.
You and me make a good team.

The function should display the output as

Count for You: 2 
Count for Me: 1

Answer:

void CountYouMe()
{
ifstream Filein; 
char arr[10]; clrscr();
Filein.open("story.txt", ios::in);
i f (! Filein)
{
cout<<"File does not exist!"; 
exit(0);
}
int i=0,j=0; 
while(!Filein.eof ())
{
Filein>>arr;
if(strcmpi(arr,"You")==0) 
i++;
else if(strcmpi(arr,"Me")==0) 
J++:
}
Filein.close();
cout<<"Count for Yog:"<<i<<endl; 
cout<<"Count for Me: "<<j<<endl;
}

Question 42:
Write a function CountDig() in C++, which reads the content of text file “story.txt” and displays the number of digits in it.
Delhi (C) 2013
e.g. if the file contains:

Amrapali was a queen of Gareware kingdom in 
the year 1911. She had 2 daughters.
Her palace had 200 rooms.

Then the output on the screen should be

Number of digits in story:8

Answer:

void CountDig()
{
int count=0; 
char Ch;
ifstream fcin("story.txt"); while(fcin)
{
fcin.get(Ch); 
if(Ch>=48&&Ch<=57)
{
count++;
}
}
cout<<"Number of digits in story; "<<count;
fcin.close();
}

Question 43:
Assuming the class WORKER as declared below, write a function in C++ to read the objects of WORKER from binary file named “WORKER.DAT” and display those records of workers, whose wage is less than 300. Delhi 2013C

class WORKER
{
int WN0;
char WName[30]; float Wage; 
public:
void EnterO (cin>>WN0;gets(WName); cin>>Wage;}
void DISP() {cout<<WNO<<"*"<<WName<<"*"<<Wage<<endl;}
float GetWageC) (return Wage;}
};

Answer:

void SHOW()
{
ifstream fcin!"WORKER.DAT", ios;;in|ios::binary);
WORKER W;
whi le(fcin.read((char*)&W, 
sizeof(W)))
{
if(W.GetWage()<300)
{
W.DISP();
}
}
fcin.close();
}

Question 44:
Write a function CountHisHer() in C++, which reads the contents of a text file “diary.txt” and counts the words His and Her (not case sensitive). Delhi 2013
e.g. if the file contains:

Pinaky has gone to his friend's house. His friend's name is Ravya. Her house is 12 KM from here.

The function should display the output as

Count for His : 2 
Count for Her : 1

Answer:

void CountHisHer()
{
ifstream AL;
AL.open("diary.txt",ios;:in); 
char Word[80]; 
int C1=0, C2=0;
while(!AL.eof())
{
AL>>Word;
if(strcmpi(Word, "His")==0)
 C1++;
else if(strcmpi(Word, "Her")==0)
C2++;
}
cout<<"Count for His :"<<C1 <<endl;
cout<<"Count for Her : "<<C2<<endl;
AL.close();
}

Question 45:
Assuming the class VINTAGE as declared below, write a function in C++ to read the objects of VINTAGE from binary file “VINTAGE.DAT” and display those vintage vehicles, which are priced between 200000 and 250000. Delhi 2013

class VINTAGE 
{
int VNO; //Vehicle Number
char VDesc[10]: //Vehicle Description
float Price; 
public:
void GET() {cin>>VN0;gets! VDesc) ;cin>>Price;}
void VIEW()
{
cout<<VNO<<endl ; 
cout<<VDesc<<endl; 
cout<<Price<<endl;
}
float ReturnPrice()
{
return Price;
}
};

Answer:

void show()
{
ifstream fcin("VINTAGE.DAT", 
ios::in|ios::binary);
VINTAGE V; 
float pr;
while(fcin.read((char*)&V, sizeof(V))) 
{
pr = V.ReturnPrice();
if(pr>=200000 && pr<=250000)
{
V.VIEW();
}
}
fcin.close();
}

Question 46:
Assuming the class ANTIQUE as declared below, write a function in C++ to read the objects of ANTIQUE from binary file “ANTIQUE.DAT” and display those antique items, which are priced between 10000 and 15000. All India 2013

class ANTIQUE 
{
int ANO; 
char Aname[10]; 
float Price; 
public:
void BUY()
{
cin>>ANO.;gets(Aname); 
cin>>Price;
}
void SHOW() 
{
cout<<AN0<<endl; 
cout<<Aname<<endl;
cout<<Price<<endl ;
}
float GetPricel){return Price;}
};

Answer:

void search()
{
ifstream ifile("ANTIQUE.DAT”, 
ios::in|ios::binary); 
if (! ifile)
{
cout<<"could not open ANTIQUE.
DAT; exit(-1);
}
else
{
ANTIQUE a;
whileCifile.read((char*)&a, sizeof(a)))
{
if(a.GetPrice()>=10000&& 
a.GetPrice()<=15000) 
a.SHOW();
}
}
}

Question 47:
Write a function in C++ to read the content of a text file “PLACES.TXT” and display all those lines on screen, which are either starting with ‘P’ or starting with ‘S’. Delhi 2012
Answer:

void Display()
{
ifstream fcin("PLACES.TXT"); 
char Ch[100]; .
fcin.getline(Ch.100); 
while(!fcin.eof())
{
if(Ch[0]=='P'|| ChCO]=='S')
{
cout<<Ch<<endl;
}
fcin.getline(Ch,100);
}
fcin.close();
}

Question 48:
Write a function in C++ to read the content of a text file “DELHI.TXT” and display all those lines on screen, which are either starting with ‘D’ or starting with ‘M’. All india 2012
Answer:

void Display()
{
char str[100]; 
ifstream fcin("DELHI.TXT"); 
fcin.getline(str,100); 
while(!fcin.eof!)
{
if(str[0]=='D' | |str[0]— ’M’) 
{
cout<<str<<endl;
}
fcin.getline(str,100);
}
fcin.close()
{

Question 49:
Write a function in C++ to search for the details (Number and Calls) of those Mobile phones, which have more than 1000 calls from a binary file “mobile.dat”. Assuming that this binary file contains records/objects of class Mobile, which is defined below. Delhi 2012

class Mobile
{
char Number[10];int Calls; 
public:
void Enter() fgets(Number) ;cin>>Calls;}
void Billing!) {cout<<Number<"#"<<Calls<<endl;}
int GetCal1s() (return Calls;)
};

Answer:

void Display()
{
Mobile M;
ifstream fcinCmobile.dat",
ios;; in | ios:: binary) ; 
fcin.read!(char*)&M, sizeof(M)); 
while(fin.eofc)
{
if(M.GetCalIs()>1000)
{
M.Billing();
}
fcin.read((char*)&M,sizeof(M));
}
fcin.close();
}

Question 50:
Write a function C++ to search for the details (Phoneno and Calls) of those phones, which have more than 1000 calls from a binary file “phones.dat”. Assuming that this binary file contains records/objects of class Phone, which is defined below.
All India 2012

class Phone
{
char Phoneno[10];int Calls; 
public:
void get(){gets(Phoneno) ;cin>>CalIs;}
void billing() (cout<<Phoneno<<"#”<<Calls<<endl;}
int getCal1s()(return Calls;}
};

Answer:

void show()
{
ifstream fcin!"phones.dat".
ios::in|ios::binary);
Phone P;
fcin.read((char*)&P,sizeof(P)); 
while(fcin)
{
if(P.getCalls()>1000)
{
P.billing();
}
fcin.read((char*)&P,sizeof(P));
}
fcin.close();
}

Question 51:
Write a function in C++ to count the number of “Me” or “My” words present in a text file “DIARY.TXT”. If the file “DIARY.TXT” content is as follows: Delhi 2011

My first book was Me and My Family.
It gave me chance to be known to the world.

The output of the function should be

Count of Me/My in file : 4

Answer:

void count()
{
ifstream fin!"DIARY.TXT");
char word[10];
int C = 0;
while(!fin.eof())
{
fin>>word; *
if((strcmpi(word,"Me")== 0)
||(strcmpi(word,"My")== 0))
C++;
}
cout<<Count of Me/My in file:"<<C; fin.close();
}

Question 52:
Write a function in C++ to search!) for a laptop from a binary file “LAPTOP.DAT” containing the objects of class LAPTOP (as defined below). The user should enter the ModelNo and the function should search and display the details of the LAPTOP. Delhi 2011

class LAPTOP
{
long ModelNo; 
float RAM,HDD; 
char Details[120];
public
void StockEnter( Hcin>>ModelNo>>RAM>>HDD;gets(Detail s);}
void StockDisplay() {cout<<Model No<<RAM<<HDD<<Details<<endl;} 
long ReturnModelNo(){return ModelNo;}
};

Answer:

void Search() 
{
LAPTOP L;
long Model No; 
if stream fin;
cout<<"enter the model no of laptop:"; cin>>ModelNo; 
fin.open("LAPTOP.DAT", ios:binary); 
while(fin.read((char*)&L, sizeof(L))
{
if(L.ReturnModelNo() == Model No) 
}
L.StockDisplay();
}
fin.close();
}

Question 53:
Write a function in C++ to count the number of “He” or “She” words present in a text file “STORY.TXT”. If the file “STORY.TXT” content is as follows. All India 2011

He is playing in the ground. She is playing with her dolls.

The output of the function should be

Count of He/She in file:2

Answer:

void Wordcount()
}
ifstream finOSTORY.TXT"); 
char word [10]; 
int C=0;
while(!fin.eof())
{
fin>>word;
if ((strcmpi (word, "He" )== 0)
||(strcmpi(word,"She") == 0)) 
C++;
}
cout<<"Count of He/She in file : 
”<<C;
fin.close();
}

Question 54:
Write a function in C++ to search for a camera from a binary file “CAMERA.DAT” containing the objects of class CAMERA (as defined below). The user should enter the ModelNo and the function should search and display the details of the camera.
All India 2011

class CAMERA 
{
long Model No; 
float Megapixel; 
int Zoom;
char Detai1s[120]; .
public;
void Enter() (cin>>ModelNo>>MegaPixel>>Zoom; gets (Detai Is);} 
void Display() (cout<<Model No<<MegaPixel <<Zoom<<Details<<endl;} 
long GetModelNo(){return ModelNo;}
};

Answer:

void SearchCam()
{
CAMERA C; 
long Model No; 
ifstream fin;
cout<<"enter the camera model no:"; 
cin>>ModelNo;
 fin.openCCAMERA.DAT", ios::binary);
while(fin.read((char*)&C, sizeof(C)))
{
if (C.GetModel No()==ModelNo) 
C.Display();
}
fin.close();
}

Question 55:
Write a function in C++ to count the words “to” and “the” present in a text file “POEM.TXT”. (Note that the words “to” and “the” are complete words) All India 2010
Answer:

void Wordcount()
{
ifstream fin("P0EM.TXT"); 
char word[80]; 
|| int WC=0;
while(ifin.eof())
{
fin>>word;
if((strcmp(word,"to")== 0)
11Cstrcmp(word,"the")— 0)) 
WC++;
}
cout<<"Count of to/the in file;
"<<WC;
fin.close();
}

Question 56:
Write a function in C++ to search and display details of all trains, whose destination is “Delhi” from a binary file “TRAIN.DAT”. Assuming the binary file is containing the objects of the following class: All India 2010

class TRAIN
{
int Tno; //Train number
char From[20]; //Train starting point
charTo[20]; //Train destinaton
public:
char *GetFrom()(return From;}
char *GetTo(){return To;}
void InputO (cin>>Tno; gets(From);gets(To):}
void Show() {cout<<Tno<<":"<<From<<":"<<To<<endl;}
};

Answer:

void Read() 
{
TRAIN T; 
ifstream fin;
fin.open("TRAIN.DAT",ios::binary); 
while(fin.read((char*)&T,sizeof(T))) 
{
if(strcmpi(T.GetTo(), "Del hi")== 0)
T.Show();
}
fin.close!);
}

Question 57:
Write a function in C++ to count the words “this” and “these” present in a text file “ARTICLE.TXT”. [Note that the words “this” and “these” are complete word] Delhi 2010
Answer:

void Wordcount()
{
ifstream finCARTICLE.TXT"); 
char word[80];
int C=0;
while!ifin.eof())
{
fin>>word;
if((strcmpi(word,"this") == 0)
||(strcmpi(word,"these") == 0)) 
C++;
}
cout<<"Number of this and these words are; "<<C; 
fin.close();
}

Question 58:
Write a function in C++ to search and display details of all flights, whose destination is “Mumbai” from a binary file “FLIGHT.DAT”. Assuming the binary file is containing the objects of the following class: Delhi 2010

class FLIGHT
{ 
int Fno; //flight number
char From[20]; //flight starting point
char To[20]; //flight destination
public:
char *GetFrom(){return From;}
char *GetTo(){return To;}
void Enter(){cin>>Fno;gets(From);gets(To);}
void Display()(cout<<Fno<<":”<<From<<”:"<<To<<endl;}
};

Answer:

void Read()
{
FLIGHT F;
ifstream fin;
fin.open("FLIGHT.DAT", ios::binary); 
while(fin.read((char*)&F, 
sizeof(F)))
{
if(strcmpi(F.GetTo(), "Mumbai") == 0)
F.Display();
}
fin.close!);
}

Question 59:
Write a function COUNT_TO() in C++ to count the presence of a word ‘To” in a text file “NOTES.TXT”. All India 2009
If the content of the file “NOTES.TXT” is as:

It is very important to know that 
Smoking is injurious to health 
Let us take initiative to stop it

The function COUNT_TO() will display the following message:
Count of-to-in file: 3
Answer:

void COUNT_TO
{
ifstream fin("N0TES.TXT"); 
char str[10]; 
int C=0;
while(!fin.eof)))
{
fin>>str;
if(strcmpi(str,"To")==0)
C++;
}
fin.close(); 
cout<<"Count of-to-in file: "<<C<<endl;
}

Question 60:
Write a function in C++ to read and display the details of all the users whose membership type is ‘L’ or ‘M’ from a binary file “CLUB.DAT”. Assuming the binary file “CLUB.DAT” is containing objects of class CLUB, which is defined as follows: All India 2009

class CLUB
{
int Mno; 
char Mname[20]; //member name
char Type: //member type:L Life Member M Monthly Member G Guest
public:
void Register(); //function to enter the content
void Display();  //function to display all data members 
char *WhatType() {return Type;}

Answer:

void DisplayMember()
{
CLUB C; 
ifstream fin;
fin.open("CLUB.DAT",ios::binary);
while(fin.read((char*)&C,
sizeof(C)))
{
if(((strcmpi(C.whatType!),"L") ==0)||(strempi(C.whatType (), "M" )==0))) 
C.DisplayO;
}
fin. close();
}

Question 61:
Write a function COUNT_DO() in C++ to count the presence of a word ‘do’ in a text file “MEMO.TXT”. Delhi 2009
If the content of the file “MEMO.TXT” is as

I will do it, if you request me to do it 
It would have been done much earlier

The function COUNT_DO() will display the following message
Count of do in file: 2

Answer:

void C0UNT_D0()
{
ifstream fin!("MEM0.TXT"); 
char str[10]; 
int C = 0; 
while(!fin.eof)))
{
fin>>str;
if(strcmpi(str,"Do")== 0)
C++;
}
fin.close();
cout<<"Count of do in file:" <<C<<endl;
}

Question 62:
Write a function in C++ to read and display the details of all the users whose status is ‘A’ (i.e. Active) from a binary file “USER.DAT”. Assuming the binary file “USER.DAT” is containing objects of class USER, which is defined as follows: Delhi 2009

class USER 
{
 int Uid;
 char Uname[20]; //username
 char Status: // user type : A active I inactive
 public:
 void Register(); //function to enter the content
 void Show(); //function to display all data members
 char *GetStatus(){return Status;}
};

Answer:

void DisplayActive()
{
USER U;
ifstream fin;
fin.open("USER.DAT",ios::binary); 
while(fin.read!(char*)&U,sizeof(U))) 
{
if(strcmpi(U.Get status(),
"A”)—0)
U.Show();
}
fin. close();
}