ASSIMMENT OF COMPUTER SCIENCES

advertisement
MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067
www.badhaneducation.in or www.mathematic.in 9810144315
ASSIMMENT OF COMPUTER SCIENCES
Q. 1. Distinguish between an object and a class.
Ans: An object is an identifiable entity with some characteristics and behaviour. A class is a
group of objects that share common properties and relationships.
Q. 2. What is polymorphism? Give an example in C++ to show its implementation in C++.
Ans: Polymorphism is the property by which the same message can be processed in more than
one form. This is of 2 types:
a. Function Overloading: Same function name can be used for more than one purpose.
b. Operator Overloading: Same operator can be used for more than one purpose
Q. 3. How does a pointer variable differ from a simple variable?
Ans: A simple variable stores a data value whereas a pointer variable stores a memory address
of another variable.
Q. 4. Name the header file to be included for the use of following built-in functions:
1. frexp( )
2. tooupper( )
3. getc( )
4. strcat( )
5. setw( )
Ans:
i.
math.h
ii.
ctype.h
iii.
stdio.h
iv.
string.h
v.
iomanip.h
Q. 5. Find the syntax errors:
# include(iostream.h)
void main( )
{
int X, Y;
Email badhan_services@yahoo.in 9810144315
MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067
www.badhaneducation.in or www.mathematic.in 9810144315
cin>>X;
for(Y=0;Y<10,Y++)
if X = = Y
cout<<Y+X;
else
cout<<Y:
}
Errors:
File iostream.h should be in angled brackets < > i.e. #include<iostream.h>
The condition and updation segments should be separated by a semicolon(;) i.e.
For(Y=0;Y<10;Y++)
The condition should be enclosed in braces as shown below: i.e. if(X= = Y)
Statement should be terminated by ; i.e. cout<<Y;
Q. 6. Will the following program execute successfully? If not, state the reason(s):
# include<isotream.h>
void main( )
{
int x, sum = 0;
cin<<n;
for(x=1,x<100,x+=2)
if x%2 = = 0
sum += x;
cout>>”SUM=”>>SUM;
}
Ans: #include<iostream.h>
Wrong operator with cin. It should be cin>>n;
In place of if x%2 = = 0, it should be if(x%2 = = 0)
Wrong operator with cout. It should be cout<<”SUM=”<<SUM;
Q. 7. What will be the output:
int fun(int &x, int y=10)
{
if(x%y = = 0) return ++x; else return y--;
}
void main( )
{
int p=20, q=23;
Email badhan_services@yahoo.in 9810144315
MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067
www.badhaneducation.in or www.mathematic.in 9810144315
q = fun(p,q);
cout<<p<<” “<<q<<endl;
p = fun(q);
cout<<p<<” “<<q<<endl;
q = fun(p);
cout<<p<<” “<<q<<endl;
}
Ans: Output is:
20 23
10 23
11 11
Q. 8. Write a function in C++ to find the sum of the following series:
1+2+3+4+5+6+7+………. upto n terms.
Ans:
#include<iostream.h>
#include<conio.h>
int sum_series(int n);
void main( )
{
clrscr( );
int t = 0, sum = 0;
cout<<”How many terms?”;
cin>>t;
sum = sum_series(t);
cout<<”\n The sum of the series 1+2+3+4+....upto”<<t<<”is”<<sum;
}
int sum_series(int n)
{
int s = 0;
for(int i =1;i<=n;i++)
s+=i;
return s;
}
Q. 9. Write a C++ function SUMFUN( ) having two parameters X of type double and n of type
integer with a result type as double to find the sum of the series given below:
X + X2/3!+ X3/5!+…….+ Xn/(2n-1)!
Ans:
double SUMFUN(double X, int N)
{
Email badhan_services@yahoo.in 9810144315
MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067
www.badhaneducation.in or www.mathematic.in 9810144315
int i,j;
double sum = 0; fact;
for(i=1;i<=n;++i)
{
fact = 1;
for(j=1;j<=2*i-1;++j)
fact = fact * j;
sum = sum + pow(x,i)/fact;
}
return sum;
}
Chapter 2 – Structures
Q. 10. Find the output of the following programs:
# include<iostream.h>
struct point
{
int X,Y;
};
void show(point p)
{
cout<<p.x<<’:’<<p.y<<endl;
}
void main( )
{
point u = {20, 10}, v, w;
v = u;
v.x += 20;
w = y;
u.y += 10;
u.x += 5;
w.x -= 5;
show(u);
show(v);
show(w);
}
Ans: Output is:
25:20
40:10
35:10
Q .11. Rewrite the corrected code for the following program. Underline each correction.
Email badhan_services@yahoo.in 9810144315
MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067
www.badhaneducation.in or www.mathematic.in 9810144315
#include<iostream.h>
structure club
{
int mem number;
char memname[20];
char memtype[] = “HELLO”;
};
void main( )
{
club p1, p2;
cin<<”Member Number:”;
cin>>memnumber.p1;
cout<<”Member Name:”;
cin>>p1.membername;
p1.memtype = “WORLD”;
p2 = p1;
cin<<”Member Number:”<<p2.memnumber;
cin<<”Member Name:”<<p2.memname;
cin<<”Member Number;”<<p2.memtype;
}
Ans:
#include<iostream.h>
#include<string.h>
struct club
{
int memnumber;
char memname[20];
char memtype[6];
};
void main( )
{
club p1, p2;
cout<<”Member Number:”;
cin>>p1.memnumber;
cout<<”Member Name:”;
cin>>p1.memname;
strcpy(p1.memtype ,“WORLD”);
p2 = p1;
cout<<”Member Number:”<<p2.memnumber;
cout<<”Member Name:”<<p2.memname;
cout<<”Member Type:”<<p2.memtype;
}
Email badhan_services@yahoo.in 9810144315
MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067
www.badhaneducation.in or www.mathematic.in 9810144315
Email badhan_services@yahoo.in 9810144315
Download