CS 112 Summer 2, 2017 -- Homework Two -- Part A Solutions

 

Part A: Analytical Problems (10 points)

The questions below are designed to reinforce the material exposed to you at lecture and from assigned readings.

  1. What's wrong with the following program? State as many errors as possible that (individually) would prevent the program from compiling without error:
  2. Public class test1 {                     // public should be all lower case
      public static integer f( x ) {         // int instead of integer, x needs a type
        if(x = 5)                            // = is assignment, need == for boolean equals
           x = 1;
      }
      public static void main [String[] argv] {     // braces in method call should be (...)
      {                                             // there is an extra left curly brace in main
        System.println('Hello World!')      // strings have to use double quotes " and missing ; 
      }                                     //     and must be System.out.println
    )                                        // braces around compound statements must be curly {}
     
  3. What would the following program print out?
  4. public class test2 {
    public static void main (String[] argv)
    { System.out.println( (double) (int) 3.1415 );
    int i = 10;
    while (i >= 1)
    System.out.println(i);
    --i;
    }
    } Solution: It will run forever, printing an infinite sequence of 10s, one per line: 3.0 10 10 10 10 10 ...... The problem is that the --i is not part of the loop (it would be in Python but not Java!).
  5. What would the following program print out?
  6. public class test3 {
      
      public static void main (String[] argv) 
      {
        int[] A = { 1, 1, 1, 1 };    // this initializes an array of length 4 with all values being 1
        for(int i = 0; i < 4; ++i) {
          for(int j = i-1; j >=0; --j) {
             A[i] +=  A[j];
          }
        }
        for(int i = 0; i < 4; ++i)
          System.out.println(A[i]); 
      }
    
    }

    Solution:

    1
    2
    4
    8

  7. What would the following program print out?
  8. public class test4 {
      
        public static void main (String[] argv) 
        {
            int[] A = { 1, 2, 3, 4, 5, 6 };    
            for(int i = 0; i < A.length; ++i) {
                if( A[i] % 2 == 0 )
                    continue;
                else if( A[i] > 4 )
                    break;
                else
                    A[i] *= 2;
            }
            for(int i = 0; i < A.length; ++i)
                System.out.println(A[i]); 
        }
    
    }
    
    Solution:
    
    2
    2
    6
    4
    5
    6
    
  9. In the following program, for each name A, B, C, D, E, F, G of a variable or method, state the locations (1 to 9) which are in the scope of the variable. For example, for location A you would write "1 - 9" or "all".
  10. public class ScopeTest {
    
        public static int A = 2;
    
        // location 1
    
        public static void B( int C ) {
             // location 2
        }
    
        public static void main (String[] argv) 
        {
            // location 3
            int D = 2;    
            // location 4 
            for( int E = 0; E < 10; ++E ) {
                // location 5
                if( D < A ) {
                    int F = 1;
                    // location 6
                }
                // location 7
            }
            // location 8
        }
    
    // location 9
    
      public static int G = 2;
    
    }
    
    Solution:
    
    A:  1 - 9 (all)
    B:  1 - 9 (all)
    C:  2
    D:  4 - 8
    E:  5, 6, 7
    F:  6
    G:  1 - 9 (all)