Test 3 CSCI 130 Fall 2016


  1. If statements
    1. [3 points] Give the syntax for an if statement.
    2. [3 points] Give the preferred use for an if-else statement.
    3. [3 points] Describe the flow of control for an if statement.
  2. [9 points] Loops: Name the three conditions necessary to avoid infinite loops.
  3. While loops
    1. [3 points] Give the syntax for a while loop.
    2. [3 points] Describe the flow of control for a while loop.
  4. Input Files
    1. [2 point] What header file is required to perform input from files?
    2. [2 point] Give code to declare a variable that can be used to read from a file.
    3. [2 points] Give the code to associate the above variable with the file "hello.txt".
  5. [5 points] Write a complete program which opens the file "test.dat" and prints out the contents of the first line of this file.
  6. The integer variable day contains an integer between 0 and 6, and that the string variable dayName contains the name of one of the days of the week. For day 0 corresponds to Sunday and 6 corresponds to Saturday. All values are valid, but the string may be in any case.
    1. [5 points] Write a code fragment that takes a string stored in dayName and converts it to lower case.
    2. [5 points] Write a code fragment which examines the value of dayName and sets the variable day accordingly. You may assume the value is valid, and limit the days to "monday", "tuesday", and "wednesday", but build code that is easily expanded to cover the other days of the week.
    3. [5 points] Write a code fragment which will print out either "weekend" or "weekday" based on the value in day. Saturday and Sunday are weekend days, all other days are weekdays.