V22.0002
Intro. to Computers & Programming
Summer 2009
Practice Final 3
  1. (15 points) The following code contains at least 5 errors (syntactic and logical). Identify them (comments and formatting or lack thereof are not errors).
    public class Count {
        /* Counts the letter frequency in a character
         * input stream. */
        public static count(String input) {
            /* initialize the frequency counts */
            int[] alpha = new int[] ;
            for( int i=0 ; i <= alpha.length ; i++ )
                alpha[i] = 0
    
            for( i=0 ; i < input.length(); i++ ) {
                char ch = input.charAt(i);
                /* Update character counts. */
                if( Character.isLetter( ch ) )
                    alpha[ch-'a']++ ;
                }
    
            /* Print out results. */
            for( int i=0 ; i < alpha.length ; )
                System.out.println("'"+(char)('a'+i)+"': "+alpha[i]) ;
        }
    
        public static void main(String[] args) {
            count('This is a test string for count.');
        }
    }
    
  2. (3 points each)
    1. How many times will the following for loop execute?
            for(index = -1; index < 7; index *= 2); 
      
    2. Given the folling declaration:
           int [] myArray = new int [99];
      

      What are the valid subscripts for array myArray?

    3. Explain the difference between call-by-value and call-by-reference.
    4. Given: int a = 4, b = 6;

      What is the value of the following expression (true/false) ?

           (((t == 5) || (b == 6)) && (a < b))
      
    5. Given: int f = 43, g = 5;

      What will be the output of the following statement ?

            System.out.println((f % g) + (f / g));   
      
  3. (10 points) What does the following code produce?
    public class WhatDoIDo {
        public static void main (String [] args) {
            int i = 0, j = 0;
    
            for ( i = 2; i >= -1; i-- )
            {
                for ( j = 0;  j <= 3; j++ )
                    System.out.print(i + j );
                System.out.println();
            }
        }
    }
    
  4. (10 points) What are the values of x, y and z after the following code executes?
      int x = 3 ;
      int y = x++ ;
      int z = --y / x-- ;
    
  5. (20 points) Write a program that reads a date as an integer in the format MMDDYYYY. The program will then print out the date in the format <Month Name> <day>, <year>.

    Sample run:

       Enter date: 12252003
       December 25, 2003.
    

    In the input the month and the day will always be given as two digits, including leading zero. So the 20th of May, 2009 will be entered as 05202009 and the 3rd of December, 2009 will be entered as 12032009.

    Your program should not crash if the input is ill-formed. Instead, if should re-prompt the user for input until a correctly formed input is received.

    Don’t worry about writing down all of the months, write code that can handle just the first couple and put a comment like:

        /* Remaining months the same way */
    
  6. (25 points) Write a method addThem that takes as parameters three integer arrays x, y, and z. The method should copies all of the elements of x into the first x.length elements of z and all of the elements of y into the next y.length elements of z. You may assume that the length of z is x.length + y.length.

This document was translated from LATEX by HEVEA.