CS35: Quiz 2

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 regarding functions and parameters. 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.
    void oberon( int n , int m ) ;
    
    main()
    {
      int x = 5 ;
      int y = 7 ;
    
      oberon( x , y ) ;
      printf( "%d %d\n" , x , y ) ;
      oberon( x , y ) ;
    }
    
    void oberon( int a , int b )
    {
      int c ;
    
      a *= 2 ;
      b *= 2 ;
      c = a ;
      a = b ;
      b = c ;
    }
    
  1. What value does c have at the end of the first call to oberon?
    1. 5
    2. 7
    3. 10
    4. 14

  2. Which of the following is the correct output from the program?
    1. 5 7
    2. 10 14
    3. 7 5
    4. 14 10

  3. What is the final value of the variable x?
    1. 5
    2. 7
    3. 20
    4. 28

  4. What value does b have at the end of the second call to oberon?
    1. 10
    2. 14
    3. 20
    4. 28


    Use the following code segment to answer questions 5 through 8.
    float titania( float thisbe , float pyramus ) ;
    int puck( float *lysander , float *hermia ) ;
    
    main()
    {
      float x = 1.3 ;
      float y = 3.0 ;
      float z = 9 ;
      float *px ;
      float *py ;
      float *pz ;
    
      px = &x ;
      py = &y ;
      pz = &z ;
    
      z = titania( x , y ) ;
      if ( puck( px , py ) )
        printf( "%f %f %f\n" , x , y , z ) ;
      else
        printf( "%f %f %f\n" , z , y , x ) ;
    
      printf( "%f %f\n" , z , *pz ) ;
    }
    
    float titania( float demetrius , float helena )
    {
      return demetrius * helena ;
    }
    
    int puck( float *lysander , float *hermia )
    {
      int bottom ;
    
      bottom = *lysander ;
      *lysander = *hermia ;
      *hermia = bottom ;
    
      return ( (*lysander) > (*hermia) ) ;
    }
    
  5. What is the final value for the variable z?
    1. 1.3
    2. 3.0
    3. 3.9
    4. 9.0

  6. What is the final value for the variable y?
    1. 1.3
    2. 3.0
    3. 3.9
    4. 9.0

  7. Which of the following is the correct output from the first printf statement executed by the program?
    1. 1.000000 3.000000 3.900000
    2. 1.300000 3.000000 3.900000
    3. 3.000000 1.000000 3.900000
    4. 3.000000 1.300000 3.900000

  8. Which of the following is the correct output from the last printf statement executed by the program?
    1. 3.900000 -2017374568
    2. segmentation fault
    3. 3.900000 3.900000
    4. bus error


Solutions:

  1. c.
  2. a.
  3. a.
  4. a.
  5. c.
  6. None of the above (1.0).
  7. c.
  8. c.

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