CSIS 10A FINAL EXAM NAME:

advertisement
CSIS 10A
FINAL EXAM
100 Points
Remember to bring your organized binders for 10 points on Final
All questions worth 5 points unless otherwise noted.
NAME:
1. What values would be stored in the given variables in each case?
a. int n= 7 / 4;
___________
b. int n=15.0 / 4.0 ;
___________
c. float x=18.0/10.0;
___________
d. float x=4/10 * (30.0-20.0);
___________
2. What is the output of the following C++ statement?
for (x=0; x<100; x+=10)
if (x<30)
cout<<x<<" ";
else if (x>70)
cout<<x<<" ";
3. What is the output of the following C++ statement?
for (x=0; x<100; x+=10)
if ((x>30) && (x<70))
cout<<x<<" ";
else
cout<<(100 + x)<<" ";
4. What would be the output from the following statement?
int y, x = 3;
for(y=0; y<x; y++)
cout << y;
5. What would be the output from the following statement?
int y, x;
for(x=3; x>0; x--)
for(y=0; y<x; y++)
cout << y;
6.Given the following function:
bool f(int myNum)
{
if (myNum>6 && myNum<12)
return true;
else
return false;
}
Which of the following function calls will return a value of true?
a) f(6)
b) f(2)
c)
f(13)
d)
f(8)
1
7. The following statement declares a filestream inFile and tries to open a file "data.dat":
ifstream inFile("data.dat");
Please write a statement that reads 3 numbers from the file into the float variables a, b,
and c.
8. Based on the given values in the four variables listed here, show what values would be
stored in them after sequentially (one after the other) executing the following block of
C++ code.
int a=5, b=2, c=10, d=14;
a-=2;
b*=4;
c=d/3;
d=d%3;
AFTER EXECUTION, a=
b=
c=
d=
9. Given the function definition
int
foo( int x, int y )
{
if (x < y )
return x;
else
return x + y;
}
What would be the output from the following code?
OUTPUT:
cout << foo(3, 8) << endl;
cout << foo(5, foo(3, 8)) << endl;
10. Given the function definition
void Twist( int
{
b = a * 3;
a = a - 3;
}
a, int b )
What would be the output of the following code fragment that invokes Twist?
int s=4, t=6;
Twist(s, t);
cout << s << ' ' << t << endl;
11. Given the following (slightly modified) function definition
void Twist( int& a, int& b )
{ b = a * 3;
a = a - 3;
}
What would NOW be the output of the following code fragment that invokes Twist?
int s=4, t=6;
Twist(s, t);
cout << s << ' ' << t << endl;
2
12. what is the exact output of the following program segment?
for (int k=0; k<8; k++)
switch(k)
{
case 0: case 7: cout<<"R"; break;
case 2: cout<<"I"; break;
case 3: cout<<"N"; break;
case 4: cout<<"D"; break;
default: cout<<"E"; break;
}
13. Suppose you have an array
int data[8]={15, 20, 25};
Draw the array, showing the index and what is stored in each cell
14. Suppose you have an array int a[8]={3,6,0,8,2,0,1,3};
Show what the array looks like after the following C++ statements process it. You
will probably have to trace this.
for (int k = 0; k < 4; k++)
a[k] = a [k+ 4] ;
15. Suppose you have a data type called Appliance, a struct:
struct Appliance
{
string type;
string manufacturer;
float cost;
};
Use the above definition to create a variable
present
and fill it with the type "DVD
Player" made by manufacturer "Sony", and a cost of 59.99.
3
16. (25 points) Suppose a file "cost.dat" contains an indefinite number of Appliance data (type,
manufacturer and cost) as defined in problem 15 above. Write a program that reads the data from
cost.dat and displays (cout) only those items that cost more than $200.00. There are no tabs (\t)
in the file, just read it normally using >>.
Use a variable of type Appliance to hold data from
the file.
Test Plan
example cost.dat file
dishwasher
dishwasher
dishwasher
refrigerator
refrigerator
maytag
kenmore
whirlpool
maytag
whirlpool
Expected Console (cout) OUTPUT
349.99
149.99
199.99
449.99
349.99
dishwasher
refrigerator
refrigerator
maytag
349.99
maytag
449.99
whirlpool 349.99
4
Download