1. What does the following method do when we pass it an array containing the numbers 1,2,3,4,5?

public static void f(int[] A) {
  int n = 0;
  int[] B = new int[A.length];
  while(n < A.length) {
    B[A.length - n - 1] = A[n];
    n++;
  }
  n = 0;
  while(n < A.length) {
    A[n] = B[n];
    n++;
  }
}

Solution

2. What does the following program print when we give it the numbers 10 and 5 as inputs?

public class two {
  public static void main(String[] args) {
    int x,y,z;
    Scanner input = new Scanner(System.in);
    x = input.nextInt();
    y = input.nextInt();
    z = 1;
    while(y > 0) {
      z = z*x;
      y--;
    }
    System.out.println(z);
  }
}

Solution

3. Write a Java program that prompts the user to enter three integers and then prints out the largest of those three. For example, on input 5 12 7 your program will print 12.

Solution

4. Write the code for a method

public static double average(double A[])

that computes and returns the average value of the numbers in the array A.

Solution

5. Here is the code for a paint method in a graphics program that draws a single red rectangle on the screen:

public void paint(Graphics g)
{
  g.setColor(Color.red);

  int left = 100;
  int top = 100;
  int width = 40;
  int height = 40;
  g.fillRect(left,top,width,height);
}

Modify this method so that it draws a checkerboard pattern of red and black squares that fills the drawing window. You may assume that the graphics window is 400 pixels wide and 400 pixels high.

Solution

6. Write the code for a paint method that draws a set of 10 concentric circles centered in the center of a graphics window. You may assume that the window is 400 pixels wide and 400 pixels high. (The Graphics class method used to draw a circle is the method drawOval(x,y,width,height) where (x,y) is the location of the upper left hand corner of the bounding box for the oval and width and height are the dimensions of the bounding box.

Solution