CSCI A202 - Introduction to Programming (II)

Sample Review Exam

Time alloted: 30'

There are six questions, each should take you 5 minutes for 12/3 points (10 points in all).

1. Turtleland

Assume the following code in a file Turtles.java. You compile and run this code:

public class Turtles {
  public static void main(String[] args) {
    Turtle a = new Turtle("Condor"); 
    int x = 2; 
    a.position(); 
    a.jump(x, x + 2); 
    a.position(); 
  } 
}

class Turtle {
  private int x, y;    
  private String name; 
  Turtle(String givenName) { 
    name = givenName; 
    x = 0; 
    y = 0; 
  } 
  void jump(int newX, int newY) {
    x = newX;
    y = newY; 
    System.out.println(name + " jumped to (" + x + ", " + y + ") "); 
  } 
  void position() {
    System.out.println(name + " located at (" + x + ", " + y + ") "); 
  } 
}
What's the output of this program? Write your answer here:




Note: objects of class Turtle have a position, determined by their x and y coordinate in the plane, and they can jump to a certain location specified by a new pair of coordinates (the coordinates of the new position, to which the turtle jumps to). These are atomic turtles.

2. Arrays

2.1 Dot product: fill in the blanks such that the value printed by the program below (dotP) is the sum of all the products of the corresponding (that is, that have the same index) elements in the arrays a and b:

public class DotProduct {
  public static void main(String[] args) {
    int[] a = new int[10]; 
    int[] b = new int[10]; 
    for (int i = 0; i < a.length; i++) { 
      a[i] = (int)(Math.random() * 10 + 1);     // [1] 
      b[i] = (int)(Math.random() * 10 + 1);     // [2] 
    } 
    dotP = 
    




    System.out.println("The dot product is: " + dotP); 
  } 
}
What's the purpose of the lines marked with [1] and [2]. What can you tell about the values of the elements in the arrays a and b before the dot product of the two arrays (the sum of products as described) is computed. Write your (short) answer here:




2.2 Find maximum: implement the following algorithm of finding the largest number in an array of at least one integer. Initialize a variable (call it max) to contain the first element in the array and then starting with the second element in the array compare each element with the value in the variable. If the element that you're comparing is bigger than the value you have in max store the element in max. Do this until you reach the end of the array.

int[] a = new int[100]; 
// ...
// assume a is initialized at this point 
max = 





System.out.println("The largest number is: " + max); 
3. Loops: for into while

Rewrite the following piece of code using a while construct that accomplishes the same thing (sums all the odd numbers between 1 and 100, including 1 and 99).

sum = 0; 
for (int i=1; i < 100; i+=2) {
  sum += i;
} 







4. BreezyGUI

Assume an application that has at least two buttons in the interface. Write the buttonClicked method that outputs "Button A" in a messageBox if Button buttonA has been pressed and "Some other button, I presume" otherwise (also in a messageBox).







5. switch from if

Rewrite this code using a switch:

int i = (int)(Math.random() * 6 + 1); 
if (i % 2 == 0) { System.out.println("Go Hoosiers!"); }
else { System.out.println("IU all the way!"); }