Sponsor Area
Write the type of C++ tokens (keywords and user-defined identifiers) from the following :
(i) For
(ii) delete
(iii) default
(iv) Value
(i) For - user defined identifier
(ii) delete - keyword
(iii) default - keyword
(iv) Value - user defined identifier
Anil typed the following C++ code and during compilation, he found four errors as follows:
(i) Function strlen should have a prototype
(ii) Undefined symbol cout
(iii) Undefined symbol endl
(iv) Function getchar should have a prototype
On asking his teacher told him to include necessary header files in the code. Write the names of the header files, which Anil needs to include, for successful
compilation and execution of the following code :
void main()
{
char S[] = 'Hello';
for(int i = 0; i<strlen(S); i++)
S[i] = S[i]+1;
cout<<S<<end1;
getchar();
}
In the given program header files are missing such as iostream.h, stdio.h and string.h.
Rewrite the following C++ code after removing any/all syntactical errors with each correction underlined.
Note: Assume all required header files are already being included in the program.
void main()
{
cout<<'Enter an integer”;
cin>>N;
switch(N%2)
case 0 cout<<'Even'; Break;
case 1 cout<<'Odd'; Break;
}
void main()
{
int N;
cout<<'Enter an integer';
cin>>N;
switch(N%2)
{
case 0 :
cout<<'Even'; break;
case 1 :
cout<<'Odd' ; break;
}
Each correction is bold and underlined.
Look at the following C++ code and find the possible output(s) from the options (i) to (iv) following it. Also, write the maximum values that can be assigned to each of the variables R and C.
Note:
● Assume all the required header files are already being included in the code.
● The function random(n) generates an integer between 0 and n-1
void main()
{
randomize();
int R=random(3),C=random(4);
int MAT[3][3] = {{10,20,30},{20,30,40},{30,40,50}};
for(int I=0; I<R; I++)
{
for(int J=0; J<C; J++)
cout<<MAT[I][J]<<' ';
cout<<endl;
}
}
(i) | (ii) |
10,20,30 20,30,40 30,40,50 |
10,20,30 20,30,40 |
(iii) | (iv) |
10,20 20,30 |
10,20 20,30 30,40 |
(ii) and (iii)
Max Value of R:2
Max Value of C:3
Aditi has used a text editing software to type some text. After saving the article as WORDS.TXT, she realised that she has wrongly typed alphabet J in place of the alphabet I everywhere in the article.
Write a function definition for JTOI() in C++ that would display the corrected version of the entire content of the file WORDS.TXT with all the alphabets “J” to be displayed as an alphabet 'I' on screen.
Note: Assuming that WORD.TXT does not contain any J alphabet otherwise.
Example: If Aditi has stored the following content in the file WORDS.TXT :
'WELL, THJS JS A WORD BY JTSELF. YOU COULD STRETCH THJS TO BE A SENTENCE'
The function JTOI() should display the following content:
'WELL, THIS IS A WORD BY ITSELF. YOU COULD STRETCH THIS TO BE A SENTENCE'
#include <iostream.h>
int main(void)
{
clrscr();
JTOI();
getch();
}
void JTOI()
{
char ch;
ifstream F('WORDS.TXT' );
while(F.get(ch))
{
if(ch=='J')
ch='I';
cout<<ch;
}
F.close(); //IGNORE
}
Write a definition for function COUNTDEPT( ) in C++ to read each object of a binary file TEACHERS.DAT, find and display the total number of teachers in the department MATHS. Assume that the file TEACHERS.DAT is created with the help of objects of class TEACHERS, which is defined below:
class TEACHERS
{
int TID; char DEPT[20];
public:
void GET()
{
cin>>TID;gets(DEPT);
}
void SHOW()
{
cout<<TID<<':'<<DEPT<<endl;
}
char *RDEPT(){return DEPT;}
};
void COUNTDEPT()
{
ifstream F;
F.open('TEACHERS.DAT', ios::binary);
int count=0;
Teachers obj;
while(F.read((char*) &obj, sizeof(obj)))
{
if(strcmp(obj.RDEPT(),'MATHS')==0)
count++;
}
cout<<'Number of MATHS teachers :'<<count<<endl;
F.close();
}
Write the type of C++ tokens (keywords and user defined identifiers) from the following:
(i) new
(ii) While
(iii) case
(iv) Num_2
Types of C++ tokens:
(i) new - Keyword
(ii) While - User defined Identifier
(iii) case - Keyword
(iv) Num_2 - User defined Identifier
Anil typed the following C++ code and during compilation, he found three errors as follows:
(i) Function strlen should have prototype
(ii) Undefined symbol cout
(iii) Undefined symbol endl
On asking, his teacher told him to include necessary header files in the code.Write the names of the header files, which Anil needs to include, for successful compilation and execution of the following code.
{
char Txt[] = 'Welcome';
for(int C= 0; C<strlen(Txt); C++)
Txt[C] = Txt[C]+1;
cout<<Txt<<endl;
}
Header files require for successful compilation and execution of the given code are:
string.h
iostream.h
Rewrite the following C++ code after removing any/all syntactical errors with each correction underlined.
Note: Assume all required header files are already being included in the program.
void main()
{
cout<<'Enter an Alphabet:';
cin>>CH;
switch(CH)
case 'A' cout<<'Ant'; Break;
case 'B' cout<<'Bear' ; Break;
}
void main()
{
cout<<'Enter an Alphabet:';
char CH;
cin>>CH;
switch(CH)
{
case ‘A’ :
cout<<'Ant'; break;
case ‘B’ :
cout<<'Bear'; break;
}
}
Look at the following C++ code and find the possible output(s) from the options (i) to (iv) following it. Also, write the maximum values that can be assigned to each of the variables N and M.
Note:
● Assume all the required header files are already being included in the code.
● The function random(n) generates an integer between 0 and n-1
void main()
{
randomize();
int N=random(3),M=random(4);
int DOCK[3][3] = {{1,2,3},{2,3,4},{3,4,5}};
for(int R=0; R<N; R++)
{
for(int C=0; C<M; C++)
cout<<DOCK[R][C]<<' ';
cout<<endl;
}
}
(i) | (ii) |
1 2 3 2 3 4 3 4 5 |
1 2 3 2 3 4 |
(iii) | (iv) |
1 2 2 3 |
1 2 2 3 3 4 |
Correct Options: (ii) and (iii)
Maximum value of N = 2
Maximum value M = 3
Polina Raj has used a text editing software to type some text in an article. After saving the article as MYNOTES.TXT, she realised that she has wrongly typed alphabet K in place of alphabet C everywhere in the article.
Write a function definition for PURETEXT() in C++ that would display the corrected version of the entire article of the file MYNOTES.TXT with all the alphabets “K” to be displayed as an alphabet “C” on screen.
Note: Assuming that MYNOTES.TXT does not contain any C alphabet otherwise.
Example:
If Polina has stored the following content in the file MYNOTES.TXT:
I OWN A KUTE LITTLE KAR.
I KARE FOR IT AS MY KHILD.
The function PURETEXT() should display the following content:
I OWN A CUTE LITTLE CAR.
I CARE FOR IT AS MY CHILD.
void PURETEXT()
{
fstream fp1;
fp1. open('MYNOTES.txt', ios::in | ios::out);
if(!fp1)
{
cout<<'Cannot open file'<<endl;
exit(0);
}
char ch;
char c;
while(!fp1.eof())
{
c=fpl.get();
if(c= ='K')
{
fp1.seekg(–1, ios::cur);
fp1.put('C');
}
}
fp1.clear();
fp1.seekp(0, ios::beg);
cout<<'\nAfter replacing character\n';
while(!fp1.eof())
{
fp1.get(ch);
cout<<ch;
}
fp1.close();
}
Write a definition for function COUNTPICS ( ) in C++ to read each object of a binary file PHOTOS.DAT, find and display the total number of PHOTOS of type PORTRAIT. Assume that the file PHOTOS.DAT is created with the help of objects of class PHOTOS, which is defined below:
class PHOTOS
{
int PCODE;
char PTYPE[20]; //Photo Type as “PORTRAIT”,”NATURE”
public:
void ENTER()
{
cin>>PCODE;gets(PTYPE);
}
void SHOWCASE()
{
cout<<PCODE<<':'<<PTYPE<<endl;
}
char *GETPTYPE(){
return PTYPE;
}
};
void COUNTPICS( )
{
int count = 0;
PHOTOS obj;
ifstream fp1;
fp1.open('PHOTOS.DAT', ios::binary);
while (fp1.read((char*)&obj, sizeof(obj)))
{
if(strcmp(obj.GETPTYPE(),'PORTRAIT')==0){
count++;
}
}
cout<<'The total number of PHOTOS of type PORTRAIT is '<<count;
fp1.close();
}
Sponsor Area
Out of the following, find those identifiers, which cannot be used for naming Variable, Constants or Functions in a C++ program:
_Cost, Price*Qty, float, Switch,
Address One, Delete, Number12, do
Price*Qty, Address One, do and float is cannot be used for naming variable, constants or Functions in a C++ program.
Jayapriya has started learning C++ and has typed the following program. When she compiled the following code written by her, she discovered that she needs to include some header files to successfully compile and execute it. Write the names of those header files, which are required to be included in the code.
void main()
{
float A,Number,Outcome;
cin>>A>>Number;
Outcome=pow(A,Number);
cout<<Outcome<<endl;
}
Missing header files,
(i) (iostream.h
(ii) math.h
Rewrite the following C++ code after removing any/all syntactical errors with each correction underlined.
Note: Assume all required header files are already being included in the program.
#define Equation(p,q) = p+2*q
void main()
{
float A=3.2;B=4.1;
C=Equation(A,B);
cout<<'Output='<<C<<endl;
}
#define Equation(p,q) p+2*q
void main()
{
float A=3.2 , B=4.1;
float C=Equation(A,B);
cout<< ”Output=” <<C<<endl;
}
Find and write the output of the following C++ program code:
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=1;}
void RegCode(long int I,float R)
{
ID=I; 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();
}
Output:
Date :1
1024#150
Date :29
2015#400
Date :20
1001#180
Look at the following C++ code and find the possible output(s) from the options (i) to (iv) following it. Also, write the maximum and the minimum values that can be assigned to the variable CHANGER.
Note:
● Assume all the required header files are already being included in the code.
● The function random(n) generates an integer between 0 and n‐1.
void main()
{
randomize();
int CHANGER;
CHANGER=random(3);
char CITY[][25]={'DELHI','MUMBAI','KOLKATA','CHENNAI'};
for(int I=0;I<=CHANGER;I++)
{
for(int J=0;J<=I;J++){
cout<<CITY[J];
}
cout<<endl;
}
}
(i) | (ii) |
DELHI DELHIMUMABAI DELHIMUMABAIKOLKATA |
DELHI DELHIMUMABAI DELHIMUMABAIKOLKATA DELHIMUMABAIKOLKATACHENNAI |
(iii) | (iv) |
MUMABAI MUMABAIKOLKATA MUMABAIKOLKATACHENNAI |
KOLKATA KOLKATACHENNAI |
(i)
DELHI
DELHIMUMBAI
DELHIMUMBAIKOLKATA
Minimum Value of CHANGER = 0
Maximum Value of CHANGER = 2
Find the correct identifiers out of the following, which can be used for naming variables, constants or functions in a C++ program:
While, for, Float, new, 2ndName, A%B, Amount2, _Counter
Identifiers which can be used for naming variables constants in a C++ program are given below:
While, Float, Amount2, _Counter
Observe the following program very carefully and write the names of those header files (s), which are essentially needed to compile and execute the following program successfully :
typedef char TEXT[80];
void main()
{
TEXT Str[] = 'Peace is supreme';
int Index=0;
while (Str[Index]!='\0'){
if (isupper(Str[Index])){
Str[Index++]='#';
}
else{
Str[Index++]='*';
}
}
puts(Str);
}
The header files, which are essential to compile and execute the given code are:
Observe the following C++ code very carefully and rewrite it after removing any/all syntactical errors with each correction underlined.
Note: Assume all required header files are already being included in the program.
#Define float Max=70.0;
Void main()
{
int Speed
char Stop='N';
cin>>Speed;
if Speed>Max
Stop='Y';
cout<<Stop<<end;
}
#define Max 70.0 //Error 1,2,3
void main() //Error 4
{
int Speed; //Error 5
char Stop='N';
cin>>Speed;
if (Speed>Max){ //Error 6
Stop='Y';
}
cout<<Stop<<endl; //Error 7
}
Study the following program and select the possible output(s) from the options (i) to (iv) following it. Also, write the maximum and the minimum values that can be assigned to the variable NUM.
Note :
– Assume all required header files are already being included in the program.
– random(n) function generates an integer between 0 and n – 1.
void main()
{
randomize();
int NUM;
NUM=random(3)+2;
char TEXT[]='ABCDEFGHIJK';
for (int I=1;I<=NUM; I++)
{
for(int J=NUM; J<=7;J++){
cout<<TEXT[J];
}
cout<<end1;
}
}
(i) | (ii) | (iii) | (iv) |
FGHI FGHI FGHI FGHI |
BCDEFGH BCDEFGH EFGH EFGH |
EFGH EFGH EFGH EFGH |
CDEFGH CDEFGH |
Possible outcome
(iii) and (iv)
Minimum value of NUM = 2
Maximum value of NUM = 4
Write a definition for function Economic() in C++ to read each record of a binary file ITEMS.DAT, find and display those items, which costs less than 2500. Assume that the file ITEMS.DAT is created with the help of objects of class ITEMS, which is defined below:
class ITEMS
{
int ID;char GIFT[20]; float Cost;
public:
void Get()
{
cin>>CODE;gets(GIFT);cin>>Cost;
}
void See()
{
cout<<ID<<':'<<GIFT<<':'<<Cost<<endl;
}
float GetCost()
{
return Cost;
}
};
void Economic()
{
ITEMS I;
ifstream fin('ITEMS.DAT',ios::binary);{
while (fin.read((char *)&I,sizeof(I)))
{
if(I.GetCost()<2500)
I.See();
}
}
fin.close();
}
Give the difference between the Type Casting and automatic Type Conversion. Also, give a suitable C++ code to illustrate both.
Type Casting | Automatic Type Conversion |
Type Casting is used to convert the value of one type to another type. | Automatic Type Conversion is the type conversion done by the compiler wherever required. |
For example, float x = (float)3/2; //1.5 will be assigned as result, because 3 is converted into 3.0 |
For example, float x = 3/2; // here 1.0 will be assigned as result, because 1 is automatically converted in 1.0 |
Which C++ header file(s) are essentially required to be included to run/execute the following C++ source code (Note: Do not include any header file, which is/are not required);
Void main()
{
char TEXT[] = 'SomeThing';
cout<<'Remaining SMS Chars:'
<<160-strlen(TEXT)<< endl;
}
Header file required to execute C++ program are -
(i) iostream.h
(ii) string.h
Rewrite the following program after removing the syntactical error(s) (if any). Underlie each correction.
#include<iostream.h>
Class Item
{
long IId,Qty;
public:
void Purchase
{
cin>>IId>>Qty;
}
} void Sale()
{
cout<<setw(5)<<IId<<'Old:'<<Qty<<endl;
cout<<'New:'<<--Qty<< endl;
}
void main()
{
Item I;
Purchase();
I.Sale();
I.Sale(0)
}
#include<iostream.h>
#include<iomanip.h>
class Item
{
long IId,Qty;
public:
void Purchase()
{
cin>>IId>>Qty;
}
} void Sale()
{
cout<<setw(5)<<IId<<'Old:'<<Qty<<endl;
cout<<'New:'<<--Qty<< endl;
};
void main()
{
Item I;
I.Purchase();
I.Sale();
I.Sale(0);
}
Find the output of the following program:
#include<iostream.h>
class METRO
{
int Mno, TripNo,PassengerCount;
public:
METRO (int Tmno=1)
{
Mno=Tmno;TripNo=0;PassengerCount=0;
}
void Trip(int PC=20)
{
TripNo++;PassengerCount+=PC;
}
void StatusShow()
{
cout<<Mno<<':'<<TripNo<<':'<<PassengerCount<<endl;
}
};
void main()
{
METRO M(5),T;
M.Trip();
T.Trip(50);
M.StatusShow();
M.Trip(30);
T.StatusShow();
M.StatusShow();
}
Output of the given code:
5:1:20
1:1:50
5:2:50
Find the output of the following program:
#include<iostream.h>
#include<c.type.h>
typeof char Str80[80];
void main()
{
char*Notes;
Str80 Str='vR2GooD':
int L = 6;
Notes =Str;
while(L>=3)
{
Str[L]=(isupper(Str[L])?tolower(Str[L]):toupper(Str[L]));
cout<<Notes<<endl;
L--;
Notes++;
}
}
The output of the following program:
vR2Good
R2GoOd
2GOOd
gOOd
Observe the following program and find out, which output(s) out of(i) to(iv) will not be expected from the program? What will be the minimum and the maximum value assigned to the variable chance?
#include<iostream.h>
#include<stdlib.h>
void main( )
{
randomize( );
int Arr[]={9,6},N;
int Chance=random(2)+10;
for (int C=0;C<2;C++)
{
N=random(2);
cout<<Arr[N]+Chance<<'#';
}
}
(i) 9#6# (ii) 19#17# (iii) 19#16# (iv) 20#16# |
(iii) 19#16#
Minimum Value: 16
Maximum Value: 20
Sponsor Area
Sponsor Area