CS35: Quiz 1

CS35: Programming and Problem Solving
Ray Ontko
Department of Computer Science
Earlham College

This quiz is meant to give you an idea of how well you understand the material covered in the first third of the course. Your performance on this quiz does not affect your grade in the course; it is meant only to provide feedback to you about how you're doing.


    Use the following code segment to answer questions 1 through 4.
    main()
    {
      int m , months , years ;
      float payment , rate , mrate , balance = 0.0 ;
    
      if( scanf( "%f %f %d" , &payment , &rate , &years ) == 3 )
      {
        months = years * 12 ;
        mrate = 1.0 + ( rate / 100.0 ) / 12.0 ;
      
        for( m = 0 ; m < months ; )
        { 
          balance += payment ;
          balance *= mrate ;
          m ++ ;
          printf( "%d %.2f\n" , m , balance ) ;
        }
      }
      else
        printf("Error reading input.\n") ;
    }
    
  1. Which of the following values will never be assigned to the variable months by the program?
    1. 6
    2. 72
    3. 36
    4. 88

  2. Suppose "500 12 10" was entered as input for the program. What is the value assigned by the program to the variable mrate?
    1. 101.0
    2. 1.01
    3. 1.166667
    4. 1.0

  3. Suppose "100 7.5 5" was entered as input for the program. What is the final value of the variable m?
    1. 1200
    2. 144
    3. 60
    4. 59

  4. Which of the following lines would never be printed by the program (for any inputs)?
    1. 60 1000.00
    2. 105.3 1023.00
    3. 67 1,734,704.23
    4. 360 187000.00


    Use the following code segment to answer questions 5 through 8.
    main()
    {
      int c ; 
      int cc = 0 , wc = 0 , lc = 0 ;
      int state = 0 ;
      float awl , all ;
    
      while ( ( c = getchar() ) != EOF )
      {
        cc ++ ;
        switch ( state )
        {
          case 0:
            if ( isalpha(c) )
            {
              state = 1 ;
              wc ++ ;
            }
            break ;
          case 1:
            if ( ! isalpha(c) )
              state = 0 ;
            break ;
        }
        if ( c == '\n' )
          lc ++ ;
      }
    
      awl = (float)cc / (float)wc ;
      all = (float)cc / (float)lc ;
    
      printf( "%d %d %d %.2f %.2f\n" , lc , wc , cc , all , awl ) ;
    }
    
  5. What is the initial value of the variable c?
    1. negative
    2. 0
    3. positive
    4. unknown

  6. What would happen if the input contained no alphabetic characters?
    1. The program wouldn't compile.
    2. The program would fail in error.
    3. The program would print all zeros.
    4. The second and fifth numbers printed would be zero.

  7. What would happen if the input was the single line "There's a fly in your soup."?
    1. The program would print "1 6 28 28.00 4.67".
    2. The program would print "1 6 27 27.00 4.50".
    3. The program would print "1 7 28 28.00 4.00".
    4. None of the above.

  8. Suggest more descriptive names for each of the following variables:
    1. cc
    2. wc
    3. lc
    4. awl
    5. all


    Use the following code segment to answer questions 9 and 10.
    main()
    {
      int i = 0 , j = 0 ;
    
      scanf( "%d %d" , &i , &j ) ;
      i += 2 ;
      j -= 2 ;
    
      if ( ( i = 7 ) && ( j = 9 ) )
        printf( "yes\n" ) ;
      else
        printf( "no\n" ) ;
    
      printf( "%d %d\n" , i , j ) ;
    }
    
  9. Under what circumstances would the program print "yes"?
    1. Always.
    2. Never.
    3. If the first number entered was 5 or the second number entered was 11.
    4. If the first number entered was 5 and the second number entered was 11.

  10. Suppose you entered "9 7" as the input for the program. What would the final line of output from the program look like?
    1. 11 5
    2. 7 9
    3. 9 7
    4. 5 11


Solutions:

  1. a, d.
  2. b.
  3. c.
  4. b, c.
  5. d.
  6. b.
  7. c.
  8. char_count, word_count, line_count, average_word_length, average_line_length
  9. a. (Note "=" instead of "==" in if.)
  10. b. (Note "=" instead of "==" in if.)

Copyright © 1999, Ray Ontko (rayo@ontko.com).