Test 2 CSCI 130 Summer 2011


  1. Selection
    1. [2 points] Give the syntax for the if statement
    2. [2 points] Describe the flow of control for the if statement
    3. [2 points] Why is a code block commonly employed for the body of the if statement?
    4. [4 points] Give a code segment which given the integer variables num and denom prints the decimal equivalent of num/denom if denom is non-zero and error otherwise.
  2. Loops
    1. [2 points] Give the syntax for the while statement.
    2. [2 points] State the flow of control for the while statement.
    3. [2 points] What is an infinite loop?
    4. [4 points] Give a code segment which will print out the even integers between 4 and 23.
  3. For statement
    1. [2 points] Describe the steps that should be taken to avoid an infinite loop.
    2. [2 points] Give the syntax for the for statement.
    3. [2 points] Give the flow of control for the for statement.
  4. [2 points] Describe when a programmer would employ the do ... while statement.
  5. [2 points] Describe the action of the stream extraction operator when reading an integer value.
  6. [10 points] The file numbers.dat contains 10 integers. Write a complete program which will find the sum, average, and maximum value of the integers in the file.
  7. [10 points] The file story.txt contains the text of a story. The story ends with the word END-OF-STORY. Write a program which will determine the number of words with 1-4, 5-8, 9-12, and 13+ letters. Your program should print the following table:
              Word Size      Count     Percent
    	    1-4           100       0.33
    	    5-8           150       0.50
    	    9-12           30       0.10
    	    13 and up      20       0.07