15-110 Sections M-Q / Fall 2009 / Quiz 4
5 Parts / 30 Minutes
 

·         Only write your final answer in the answer column.  Place scratch work outside the answer boxes.

·         All code examples compile and run without errors.

·         Unicode ‘A’ is 65, ‘a’ is 97, and ‘0’ is 48.
 

 

1a.  (10 pts) Writing Code

Assuming the variable s holds a non-null String, write two lines of code that print the characters in s in reverse order, one character per line.

 
 

1b.  (10 pts) Writing Code
Assuming the variables n and m hold non-negative integers, write a few lines of code that print out n lines each with m characters, where the lines alternate + and -.  For example, if n=3 and m=5, your code would display:

+-+-+
+-+-+
+-+-+


 

2a.  (10 pts) Tracing
Indicate what the following will print.
    String s = "ab";

    int x = 12, y = 2;

    while (x > y + s.length()) {

      s += x;

      x--;

      y++;

      System.out.println(x + "," + y + "," + s);

    }


 

2b.  (10 pts) Tracing
Indicate what the following will print.
    int y, x=3;

    for (y=10; y>=x; y-=x) {

      System.out.println(x + "," + y);

      x++;

    }

    System.out.println("y=" + y);

 

 

3a.  (10 pts) Mystery Method
State, in 8 words or less, what this method does in general.
 
public static boolean g(int x, int y) {

   int counter = 1;

   if (y < 1) return false;

   while (y > 0) {

     counter++;

     if (isPrime(counter)) // assume isPrime

       y--;                // is written for you

   }

   return (counter == x);

 }


 

3b.  (10 pts) Mystery Method
State, in 8 words or less, what this method does in general.  You may ignore cases where the method might crash.

  public static boolean f(String s) {

     int i, x = 0;

     for (i=0; i<s.length(); i++) {

       char c = s.charAt(i);

       if ((c >= 'a') && (c <= 'z'))

         x++;

       else if ((c >= 'A') && (c <= 'Z'))

         x--;

     }

     return (x >= 0);

   }


 

4.  (20 pts) Tracing
Indicate what the following will paint after 7 keypresses:

   
import java.awt.*;

  import javax.swing.*;

  class MyAnimation extends JComponentWithEvents {

 

   private int size=4, hrow=0, hcol=0;

 

   public void keyPressed(char key) {

     hrow++;

     if (hrow % size == 0)

       hcol++;

   }

 

   public void paint(Graphics2D page) {

    int row, col;

    for (row=0; row<size; row++)

      for (col=0; col<=row; col++) // note: col<row!!!

        if ((row == (hrow % size)) && (col == (hcol % size)))

          page.fillOval(20*col, 20*row, 20, 20);

        else

          page.drawOval(20*col, 20*row, 20, 20);

   }

   public static void main(String[] args) {launch(80, 80);}

  }


 

5.  (20 pts) Reading / Short Answers (Answer in 5 words or less.)
5a.
  The Java compiler translates Java source code into Java __________.
5b.  Storage capacity approximately doubles about once every ___________.
5c.  Draw a very simple picture showing how an analog signal is digitized by sampling.

5d.  Domain servers translate domain names to their corresponding ___________.

5e.  In Java, ___________  items are “considered old fashioned and should not be used.”

5f.  In Java, the ________ type is like the “double” type, only with less precision.

5g. Give an example of assignment conversion.

5h.  Write the expression tree for:  a + (b - c)  / d

 
 

6.  Bonus/Optional:   Tracing
Indicate what the following will print.

import java.util.*;
class Quiz4Bonus {  
  public static int[] f(int[] a, int[] b) {
    int n = Math.min(a.length, b.length);
    int[] result = new int[n];
    for (int i=0; i<n; i++)
      result[i] = a[i] + b[n-1-i];
    return result;
  }

  public static void main(String[] args) {
    int[] x = { 1, 2, 3, 42, 99, 100 };
    int[] y = { x.length, x[3], x[2]-x[1] };
    System.out.println(Arrays.toString(f(x,x)));
    System.out.println(Arrays.toString(f(x,y)));
    System.out.println(Arrays.toString(f(x,f(x,y))));
  }
}