<p></p>
Q.1. Write the output of the following program:
#include<iostream.h>
#include<conio.h>
int a =3;
void demo(int &x, int y, int *z)
{
a+= x;
y*=a;
*z = a+y;
cout<< a << “”<< x << “”<<y << “”<<z <<endl;
}
void main( )
{
clrscr( );
int a = 2, b =5;
demo(::a,a, &b);
cout<< ::a<< “”<<a<< “”<< b<<endl;
}
Ans.1. In function definition, 1 variable by reference, 1 normal variable and 1 pointer variable is used. In calling for a pointer variable, always use & symbol. Calling : demo(3,2,Address of b);
::a and x are having same address ( due to by reference)
a =a+x
=3+3=6
y = y * a
= 2*6=12
*z = a + y
= 6+12 = 18
First output : 6(value of a), 6 ( value of x), 12, z will be any valid address
0xfff4
Second output: 6,2,18( value stored at the address pointed by z i.e. value
of b)
Q.2. Find the ouptput of the following :
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void main( )
{
char *Name= “IntRAneT”;
for(int x =0; x<strlen(Name); x++)
{
if(islower(Name[x]) )
Name[x]=toupper(Name[x] );
else
if(isupper(Name[x]) )
if (x%2 = =0)
Name[x]=tolower(Name[x]);
else
Name[x]=Name[x-1];
}
puts(Name);
}
Ans.2. iNTTaNEE
Q.3.. Give the output of the following program:
void main()
{
int x [] = { 10, 20, 30, 40, 50};
int *p, **q, *t;
p = x;
t = x + 1;
q = &t;
cout << *p << “\t” << **q << “\t” << *t++;
Ans.3. 10 20 30
Explanation:
here 3 pointer variables p, q and t.
p=x means p contains base address i.e. address of first element of array x.
t = x+1 means t contains address of second element i.e. address of second
element i.e. address of 20.
q =&t means q contains address of t.
cout << *p << “\t” << **q << “\t” << *t++;
In this expression Evaluation will start from right to left.
*t++ means first print the value stored at the address pointed by t i.e.
Value of second element i.e. 20. after that the address pointed by t will be incremented 1 int. ( 2 bytes). Now t will contain the address of 30.
Since q contains address of t so *q will give the address of t. therefore **q will give 30.
Q.4.. What will be the output of the program( Assume all necessary header files are included) :
#include<iostream.h>
void print (char * p )
{
p = "pass";
cout<<"value is "<<p<<endl;
}
void main( )
{
char * x = "Best of luck";
print(x);
cout<<"new value is "<<x<<endl;
}
Ans.4. value is pass
New value is Best of luck
Q.5. What will be the output of the following program
#include<iostream.h>
#include<ctype.h>
#include<conio.h>
#include<string.h>
void changestring(char text[], int &counter)
{
char *ptr = text;
int length=strlen(text);
for(;counter<length-2;counter+=2, ptr++)
{
*(ptr+counter) = tolower(*(ptr+counter));
}
}
void main()
{
clrscr();
int position = 0;
char message[]= “POINTERS FUN”;
changestring(message, position);
cout<<message<< “@” <<position;
}
Ans.5. pOInTErS fUN@10
Q.6. Find the output of the following program : 2
#include<iostream.h>
void main()
{
int Numbers[] = {2,4,8,10};
int *ptr = Numbers;
for (int C = 0; C<3; C++)
{
cout<< *ptr << “@”;
ptr++;
}
cout<<endl;
for(C = 0; C<4; C++)
{
(*ptr)*=2;
--ptr;
}
for(C = 0; C<4; C++)
cout<< Numbers [C]<< “#”;
cout<<endl;
}
Ans.6. 2@4@8@
4#8#16#20#
Q.7. What is the output of the following program if all the necessary header files have been
included:
char *Name= “a ProFile”;
for(int x =0; x<strlen(Name); x++)
{
if(islower(Name[x]) )
Name[x]=toupper(Name[x] );
else
if(isupper(Name[x]) )
if (x%2!=0)
Name[x]=tolower(Name[x-1]);
else
Name[x]--;
}
cout<<Name<<endl;
Ans.7. A OROoILE
If *Name = “a ProFIle” then output would be:
A OROoHLE
Q.8. Find the output of the following program:
#include<iostream.h>
void main( )
{
int U=10,V=20;
for(int I=1;I<=2;I++)
{
cout<<”[1]”<<U++<<”&”<<V – 5 <<endl;
cout<<”[2]”<<++V<<”&”<<U + 2 <<endl;
}
}
Ans.8. [1]10&15
[2]21&13
[1]11&16
[2]22&14
Q.9. #include<stdlib.h>
#include<iostream.h>
void main( )
{
randomize( );
char City[ ][10]={“DEL”,”CHN”,”KOL”,”BOM”,”BNG”};
int Fly;
for(int I=0; I<3;I++)
{
Fly=random(2) + 1;
cout<<City[Fly]<<”:”;
}
}
Outputs:
(i) DEL : CHN : KOL:
(ii) CHN: KOL : CHN:
(iii) KOL : BOM : BNG:
(iv) KOL : CHN : KOL:
Ans.9. Since random(2) gives either 0 or 1, Fly value will be either 1 or 2.
(random(n) gives you any number between 0 to n-1)
City[1] is “CHN”
City[2] is “KOL”
Since I value from 0 to 2 (ie<3), 3 iterations will takes place.
So the possible output consists 3 strings separated by :, each of them may
be either “CHN” or “KOL”. So the possible output will be
(ii) CHN : KOL : CHN:
(iv) KOL :CHN : KOL:
Q.10. Find the output of the following program.
#include<iostream.h>
void Withdef(int HisNum=30)
{
for(int I=20;I<=HisNum;I+=5)
cout<<I<<”,”;
cout<<endl;
}
void Control(int &MyNum)
{
MyNum+=10;
Withdef(MyNum);
}
void main()
{
int YourNum=20;
Control(YourNum);
Withdef();
cout<<”Number=”<<YourNum<<endl;
Ans.10. 20,25,30,
20,25,30,
Number=30
Q.11. Find the output of the following program:
#include<iostream.h>
void main( )
{
long NUM=1234543;
int F=0,S=0;
do
{
int R=NUM % 10;
if (R %2 != 0)
F += R;
else
S += R;
NUM / = 10;
} while (NUM>0);
cout<<F-S;
}
Ans.11. 2
Q.12. Observe the following program GAME.CPP carefully, if the value of Num entered by the user is 14, choose the correct possible output(s) from the options from (i) to (iv), and
justify your option.
//Program:GAME.CPP
#include<stdlib.h>
#include<iostream.h>
void main( )
{
randomize( );
int Num,Rndnum;
cin>>Num;
Rndnum=random(Num)+7;
for(int N=1;N<=Rndnum;N++)
cout<<N<<” “;
}
Output Options:
(i) 1 2 3 (ii) 1 2 3 4 5 6 7 8 9 10 11
(iii) 1 2 3 4 5 (iv) 1 2 3 4
ans.12. Expected Output
(ii) 1 2 3 4 5 6 7 8 9 10 11
Q.13. Give the output of the following program:
#include<iostream.h>
#include<conio.h>
int g=20;
void func(int &x,int y)
{
x=x-y;
y=x*10;
cout<<x<<’,’<<y<<’\n’;
}
void main( )
{
int g=7;
func(g,::g);
cout<<g<<’,’<<::g<<’\n’;
func(::g,g);
cout<<g<<’,’<<::g<<’\n’;
}
Ans.13. -13,-130
-13,20
33,330
-13,33
Q.14. Find the output of the following program:
#include<iostream.h>
struct Box {
int Len, Bre, Hei;
};
void Dimension(Box B)
{
cout << B.Len << “ X ” << B.Bre << “ X ”;
cout << B.Hei << endl;
}
void main ( )
{
Box B1 = {10, 20, 8}, B2, B3;
++B1.Hei;
Dimension (B1); //first calling
B3= B1;
++B3.Len;
B3.Bre++;
Dimension (B3); // second function calling
B2= B3;
B2.Hei += 5;
B2.Len - = 2;
Dimension (B2); // third function calling
}
Ans.14. Execution starts from main( )
Output due to first function calling
10X20X9
Due to B3=B1
All the values of B1 will be assigned to B3.
Now B3 contains: Length=10, Breadth=20, Height=9
Due to ++B1. Len, Length will be 11.
Due to B3. Bre++, Breadth will be 20 ( Post Increment) but in next
statement incremented value will be used.
So second calling will take ( 11,21,9).
Therefore output for second calling:
11X21X9
9X21X14 ( Third calling)
Therefore final output will be:
10X20X9
11X21X9
9X21X14
Q.15. Find the output of the following program:
#include <iostream.h>
struct PLAY
{ int Score, Bonus;
};
void Calculate(PLAY &P, int N=10)
{
P.Score++;P.Bonus+=N; }
void main()
{
PLAY PL={10,15};
Calculate(PL,5);
cout<<PL.Score<<”:”<<PL.Bonus<<endl;
Calculate(PL);
cout<<PL.Score<<”:”<<PL.Bonus<<endl;
Calculate(PL,15);
cout<<PL.Score<<”:”<<PL.Bonus<<endl;
}
Ans.15. 11:20
12:30
13:45
Q.16.. In the following C++ program , what will the maximum and minimum value of r
generated with the help of random function.
#include<iostream.h>
#include<stdlib.h>
void main()
{
int r;
randomize();
r=random(20)+random(2);
cout<<r;
}
Ans.16. Minimum Value: 0
Maximum Value:20
Q.17. Study the following program and select the possible output from it:
#include<iostream.h>
#include<stdlib.h>
const int Max=3;
void main( )
{
randomize();
int Number;
Number=50+random(Max);
for(int P=Number; P >=50;P- -)
cout<<P<<”#”;
cout<<endl;
}
(i) 53#52#51#50#
(ii) 50#51#52#
(iii) 50#51#
(iv) 51#50#
Ans.17. 51#50#
Q.18. Find the output of the following program:
#include<iostream.h>
void main()
{
int A[]={10,20,30,40,50};
int *p=A;
while(*p<30)
{
if(*p%3!=0)
*p = *p+2;
else
*p=*p+1;
*p++;
}
for(int J=0;J<=4;J++)
{
cout<<A[J]<< "@";
if(J%3 == 0)
cout<<endl;
}
cout<<A[4]*3<<endl;
}
Ans.18. 12@
22@30@40@
50@150
Q.19. Find the output of the following program:
#include <iostream.h>
void Changethecontent(int Arr[ ], int Count)
{
for (int C=1;C<Count;C++)
Arr[C-1]+=Arr[C];
}
void main( )
{
int A[ ]={3,4,5},B[ ]={10,20,30,40},C[ ]={900,1200};
Changethecontent(A,3);
Changethecontent(B,4);
Changethecontent(C,2);
for (int L=0;L<3;L++) cout<<A[L]<<’#’;
cout<<endl;
for (L=0;L<4;L++) cout<<B[L] <<’#’;
cout<<endl;
for (L=0;L<2;L++) cout<<C[L] <<’#’; }
Ans.19. 7#9#5#
30#50#70#40#
Q.20. In the following program, if the value of Guess entered by the user is 65, what will be the expected output(s) from the following options (i), (ii), (iii) and (iv)?
#include <iostream.h>
#include <stdlib.h>
void main()
{
int Guess;
randomize();
cin>>Guess;
for (int I=1;I<=4;I++)
{
New=Guess+random(I);
cout<<(char)New;
} }
(i) ABBC
(ii) ACBA
(iii) BCDA
(iv) CABD
Ans.20. (i) ABBC
21. #include <iostream.h>
void Secret(char Str[ ])
{
for (int L=0;Str[L]!='\0';L++);
for (int C=0;C<L/2;C++)
if (Str[C]=='A' || Str[C]=='E')
Str[C]='#';
else
{
char Temp=Str[C];
Str[C]=Str[L-C-1];
Str[L-C-1]=Temp;
}
}
void main()
{
char Message[ ]="ArabSagar";
Secret(Message);
cout<<Message<<endl;
}
Ans.21. #agaSbarr
Q.22. Find the output of the following code.
#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int a =32;
int *ptr = &a;
char ch = 'D';
char *cho=&ch;
*cho+=a;
*ptr += ch;
*ptr *= 3;
ch=ch-30;
cout<< a << "" <<--ch<<endl;
}
Ans.22. 396 E
Q.23. Give the output of the following program.
#include<iostream.h>
void main( )
{
char *p="Difficult";
char c;
c=*p++;
cout<<c<<c++<<++c<<"\n";
char d =c+1;
cout<<d++<<"\n";
cout<<d<<"\n";
cout<<*p;
}
Ans.23. FEE
G
H
i
Q.24. Given a binary file PHONE.DAT, containing records of the following structure type
class Phonlist
{
char Name[20];
char Address[30];
char AreaCode[5];
char PhoneNo[15];
public:
void Register();
Void Show();
int CheckCode(char AC[])
{
return strcmp(AreaCode,AC);
}
};
Write a function TRANSFER ( ) in C++, that would copy all those records which are having AreaCode as “DEL” from PHONE.DAT to PHONBACK.DAT.
Ans.24.
void TRANSFER()
{
Phonlist P;
fstream fin,fout;
fin.open(“PHONE.DAT”,ios::binary|ios::in);
fout.open(“PHONBACK.DAT”,ios::binary|ios::out);
while(fin.read((char*)&P,sizeof(P)))
{
if(P.CheckCode(“DEL”)==0)
fout.write((char*)&P,sizeof(P));
}
fin.close(); //ignore
fout.close(); //ignore
}
OR
void TRANSFER()
{
Phonlist P;
fstream fin,fout;
fin.open(“PHONE.DAT”,ios::binary|ios::in);
fout.open(“PHONBACK.DAT”,ios::binary|ios::in);
if(fin)
{
fin.read((char*)&P,sizeof(P));
while(!fin.eof())
{
if(P.CheckCode(“DEL”)==0)
fout.write((char*)&P,sizeof(P));
fin.read((char*)&P,sizeof(P));
}
}
fin.close(); //ignore
fout.close(); //ignore
}
Q.25. Given a binary file TELEPHON.DAT, containing records of the following class
Directory:
class Directory
{
char Name[20];
char Address[30];
char AreaCode[5];
char Phone_No[15];
public:
void Register();
void Show();
int CheckCode(char AC[])
{
return strcmp(AreaCode,AC[]);
}
};
Write a function COPYABC in C++ that would copy only those records having AreaCode as “123” from TELEPHON.DAT to TELEBACK.DAT.
Ans.25. //Function to copy records from TELEPHON.DAT to
//TELEBAC.DAT
void COPYABC()
{
fstream IS(“TELEPHON.DAT”,ios::binary|ios::in);
fstream OA(“TELEBACK.DAT”,ios::binary|ios::out);
Directory D;
while(IS.read((char*) &D,sizeof(D)))
{
if(D.CheckCode(”123”)==0)
OA.write((char *)&D,sizeof(D));
}
IS.close();
OA.close();
}
Q.26. Given a binary file SPORTS.DAT, containing 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”.
Ans.26. //Function to copy records from SPORTS.DAT to
//ATHELETIC.DAT
void SP2AT()
{
Sports S;
fstream IS(“SPORTS.DAT”,ios::binary|ios::in);
fstream OA(“ATHLETIC.DAT”,ios::binary|ios::out);
while(IS)
{
IS.read((char*) &S,sizeof(S));
if(strcmp(S.Event,”Athletics”)==0)
OA.write((char *)&S,sizeof(S));
}
IS.close();
OA.close();
}
OR
void SP2AT()
{
fstream F1,F2;
Sports S;
F1.open(“SPORTS.DAT”,ios::binary|ios::in);
F2.open(“ATHLETIC.DAT”,ios::binary|ios::out);
while(F1.read((char*) &S,sizeof(S)))
{
if(!strcmp(S.Event,”Athletics”))
F2.write((char *)&S,sizeof(S));
}
F1.close();
F2.close();
}
OR
void SP2AT()
{
fstream F1,F2;
Sports S;
F1.open(“SPORTS.DAT”,ios::binary|ios::in);
F2.open(“ATHLETIC.DAT”,ios::binary|ios::out);
while(F1)
{
F1.read((char*) &S,sizeof(S));
if(!strcmp(S.Event,”Athletics”))
F2.write((char *)&S,sizeof(S));
}
F1.close();
F2.close();
}
Q.27. Write a function in C++ to search for a BookNo from a binary file “BOOK.DAT”, assuming the binary file is containing the objects of the following class.
class BOOK
{
int Bno;
char Title[20];
public:
int RBno(){return Bno;}
void Enter(){cin>>Bno;gets(Title);}
void Display(){cout<<Bno<<Title<<endl;}
};
Ans.27. void BookSearch()
{
Fstream FIL(“BOOK.DAT”, ios::binary|ios::in);
BOOK B;
int bn, Found=0;
cout<<”Enter Book Num to search…”;
cin>>bn;
while(FIL.read((char*)&S, sizeof(S)))
if(B.RBno()==bn)
{
B.Dispaly( );
Found++;
}
if(Found==0)
cout<<”Sorry!!! Book not found!!!”<<endl;
FIL.close();
}
Q.28. Write a function in C++ to add new objects at the bottom of a binary file “STUDENT.DAT”, assuming the binary file is containing the objects of the following class.
class STUD
{
int Rno;
char Name[20];
public:
void Enter()
{
cin>>Rno;gets(Name);
}
void Display(){cout<<Rno<<Name<<endl;}
};
Ans.28. void Addnew()
{
fstream FIL(“STUDENT.DAT”,ios::binary|ios::app);
STUD S;
int n;
cout<<”How many objects do you want to add??”;
cin>>n;
for(int i=0; i<n;i++)
{
S.Enter();
FIL.write((char*)&S,sizeof(S));
}
FIL.close();
}
Q.29. Write a function in C++ to read and display the detail of all the members 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:
class CLUB
{
int Mno.
char Mname[20];
char Type; //Member Type: L Life Member M Monthly member G Guest
public:
void Register( );
void Display( );
char whatType( ) { return type; }
};
Ans.29. void DisplayL_M( )
{
fstream fin(“CLUB.DAT”,ios::binary|ios::in);
CLUB C;
while(fin.read((char*) &C,sizeof(C)))
{
if(C.whatType()==’L’ || C.whatType()==’M’)
C.Display();
}
fin.close();
}
OR
void DisplayL_M( )
{
fstream fin(“CLUB.DAT”,ios::binary|ios::in);
CLUB C;
while(fin)
{
fin.read((char*) &C,sizeof(C))
{
if(C.whatType()==’L’ || C.whatType()==’M’)
C.Display();
}
fin.close();
}
Q.30. Assuming the class DRINKS defined below, write functions in C++ to perform the following :
(i) write the objects of DRINKS to binary file.
(ii) Read the objects of DRINKS from binary file and display them on screen
when Dname has value “ Pepsi”.
class DRINKS
{
int DCode;
char DName[13];
int Dsize; // size in litres.
float Dprice;
}
public:
void getdrinks( )
{ cin>>DCode>>DName>>Dsize>>Dprice;}
void showdrinks( )
{ cout<< DCode<<DName<,Dsize<,Dprice;}
char *getname()
{ return Dname;}
};
Ans.30. (i) void write( )
{
DRINK D;
ofstream outf(“Drink.DAT”,ios::binary);
if(!outf)
cout<<”Unable to open file for writing”);
else
{
D.getdrinks( ); // reading values through object
Outf.write((char*)&D; sizeof(D)); // writing object to file using fout.
Outf.close( );
}
}
(ii) void read( )
{
DRINK D;
ifstream inf(“Drink.DAT”,ios::binary);
if(!inf)
cout<<”Unable to open file for reading”);
else
{
while(inf)
{ inf.read((char*)&D; sizeof(D)); // reading object from file
if((strcmp(D.getname(), “Pepsi”)==0)
{
D.showdrinks( ); // display values through object of class
}
inf.close( );
}
}
}
Note: (i) Due to more objects, we will use while.
(ii) To find out how many such records are found, then you may use count variable to count the number of records accordingly.
Q.31. Write a function in C++ to add a new object in a binary file “Customer.dat”. assume the binary file is containing the objects of the following class:
class customer
{
int CNo; Char CName[21];
public:
void Enterdata( )
{cin>>CNo.;gets(CName);}
void dispdata( )
{ cout<<CNo.<<CName;}
};
Ans.31. Note: in this question, we have to add new object, so we have to use app
mode.
void add_obj( )
{
customer C;
ofstream outf (“Customer.dat”, ios::binary||ios::app);
if(!outf)
{cout<<”Error”; exit(0);}
else
{
C.enterdata();
Outf.write((char*)&C, sizeof(C));
Out.close();
}
}
Q.32. Assuming the class DRINKS defined below, write functions in C++ to perform the following :
(i) Write the objects of DRINKS to binary file.
(ii) Read the objects of DRINKS from binary file and display them on screen
when DCode has value 1234.
class DRINKS
{
int DCode;
char DName[13];
int Dsize; // size in litres.
float Dprice;
}
public:
void getdrinks( )
{ cin>>DCode>>DName>>Dsize>>Dprice;}
void showdrinks( )
{ cout<< DCode<<DName<,Dsize<,Dprice;}
int getcode()
{ return Dcode;}
};
Ans.32. (i) void write( )
{
DRINK D;
ofstream outf(“Drink.DAT”,ios::binary);
if(!outf)
cout<<”Unable to open file for writing”);
else
{
D.getdrinks( ); // reading values through object
Outf.write((char*)&D; sizeof(D)); // writing object to file using fout.
Outf.close( );
}
}
(ii) void read( )
{
DRINK D;
ifstream inf(“Drink.DAT”,ios::binary);
if(!inf)
cout<<”Unable to open file for reading”);
else
{
while(inf)
{
inf.read((char*)&D; sizeof(D)); // reading object from
file
if(D.getcode()==1234)
{
D.showdrinks( ); // display values through object
of class
}
inf.close( );
}
}
Note: (i) To find out how many such records are found, then you may use count variable to count the number of records accordingly.