Professional Documents
Culture Documents
Exercise # 1
Questions
1) Pascal, BASIC and C are procedural languages, while C++ is object oriented language.
a) A member function.
b) A class.
c) An operator.
d) A data item.
3) Two major components of an object are data and functions that operate on that data.
a) A member function.
b) An operator.
c) A class function.
d) A method.
6) Which of the following are good reasons to use an object oriented language?
7) Objects model entities in the real world more closely than do functions.
8) True or False: A C++ program is similar to a C program expect for the details of coding.
10) When a language has the capability to produce new data types , it is said to be
a) Reprehensible.
b) Encapsulated.
c) Overloaded.
d) Extensible.
11) True or False: You can easily tell from any two lines of code, whether a program is in C++
or in C.
12) The ability of a function or operator to act in different ways on different data types is called
polymorphism.
13) A normal C++ operator that acts in special ways on newly defined data types is said to be,
a) Glorified.
b) Encapsulated.
c) Classified.
d) Overloaded.
a) Critically important.
b) Something you can return to later.
c) The key to wealth and success.
d) Completely irrelevant.
Robert Lafore
Exercise # 2
Questions
1) Dividing a program into functions
7) An expression
a) Type int = 4
b) Type float = 4
c) Type long double = 10
d) Type long = 4
9) True or False: A variable of type char can hold the value 301.
a) 12 = integer constant.
b) ‘a’ character constant.
c) Junglejim = variable name or identifier.
d) Junglejim ( ) = function name.
11) Write a statement that display on the screen,
12) True or False: in an assignment statement, the value on the left of the equal sign is
always equal to the value on the right side.
13) Write a statement that displays the variable George in a field ten characters wide.
Ans. Cout<<setw(10)<<George;
14) What header file must you #include with your source file to use cout and cin?
Ans. <iostream.h>
15) Write a statement that gets a numerical value from the key board and plaaces it in
the variable temp.
Ans. Cin>>temp;
16) What header file must you #include with your program to use setw?
Ans. <iomanip.h>.
17) Two exceptions to the rule that the compiler ignores whitespace are the string constant
and preprocessor directives.
18) True or false: it’s perfectly all right to use variables of different data types in the
same arithmetic expression.
20) An arithmetic assignment operator combines the effect of what two operators?
21) Write a statement that uses an arithmetic assignment operator to increase the value of the
variable temp by 23. Write the same statement without the arithmetic assignment
operator.
Ans. With arithmetic assignment operator temp + = 23;
Without arithmetic assignment operator temp = temp + 23;
22) The increment operator increases the value of a variable by how much?
Ans. It increases by 1.
23) Assuming var1 starts with the value of 20. What will be the following code print
out? cout<<var1--;
cout<<++var1;
Ans. 20 & 20 each time.
24) In the example we have seen so far header files have been used for what purpose?
Ans. Header files are used for declaration, data for library, objects and over loaded
operators.
25) The actual code for library functions is contained in a library file.
Exercises
Q 1 Answer)
#include<constream.h>
void main()
{
clrscr();
long double g; //g stands for gallons
cout<<"please enter the number of gallons\n\t";
cin>>g;
cout<<"\nThe cube feet are\t\a"<<g*0.134;
getch();
}
Q 2 Answer)
#include<constream.h>
void main()
{
clrscr();
cout<<"\n1990\t135\n1991\t7290\n1992\t11300\n1993\t16200";
getch();
}
Q 3 Answer)
#include<constream.h>
void main()
{
clrscr();
int a=10,b,c;
b=a*2;
c=b-1;
cout<<a<<"\n"<<b<<"\n"<<c;
getch(); }
Q 4 Answer)
#include<constream.h>
void main()
{
clrscr();
cout<<"\n\tMy Favorite Poem\n\nTwinke twinkle little star,\n\n\thow i wonder what
you are.\n\nup above up so high,\n\n\tLike a diamond in the sky.\n\nTwinke twinkle little star,\
n\n\tHow i wonder what you are.";
getch();
}
Q 5 Answer)
#include<constream.h>
#include<ctype.h>
void main()
{
clrscr();
char a;
cout<<" enter a lower case letter\n\t";
cin>>a;
cout<<"\n\t"<<islower(a);
getch();
}
Q 6 Answer)
#include<constream.h>
void main()
{
clrscr();
long double d,p,f,g,y;//d for dollars, p for pounds, f for franc, g for germen and y for yen.
p=d*0.672;
cout<<"\n\tYour amount in pounds is = "<<p<<" Pounds";
f=d*5.814;
cout<<"\n\tYour amount in francs is = "<<f<<" Francs";
g=d*1.172;
cout<<"\n\tYour amount in Germen Duetschemark is = "<<g<<" Duetschemark";
y=d*104.712;
cout<<"\n\tYour amount in japani yens is = "<<y<< "Yens";
getch();
}
Q 7 Answer)
#include<constream.h>
void main()
clrscr();
long double c;
cout<<"\n\tEnter temperature in celsius\t";
cin>>c;
Q 8 Answer)
#include<constream.h>
#include<iomanip.h>
void main()
{
clrscr();
cout<<"portcity"<<setw(18)<<setfill(3)<<"2425785";
getch();
}
Q 9 Answer)
#include<constream.h>
void main()
{
clrscr();
long double a,b,c,d;
cout<<a<<"/"<<b<<"+"<<c<<"/"<<d<<" = "<<(a*d+b*c)/(b*d);
getch();
}
Q 10 Answer)
#include<constream.h>
void main()
{
clrscr();
int pound,shilling,pence; cout<<"\
nenter pounds : "; cin>>pound;
getch();
Q 11 Answer)
#include<constream.h>
#include<iomanip.h>
void main()
{
clrscr();
Q 12 Answer)
#include<constream.h>
void main()
{
clrscr();
float decpounds,decfrac;
int pounds,x,y;
cout<<x;
getch();
}
Robert Lafore
Exercise # 3
Exercises
Q 1 Answer)
#include<constream.h>
void main()
{
clrscr();
long double x;
int y;
cout<<" enter number : ";
cin>>x;
cout<<endl;
for(y=1;y<=30;y++)
{
cout<<x*y<<"\t"; if(y
%10==0)
cout<<endl;
}
getch();
}
Q 2 Answer)
#include<constream.h>
void main()
{
clrscr();
long double f,c;
int x;
cout<<"Type\n\n 1 to convert temperature from Fahrenheit to Celsius\n\n 2 to
convert temperature from Celsius to Fahrenheit\n\t";
cin>>x;
if(x==1)
{
cout<<"\nEnter temperature in Fahernheit : ";
cin>>f;
cout<<"\n In Celsius it is : "<<5*(f-32)/9<<" Degrees";
}
if(x==2)
{
cout<<"\nEnter temperature in Celsius : ";
cin>>c;
cout<<"\n In Fahrenheit it is : "<<(9*c/5)+32<<" Degrees";
}
getch();
}
Q 3 Answer)
#include<constream.h>
int main()
{
clrscr();
char ch;
unsigned long total=0;
cout<<"Enter six digits";
while((ch=getch())!='\r')
{total=total*10+ch-'0';} cout<<"\
nNumber is : "<<total<<endl; return
0;
}
Q 4 Answer)
#include<constream.h>
int main()
{
clrscr();
long double x,y,s;
char op,ch;
do{
cout<<"Enter ist digit ,peration & 2nd digit\n\t";
cin>>x>>op>>y;
switch(op)
{
case'*' : s=x*y;
break;
case'/' : s=x/y;
break;
case'+' : s=x+y;
break;
case'-' : s=x-y;
break;
}
cout<<"\n\tAnswer is : "<<s;
cout<<"\n\ndo you want to continue enter (Y for yes and N for no)";
cin>>ch;
}
while
(ch != 'n');
return 0;
}
Q 5 Answer)
#include<constream.h>
void main()
{
int rows;
int x;
int space;
Q 6 Answer)
Could not solve it…..
Q 7 Answer)
#include<constream.h>
void main()
{
clrscr(); long double w,x,y,z;
cout<<"\nEnter amount\t";
cin>>x;
cout<<"\nEnter %age\t";
cin>>y;
cout<<"\nEnter no. of years\t";
cin>>z;
y=y/100;
for(w=1;w<=z;w++)
{
x=x+(x*y);
}
cout<<"At the end of"<<z<<" years your amonut is : "<<x<<" Dollras";
getch();
}
Q 8 Answer)
#include<constream.h>
void main()
{
clrscr(); int pnds,sh,pnc,pnds2,sh2,pnc2,x,y,z;
char ch;
do{
cout<<"enter first amount:\npounds : ";
cin>>pnds;
cout<<"\nshillings : ";
cin>>sh; cout<<"\
npence : "; cin>>pnc;
x=pnds+pnds2;
y=sh+sh2;
z=pnc+pnc2;
if(z>11)
y++;
z--;
if(y>19) x+
+;
cout<<"\nyour ans is : \x9c\n"<<x<<"."<<y<<"."<<z;
cout<<"continue? (Y or N)\n";
cin>>ch;
} while(ch!='n'); }
Q 9 Answer)
#include<constream.h>
void main()
{
clrscr();
int w,x,y,z;
cout<<"Enter the number of chairs : ";
cin>>x;
cout<<"\nEnter the no. of guests : ";
cin>>y;
for(z=y;z>(y-x);z--)
{
w=z*(y-1);
}
cout<<"\npossible combinations are : "<<w;
getch();
}
Q 10 Answer)
#include<constream.h>
void main()
{
clrscr();
int x=2;
long double i,f,p,p1;
cout<<"Enter final amount : ";
cin>>f;
cout<<"\nEnter %age of interest : ";
cin>>p;
cout<<"\nEnter initial amount : ";
cin>>i;
p1=p/100;
while(i<=f)
{
i=i+(f*p1);
x++;
}
cout<<"\n it took "<<x<<" years at interest of "<<p<<" %";
getch(); }
Q 11 Answer)
#include<constream.h>
void main()
{
clrscr();
int pnds, sh, pnc,pnds1,sh1,pnc1,x,y,z;
char op;
if(z>11) y+
{ +; z=z-12; }
if(y>19)
{ x++; y=y-20; }
cout<<"\nNow amountis\x9c"<<x<<"."<<y<<"."<<z;
getch();
}
Q 12 Answer)
#include<constream.h>
void main()
{
clrscr();
long double x,y,a,b,c,d;
char op;
switch(op)
{
case'+' : x=(a*d+b*c);y=(b*d);
break;
case'-' : x=(a*d-b*c);y=(b*d);break;
case'*' :x=a*c;y=b*d;break;
case'/' :x=a*d;y=b*c;break;
}