Answer Key Practice Exam 2 Spring 2004

advertisement
CS230 Spring 2004
PRACTICE Exam 2
ANSWER KEY_________
No Calculators allowed!
Multiple Choice !
1. Given that alpha is an integer variable. alpha++ is the same as which of the following:
a.
b.
c.
d.
alpha = alpha + 1;
alpha = alpha;
alpha = beta + alpha;
alpha = alpha % beta;
2. Given that x is a float variable containing the value 84.7 and num is an int variable, what will
num contain after execution of the following statement: num = x + 2;
a.
86.7
b. 86
c. 87
d. 87.0
e.compilation error
3. The value of the C++ expression 8*10-11 + 22 % 4*2; is:
a.
6
b. 83
c. 74
d. 73
e. none of the these
4. Given that x is a float variable and num is an int variable containing the value 38, what will x
contain after execution of the following statement:: x = num / 4 + 3.0;
a.
12.5
b. 13.0
c. 11.0
d. 12.0
e.compilation error
5. What is the output of the following program fragment?
age = 19;
cout << "Are you" << age << "years old?" << endl;
a.
b.
c.
e.
Are you19years old?
Are you 19 years old?
Are you19 years old?
none of the above
d.
e.
Are you 19years old?
Are
you
age
years
6. Given the two lines of input data and the input statement for the integer variables.
17 13 7 3 24 6
cin >> int1 >> int2 >> int3;
What is the value of each variable after the statement is executed?
a.
1 7 1
b. 17 1 3
c. 17 13 7
d. none of these
old?
7. Given the following C++ code what will happen on execution if the data file is missing?
if (!dataIn.eof())
{
cout<<”ERROR! PROBLEM WITH DATA FILE”;
}
a. program will stop executing without displaying an error message
b. program will display error message and continue to execute
c. program will display error message and stop executing
d. none of the above
Answer 8, 9 and 10 using the following declarations:
float num; int ans;
8. What is the result of num = 5/2; ?
a. 2.5
b. 2.0
c. 2
d. none of the above
9. What is the result of num = float(7)/float(4); ?
a. 1.75
b. 1.0
c. 1.8
d. 2
10. What is the result of ans = 4/6;
a. 0.8
b. 8
c. 0
d. none of the above
II. Short answer
11. What does the following output? Put your answers(there will be 4 answers)in the box.
string school;
string phrase;
string strAns;
string fullName;
int position;
school = “Jacksonville State”;
phrase = “yes and no”;
cout << school.length( ) << endl;
fullName = school + “ University”;
cout << fullName.length( ) << endl;
position = phrase.find(“and”);
cout << position << endl;
strAns = school.substr(4, 3);
cout << strAns;
 - is a space
18
29
4
son
12. Inside a loop, how would you increment a counter?
count++; or count = count + 1;
13. Inside a loop, how would you accumulate a set of grades?
sum = sum + grade;
14. Convert the following decimal number to binary.
232
111010002
15. Convert the following decimal number to octal.
189
2758
16. Convert the following decimal number to hexadecimal.
27
1B16
17. Convert the following from hexadecimal to decimal –
a. 13C
b. 25A
a. 31610
b. 60210
18. Add the following numbers –
a. 11110110 + 1001101
b. 110101 + 11111
a. 101000011
b. 1010100
19. Subtract the following number
a. 11101000 – 101111
b. 1000 - 110
a. 10111001
b. 10
20. What is the output of this loop?
There is more than one number in the output.
Number = 1;
while(Number < 6)
{
Number ++;
cout<<number<<endl;
}
2
3
4
5
6
21. Assume 0 is input for n.
What is the output for the lines of code?
cin>>n;
1
i = 1;
do
{
cout <<i;
i++;
}while (i <= n);
22. Assume 5 in input for n;
What is the output for the lines of code?
cin>>n;
for (int i = 1; i <= n; i++)
{
cout<<i<<’ ‘;
}
12345
III Write an algorithm for the following c++ program.
Problem Description: This program will ask the user to enter their first and last name and 2
bowling scores. Add the bowling scores together and divide by 2.0 to get the average. Output
the name and the average to the screen.
INPUT – user to enter first and last name and 2 bowling scores
OUTPUT
- name and average on the screen
RELATIONSHIP - average = (score1 + score2) /2.0
Analysis:
Level 1:
1. get first name, last name and 2 bowling scores
2. average = (score1 + score2)/2.0;
3. output first name, last name and average to screen
Level 2:
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
ask for first name
get first name
ask for last name
get last name
ask for bowling score 1
get bowling score 1
ask for bowling score 2
get bowling score 2
2.1 average = (score1 + score2)/2.0 ;
3.1 display firstname;
3.2 display lastname;
3.3 display average
V Programming portion of exam
Write a program (no comments required) that uses a while loop to read a lastname and a bowling
score from an external datafile called bowlingscores.txt. Inside the loop accumulate the scores and
increment a counter; put only the names to an external data file called bowlingteam.txt. Outside
the loop calculate the average. Check the average. When the average is > 80% display on the
screen the following: “The team is going to the bowling tournament”.
For the external files use the source code names dataIn and dataOut . The other variables may be
variables of your choice. Be sure to close the files.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ofstream dataOut;
ifstream dataIn;
int main()
{
dataOut.open(“bowlingteam.txt”);
dataIn.open(“bowlingscores.txt”);
string lastname;
int score = 0;
int count = 0;
int accumulate = 0;
float avg = 0.0;
if (!dataIn)
{
cout<<”ERROR: Program will terminate”;
return 1;
}
while (!dataIn.eof())
{
dataIn>>lastname>>score;
dataOut<<lastname<<endl;
count++;
accumulate = accumulate + score;
}
avg = float (accumulate) / float (score);
if (avg > .80)
{
cout<< “The team is going to the bowling tournament”;
}
dataIn.close();
dataOut.close();
return 0;
}
Download