NAME:



Midterm Exam
COS 126 Solutions, Spring 2012

  1. Expressions
    Type Value
    String "10099"
    int 3
    String "199A"
    boolean false
    double 1.0
    double 1.5
    boolean false
  2. Number Systems
    Decimal Binary in Memory Hex on display
    7 0000 0000 0000 0111 0007
    -7 1111 1111 1111 1001 FFF9
    162 0000 0000 1010 0010 00A2
    -275 1111 1110 1110 1101 FEED
  3. Methods and I/O
    1. 2
      0
      0
      
    2. 5
      0
      0
      
  4. Loops and Arrays
    1. 0 1 2 3 4 5 6 7 8 9
      1
      1 1
      1 1 1
      1 1 1 1
      1 1 1 1 1
      1 1 2 1 1
      1 1 3 1 1
    2. 0 1 2 2 2 3 9 
      
    3. a blank line
      
  5. Debugging
    1. 6 double x = StdIn.readDouble();
    2. 3 int N = Integer.parseInt(args[0]);
    3. 5 for (int i = 0; i < N; i++) {

  6. TOY
    1.  
      R[6] <-- 01
      R[4] <-- R[4] - R[6]
      
    2. 6 times.
    3. The program computes the sum of all integers from 1 to N
    4. Overflow.
      When R[4] is 0100 or more, the sum of the numbers is larger than 7FFF, the largest possible positive integer.

  7. Recursion
    1. The rightmost picture.
    2. 4 squares are uncovered.
    3. There were many correct answers. This is one:
      draw( x - s, y - s, s/2.2, level-1, k ); // bottom left
      draw( x + s, y - s, s/2.2, level-1, k ); // bottom right
      

  8. Sorting and Analysis
    1. 1-3 hours
    2. Insertion Sort
    3. Linear. The second call has a sorted array so only the comparisons between adjacent elements occur.