Practice Midterm Solutions

advertisement
CSIS 10A
MIDTERM EXAM
Name: SOLNS
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) ____0____
c)
___10____
e)
____4____
8 / 10
8 + 4 / 2
b)
___8_____
d)
___8_____
16 / 4 * 2
5 % 3 +
6
25 % 7
2. Identify the following names as valid or invalid identifiers in C++:
a)
c)
e)
jack_pot _____v________
tax rate _____i_________
SALARY ________v________
b)
total$money ____i________
d)
2ndYear _____i_________
3. Convert the following numbers to ordinary decimal notation
a)
c)
-4.75E+03 __-4750.0_____
–7.3342E-04 __-.00073342
b)
d)
8.6026E-03
2.0e+05
__.0086026___
__200000.0__
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;
variable undeclared...use yds
feat = yds * 3;
cout<< yds << "yards = " << feat << " feet. ";
system("pause");
return 0;
}
5 .Give the output of the following program fragment:
OUTPUT k = -2 m = 16
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? Alright!
b) How about when the value of x is 17 ? __Hi!___
c) How about when the value of x is 2 ? ___Low!_____
d) How about when the value of x is 8 ? __(no ouput)__
7. Give the output of the following program fragment
OUTPUT
for(int k = 40; k > 10; k = k - 10)
{
cout << k << " ";
}
cout << "Done"<<endl;
40 30 20 Done
8. What will be the output for the following code segment when
a) x=5, y=5
____5 10________
b) x=4, y=6
____2 6________
c) x=1, y=8
____1 10________
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;
ATLAS
ATLASKING
ATLASATLASKING
KING
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 ( x >=100 && x <=200)
b) x is above 100 or below 0
(x > 100 || x < 0)
c) x is greater than 0 but it is not 5 (x > 0 && x!=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:
__int_____ calcSize(_char______ size)
{
if ( __size==’S’_ )
_26______ ;
if ( _size==’M’__ )
return __30__ ;
if ( _size==’L’_ )
return __34___ ;
return
else
else
else
return
__0_
;
}
12. (10 Points) Write a function getMin, which is passed 3 integer arguments and returns the smallest
of the 3 arguments. For example in the following example function calls
cout<<getMin(5, 1, 3)
will display 1
cout<<getMin(6, 4, 3)
will display 3
int getMin(int a, int b, int c)
{
if (a<=b && a<=c)
return a;
else if (b <= a && b <= c)
return b;
else
return c;
}
13. (10 Points) Give a trace and output for the following fragment if the input is:
11 6 15
x
0
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;
}
y
y>10
11
OUTPUT
Enter a number: 11
T
11
11 11
Enter a number: 6
6
F
5
5
6
Enter a number: 15
15
T
20
20 15
14. (10 Points) Show what is printed by the following program segment. (remember: integer arithmetic)
a
z
z>=a
OUTPUT
int a = 2, z = 16;
while (z >= a)
{
z -= a;
a += z / 2;
cout << a << z;
}
2
9
11
16
14
5
T
T
9
11
14
5
15. (10 Points) Write a program that asks the user to input a list of 5 numbers (integers). The program
will count the number of values that were over 100. For example,
Sample execution
(computer output bold, user input italics)
Your Program Here (assume all libraries are included)
int
{
main()
int count=0, num;
cout<<"Enter five numbers:"<<endl;
for (int i=0; i<5; i++)
{
cin>>num;
if (num>100)
count=count+1;
}
cout<<"There were "<<count<<
" numbers over 100"<<endl;
system(“pause”);
return 0;
Enter 5 numbers:
500 50 90 150 175 -999
There were 3 numbers over 100
}
Download