Final Exam Form 2 CSCI 130 Fall 2022

Name ______________________________________

  1. Loops
    1. [6 points] Name/describe the three items required for a successful loop.

  2. For loops
    1. [4 points] Give the syntax of a for loop. Explain all of the parts.
    2. [4 points] Describe how a for loop helps a programmer write correct loops.

  3. While Loops
    1. [4 points] Provide the syntax for a while loop.
    2. [4 points] Provide the preferred use of a while loop.
    3. [4 points] Give the semantics of a while loop.

  4. [6 points] Describe the error in the following code. What happens that is incorrect? There are no syntax errors.
    ifstream inFile;
    string word;"ocap.txt");
    while(inFile) {
       inFile >> word;
       cout << word << endl;
  5. [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.

  6. [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.