UMBC CMSC201, Computer Science I, Fall 1994 Section 0101, 0102 and Honors

Final Exam, Short Answer Section


Short Answers, 4 points each

This section cannot be graded on-line. To practice taking the exam, jot down your answer and click on the button after each question to see a sample solution.

  1. This question tests your knowledge of global variables and lexical scoping. What is the output of the following program?
    #include <stdio.h>
      
    /* Global Variables */
    int w = 100, x = 101, y = 102, z = 103 ;
    
    int towers(int) ;
      
    main() {         
      int w = 1 , x = 3 ;
    
      printf("before: w = %d, x = %d, y = %d, z = %d\n", w, x, y, z) ;
      w = towers(y) ; 
      printf("after: w = %d, x = %d, y = %d, z = %d\n", w, x, y, z) ;
    }
    
    int towers(int x) {
      int y ;
    
      y = x + 5 ;
      z = z + y ;
      w = z - x ; 
      printf("during: w = %d, x = %d, y = %d, z = %d\n", w, x, y, z) ;
      return(y) ;
    }
    

    Click here for the solution.

  2. This question tests your knowledge of nested for loops. Please put carriage returns in the proper places. What is the output of the following code?
          int i, j;
          
          for (i = 0; i <= 3; i++) {
             for (j = 1; j <= 4; j++) {
                if ( (i + 1 == j) || (j + i == 4) ) {
                   printf("+") ;
                } else {
                   printf("o") ;
                }
             }
             printf("\n") ;
          }
    

    Click here for the solution.

  3. This question tests your knowledge of pointers and parameter passing by reference. What is the output of the following program?
    #include <stdio.h>
    
    int ents(int *, int *) ;
    
    main() {
      int a = 1, b = 2, c = 3 ;
    
      a = ents(&b, &c) ;
      printf("First Call: a = %d, b = %d, c = %d\n", a, b, c) ; 
           
      c = ents(&a, &b) ;
      printf("Second Call: a = %d, b = %d, c = %d\n", a, b, c) ;
    }          
    
    int ents(int *p1, int *p2) {
      int result, *q ;
    
      q = p1 ;
      *p1 = *p2 * 2 ;
      p1 = p2 ;
      *p1 = *q + 5 ;
      result = *p1 + *p2 ;
      return(result) ;
    }
    

    Click here for the solution.

  4. This question tests your ability to work with characters and strings. What is the output of the following program?
    #include <stdio.h>
    #include "genlib.h"
    #include "simpio.h"
    #include "strlib.h"
    
    main() {
       string str, new ;
       int i, length ;
       char c ;
    
       str = "Smaug the Worm" ;
       new = "" ;
       length = StringLength(str) ;
       for(i = 0 ; i < length ; i++) {
          c = IthChar(str,i) ;
          if (isalpha(c)) {
             c = tolower(c) ;
             c = c + 7 ;
             if (c > 'z') c = c - 26 ; 
          }
          new = Concat(new, CharToString(c)) ;
       }
       printf("new = %s\n", new) ;
    }
    

    Click here for the solution.

  5. Write a simple loop which prints out the following sequence of numbers:
          18 23 28 33 38 43 48
    

    Click here for a sample solution.

  6. Write a function Second which takes two parameters, an integer array A and an integer variable size. You may assume that when the function Second is called, the value of size is then number of elements in the array A. The function Second should return the value of the second smallest element of the array A. For example, if the array A contains {17, 2, 37, 5, 13, 12}, the function should return 5.

    Click here for a sample solution.

  7. Write a function LastVowel which takes a string and returns the position of the last vowel ('a', 'e', 'i', 'o' or 'u') in the string. For example, LastVowel("Riders of Rohan") should return 13.

    Click here for a sample solution.


Last Modified: Wed Jan 4 14:25:59 EST 1995

Richard Chang, chang@gl.umbc.edu