Documente Academic
Documente Profesional
Documente Cultură
a) Write the type of C++ tokens (keywords and user-defined identifiers) from the following:
(i) Struct (ii) return (iii) else (iv) stack 2m
b) The following C++ code during compilation reports errors as follows: 1m
Error: ‘ofstream’ not declared
Error: ‘strcpy’ not declared
Error: ‘strcat’ not declared
Error: ‘F’ not declared
Write the names of the correct header files, which must be included to compile the
code successfully:
void main()
{
ofstream F(“WELCOME.TXT”);
char Word1[]="WELCOME";
char Word2[]="C++";
char Word[40];
strcpy(Word, Word1);
strcat(Word, Word2);
F<<Word<<end1;
}
c) Rewrite the following C++ code after removing any/all syntactical errors with each
correction underlined. 2m
Note : Assume all required header files are already included in the program.
Typedef Count int;
void main()
{ Count C;
cout<<"Enter the count:";
cin>>C;
for (K = 1; K<=C; K++)
cout<< C "*" K <<endl;
}
d) Find and write the output of the following C++ program code : 3m
Note : Assume all required header files are already included in the program.
void Revert(int &Num, int Last=2)
{
Last=(Last%2==0)?Last+1:Last-1;
for(int C=1; C<=Last; C++)
Num+=C;
}
void main()
{
int A=20,B=4;
Revert(A,B);
cout<<A<<"&"<<B<<endl;
B--;
Revert(A,B);
cout<<A<<"#"<<B<<endl;
Revert(B);
cout<<A<<"#"<<B<<endl;
}
e) Find and write the output of the following C++ program code : 2m
Note : Assume all required header files are already included in the program.
#define Modify(N) N*3+10
void main()
{
int LIST[]={10,15,12,17};
int *P=LIST, C;
for(C=3; C>=0; C--)
LIST[I]=Modify(LIST[I]);
for (C=0; C<=3; C++)
{
cout<<*P<<":";
P++;
}
}
f) Observe the following program and find out, which output(s) out of (i) to(iv) will be
expected from the program? What will be the minimum and the maximum value
assigned to the variable Alter? 2m
Note: Assume all required header files are already being included in the program.
void main( )
{
randomize();
int Ar[]={10,7}, N;
int Alter=random(2) + 10 ;
for (int C=0;C<2;C++)
{
N=random(2) ;
cout<<Ar[N] +Alter<<”#”;
}
}
(i) 21#20# (ii) 20#18# (iii) 20#17# (iv) 21#17#
2.
a) What do you understand by Function overloading or Functional polymorphism? Explain
with suitable example. 2m
b) Answer the questions (i) and (ii) after going through the following class:
class planet 2m
{
char name[20];char distance[20];
public:
planet() //Function 1
{
strcpy(name, "Venus");
strcpy(distance,"38 million km");
}
void display(char na[],char d[]) //Function 2
{
cout<<na<<"has "<<d<<" distance from Earth"<<endl;
}
planet(char na[], char d[]) //Function 3
{
strcpy(name,na);
strcpy(distance,d);
}
~planet() //Function 4
{
cout<<"Planetarium time over!!!"<<endl;
}
};
I. What is Function 1 referred as? When will it be executed?
II. Write suitable C++ statement to invoke Function 2.
4.
(a) Find the output of the following C++ code considering that the binaryfile sp.dat already
exists on the hard disk with 2 records in it. 1m
class sports
{
int id;
char sname[20];
char coach[20];
public:
void entry();
void show();
void writing();
void reading();
}s;
void sports::reading()
{
ifstream i;
i.open("sp.dat");
while(1)
{
i.read((char*)&s,sizeof(s));
if(i.eof())
break;
else
cout<<"\n"<<i.tellg();
}
i.close();
}
void main()
{
s.reading();
}
(b) Write a user defined function wordcount() in C++ to count how many words are present
in a text file named “opinion.txt”. 2m
For example, if the file opinion.txt contains following text:
Co-education system is necessary for a balanced society. With
co-education system, Girls and Boys may develop a feeling of
mutual respect towards each other.
The function should display the following:
Total number of words present in the text file are: 24
(c) Write a function display () in C++ to display all the students who have got a
distinction(scored percentage more than or equal to 75) from a binary file “stud.dat”,
assuming the binary file is containing the objects of the following class: 3m
class student
{
int rno;
char sname [20];
int percent;
public:
int retpercent()
{
return percent;
}
void getdetails()
{
cin>>rno;
gets(sname);
cin>>percent;
}
void showdetails()
{
cout<<rno;
puts(sname);
cout<<percent;
}
};