Final Exam Form 2 CSCI 130
Fall 2022
Name ______________________________________
- Please make sure that your name is on this page.
- Please staple this sheet to your answers, this page on top.
- Please answer each question fully, carefully and thoughtfully.
- No credit will be given for unreadable answers, please write neatly.
- Answer the questions in any order, but please number each answer.
- You may assume all code given is free from syntax errors. If you find a syntax error please ask.
- Loops
- [6 points] Name/describe the three items required for a successful loop.
- For loops
- [4 points] Give the syntax of a for loop. Explain all of the parts.
- [4 points] Describe how a for loop helps a programmer write correct loops.
- While Loops
- [4 points] Provide the syntax for a while loop.
- [4 points] Provide the preferred use of a while loop.
- [4 points] Give the semantics of a while loop.
- [6 points] Describe the error in the following code. What happens that is incorrect? There are no syntax errors.
ifstream inFile;
string word;
inFile.open("ocap.txt");
while(inFile) {
inFile >> word;
cout << word << endl;
}
- [6 points] The variable
phrase
is of type string and contains a valid phrase. Write a code segment that converts the value stored in phrase to all uppercase letters.
- [12 points] The variables
phrase
and word
are both strings and contain valid values. Write a segment of code that will count the number of times the string stored in word
occurs in the string stored in phrase
.