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.');
        }
    }
    

    Answer:

  2. (3 points each)
    1. How many times will the following for loop execute?
            for(index = -1; index < 7; index *= 2); 
      

      Answer: The loop is infinite. No matter how many times we multiply −1 by 2, it will never be greater than 7.

      (NOTE: This is even true for 32-bit integers (i.e., without assuming index can hold arbitrarily large negative numbers). The int value of −1 * 232, −1 * 233, −1 * 234, etc. is 0.)

    2. Given the following declaration:
           int[] myArray = new int[99];
      

      What are the valid subscripts for the array myArray?

      Answer: 0 through 98.

    3. Explain the difference between pass-by-value and pass-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))
      

      Answer: t is undefined. The expression is invalid.

    5. Given: int f = 43, g = 5;

      What will be the output of the following statement ?

            System.out.println((f % g) + (f / g));   
      

      Answer:

      11
      
  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();
            }
        }
    }
    

    Answer:

    2345
    1234
    0123
    -1012
    
  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-- ;
    

    Answer:

    x: 3
    y: 2
    z: 0
    
  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 should always be given as two digits, including leading zero. So the 20th of May, 2009 should be entered as 05202009 and the 3rd of December, 2009 should be entered as 12032009.

    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 */
    

    Answer:

    import javax.swing.JOptionPane;
    
    public class ParseDate
    {
        public static void main (String [] args)
        {
            int date;
            int month, day, year;
    
            String dateAsString = JOptionPane.showInputDialog ("Enter number:");
            date = Integer.parseInt (dateAsString);
            
            month = date / 1000000;
            day = date % 1000000 / 10000;
            year = date % 10000;
            
            switch (month)
            {
                case 1:
                    System.out.print("January ");
                    break;
                case 2:
                    System.out.print("February ");
                    break;
                case 3:
                    System.out.print("March ");
                    break;
                case 4:
                    System.out.print("April");
                    break;
                case 5:
                    System.out.print("May ");
                    break;
                case 6:
                    System.out.print("June ");
                    break;
                case 7:
                    System.out.print("July ");
                    break;
                case 8:
                    System.out.print("August ");
                    break;
                case 9:
                    System.out.print("September ");
                    break;
                case 10:
                    System.out.print("October ");
                    break;
                case 11:
                    System.out.print("November ");
                    break;
                case 12:
                    System.out.print("December ");
                    break;
                default:
                    System.out.print ("Invalid month");
            }            
                
            System.out.println(day + ", " + year + ".");
            
            System.exit(0);
        }
    }
    
  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.

    Answer:

          public static void addThem(int[] x, int[] y, int[] z) {
              int zIndex = 0;
              for( int i = 0; i < x.length; i++ ) 
                  z[zIndex++] = x[i];
              for( int i = 0; i < y.length; i++ ) 
                  z[zIndex++] = y[i];
          }
    

This document was translated from LATEX by HEVEA.