UMBC CMSC201, Computer Science I, Spring 1994 Sections 0101, 0102 and Honors

Final Exam A, 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 a = 2, b = 4, c = 8 ;
      
      int foobar(int b) {
         int a ;
    
         a = b + c ;
         b = 2 * a ;
         c = b + 1 ; ;
         printf("foobar: a = %d, b = %d, c = %d\n", a, b, c) ;
         return(b) ;
      }
      
      main() {         
         a = foobar(c) ;
         printf("main: a = %d, b = %d, c = %d\n", a, b, c) ;
      }
    

    Click here for a sample answer.

  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 <= 2; i++) {
         for (j = 0; j <= 2; j++) { 
    	if ( i * i < j ) 
    	   printf("X") ;
    	else
    	   printf("O") ;
          }
          printf("\n") ;
      }
    

    Click here for a sample answer.

  3. This question tests your knowledge of pointers and parameter passing by reference. What is the output of the following program?
      int f(int x, int *p, int *t) {
      
         x = x + 1 ;
         *p = 2 * *p ;
         *t = 3 * *t  ;
         x = x + *p + *t ;
         return(x) ;
      }
      
      main() {
        int a = 1, b = 2, c = 3 ;
        
        a = f(a, &b, &c) ;
        printf("First Call: a = %d, b = %d, c = %d\n", a, b, c) ; 
    	   
        c = f(a, &b, &b) ;
        printf("Second Call: a = %d, b = %d, c = %d\n", a, b, c) ;
        
      }          
    

    Click here for a sample answer.

  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() {
         int i, start, stop  ;
         string str ;
      
         str = "Our zip code is 21228." ;
         
         for (i = 0 ; str[i] != '\0' ; i++) {
    	if ( str[i] > 'w' ) break ;
         }
         start = i ;
         for (i = start ; str[i] != '\0' ; i++) {
    	if ( str[i] >='0' && str[i] <= '9' ) break ;
         }
         stop = i ;
         printf("%s\n", SubString(str, start, stop) ) ;
      }
    

    Click here for a sample answer.

  5. Write a simple loop which prints out the following sequence of numbers:
          4 8 16 32 64 128
    

    Click here for a sample answer.

  6. Rewrite the following loop without using the break statement. (Your code should be equivalent to the given code.)
      string str ;
      int i ;
    
      i = 0 ;
      str = GetLine() ;
      while ( str[i] != '\0' ) {
         if (str[i] == 'a') break ;
         i = i + 1 ;
      }
      printf ("Answer: %d\n", i) ;
    

    Click here for a sample answer.

  7. Write a function Occurrence which takes two arguments, a string and a character, and returns the number of times the character appears in the string. For example, Occurrence("Senselessness",'s') should return 6.

    Click here for a sample answer.


Last Modified: Mon Sep 4 16:02:29 EDT 1995

Richard Chang, chang@gl.umbc.edu