Practice Midterm.doc

advertisement
CSIS 10A
MIDTERM EXAM
Name:
Worth 100 Points Open-book  Open-note No Computer
Put down your answers on paper in an orderly and recognizable fashion.
1. Give the numerical value of each of the following C++ expressions (hint: they are all integers)
a) ________
8 / 10
b)
________
c)
_______
8 + 4 / 2
d)
________
e)
________
25 % 7
16 / 4 * 2
5 % 3 +
6
2. Identify the following names as valid or invalid identifiers in C++:
a)
c)
e)
jack_pot _____________
tax rate ______________
SALARY ________________
b)
d)
total$money ____________
2ndYear ______________
3. Convert the following numbers to ordinary decimal notation
a)
c)
-4.75E+03
___________
–7.3342E-04 ___________
b)
d)
8.6026E-03
2.0e+05
___________
___________
4. Exactly ONE line in the following program has a C++ syntax error. Circle the error in that line, and explain
what the error is and how to correct it:
#include<iostream>
using namespace std;
int main()
{
int yds, feat;
cout << "Enter number of yards: ";
cin >> yards;
feat = yds * 3;
cout<< yds << "yards = " << feat << " feet. ";
system("pause");
return 0;
}
5 .Give the output of the following program fragment:
int k=6; m = 12;
k = k + m;
m = k - 2;
k = m - k;
cout << "k = " << k << " m = " << m << endl;
6. Consider the following statement:
if
(x>15 )
if ( x > 20)
cout << “Alright!”;
else
cout << “Hi!”;
else
if (x<5)
cout<<"Low!";
a) What would be the output from the above statement when the value of x is 22?________
b) How about when the value of x is 17 ? ________
c) How about when the value of x is 2 ? ________
d) How about when the value of x is 8 ? ________
7. Give the output of the following program fragment
OUTPUT
for(int k = 40; k > 10; k = k - 10)
{
cout << k << " ";
}
cout << "Done"<<endl;
8. What will be the output for the following code segment when
a) x=5, y=5
____________
b) x=4, y=6
____________
c) x=1, y=8
____________
if (x == y ||
y = 10;
else
x = 2;
cout
<< x << "
y > 6)
" << y << endl;
9. What are the outputs of this code?
OUTPUT
string s1 = “Atlas”, s2 = “King”, s3;
s3 = s1 + s2;
cout << s1 << endl;
cout << s3 << endl;
s1 += s3 ;
cout << s1 << endl;
cout << s2 << endl;
10. Convert the following English descriptive statements to a LEGAL C++ conditional expression that
gives true when the variable satisfies the description. For example, the statement "a is over 100"
translates to C++ as (a > 100)
a) x is between 100 and 200 inclusive _____________________
b) x is above 100 or below 0 ___________________
c) x is greater than 0 but it is not 5 _____________
11. (10 Points) Define a function named calcSize; the function is passed a character argument
representing a size code and the function returns the size in inches (an integer) according to the
following chart and sample function calls:
Code
Size in Inches
'S'
26
cout<<calcSize('S')
will display 26
'M'
30
cout<<calcSize('M')
will display 30
'L'
34
any other
0
Use the following schematic function definition, filling in the missing parts:
_______ calcSize(_______ size)
{
if ( _______ )
_______
if ( _______ )
return _______
if ( _______ )
return _______
;
_______
;
return
else
else
;
;
else
return
}
12. (10 Points) Write a function getMin, which is passed 3 integer arguments and returns the smallest
of the 3 arguments. For example the following example function calls
y = getMin(5, 1, 3);
will store 1 in y
y = getMin(6, 4, 3);
will store 3 in y
13. (10 Points) Give a trace and output for the following fragment if the input is:
11 6 15
x
y
y>10
OUTPUT
x = 0;
for(i=1; i<4; i++)
{
cout << " Enter a number: ";
cin >> y;
if ( y > 10 )
x = x + y;
else
x = x – y;
cout << x << " " << y << endl;
}
14. (10 Points) Show what is printed by the following program segment. (remember: integer arithmetic)
OUTPUT
int a = 2, z = 16;
while (z >= a)
{
z -= a;
a += z / 2;
cout << a << z;
}
15. (10 Points) Write a program that asks the user to input a list of five numbers (integers). The
program will count how many of the input numbers were over 100. For example, see the following
sample execution:
Sample execution
(computer output bold, user input italics)
Your Program Here (assume all libraries are included)
int
{
Enter five numbers:
500 50 90 150 175
There were 3 numbers over 100
}
main()
Download