CSCI A202 - Introduction to Programming (II)

Sample Review Exam - Solutions

Correct answers appear in blue.

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:

Answer: compiling Turtle.java produces two .class files:

If we run Turtles execution will start with its main method. This method creates a new object, an instance of class Turtle and will keep a reference to it in the Turtle a variable. When created, this object will set its private String name variable to "Condor" and the x and y coordinates to 0 and 0 respectively.

The instance method position() reports the current values of the x and y coordinates. Originally the Turtle is located at (0, 0). The jump(x, x + 2) method sets x and y to 2 and 4 respectively and after that it reports the change.

Calling position() once again we obtain the following output:

Condor located at (0, 0) 
Condor jumped to (2, 4) 
Condor located at (2, 4) 
On a real test, unless otherwise mentioned, no justification is required for such a question (in this case simply listing the correct the output would have been enough).

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 = 0; // since we are computing a sum 
    for (int i = 0; i < a.length; i++) 
      dotP += a[i] * b[i];  
    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:

Line [1] initializes elements of array a with integers from the interval [1, 11).

Line [2] takes care of the elements of array b in a similar fashion.

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 = a[0]; // first element of a
for (int i = 1; i < a.length; i++) 
  if (max < a[i]) 
    max = a[i];  
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;
} 
sum = 0; 
i = 1; 
while (i < 100) {
  sum += i; 
  i += 2; 
}

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).

public void buttonClicked(Button b) {
  if (b == buttonA) {
    messageBox("Button A"); 
  } else {
    messageBox("Some other button, I presume"); 
  } 
}
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!"); } 
Here's one way:
int i = (int)(Math.random() * 6 + 1); 
// i is now 1, 2, 3, 4, 5, or 6 
switch (i) {
  case  0:
  case  2:
  case  4:
  case  6: System.out.println("Go Hoosiers!");
           break; 
  default: System.out.println("IU all the way!");
           break; 
} 
Another way, suggested by Igal Elias, is:
int i = (int)(Math.random() * 6 + 1);
switch (i % 2) {
  case  0: System.out.println("Go Hoosiers!");
           break;
  default: System.out.println("IU all the way!");
           break; 
}