15-110 Lecture 9 / Fall 2009 / Midterm Exam #1
Thu 15-Oct-2009

80 minutes total (for Part 1 and Part 2 combined)
 


Part 1:  14 Questions (5 points each / 70 points total)


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

Part IA:  What will the following code snippets print?
 

1.    System.out.println(6 - 5 % 3 * 2);
System.out.println(6 + 5 / 4 + 3.0 / 2.0);


 

2.    double d = 3.689394721934;
int i = 10/(int)Math.floor(d);
String s = "" + i + Math.pow(Math.ceil(d),2);
System.out.println(i + "," + s);


 

3.    int foo = 65;
String s = "foo" + foo + (char)(foo);
char c = s.charAt(s.length()-2);
System.out.println(s + "," + c);


 

4.    String s = "\"\ta\n\tb";
String t = s + s;
System.out.println(t + t.length());

 

5.    String s = "abcdefg";
int n = s.length();
for (int i=n/2; i>0; i-=2)
  System.out.println(s.charAt(i) + "," + s.charAt(n-i));


 

6.    String[] a = { "abcd", "efghi", "j", "klmnopqrst" };
for (int i=0; i<5; i+=2) {
  for (int j=0; j<a.length; j++)
    if (i < a[j].length())
      System.out.print(a[j].charAt(i));
  System.out.println();
}


 

7.    Scanner scanner1 = new Scanner("Peanut Butter + Jelly" + "\n" +
                               "Bologna & Cheese" + "\n" +
                               "Pepperoni Pizza");
String s1 = scanner1.nextLine();
Scanner scanner2 = new Scanner(scanner1.nextLine());
while (scanner2.hasNext())
  System.out.println(scanner2.next().charAt(0));


 

8.    int i=4, j= 12;
while (i < j) {
  if (j % i < 2)
    j--;
  else
    i+=3;
  if (i + j == 18)
    continue;
  System.out.println(i + "," + j);
}

// Hint:  This code prints out exactly 3 lines.

 

9.    String s = "bcda", t = s+s+s;
while (!t.equals(s)) {
  if (t.contains("dab"))
    t = t.replace("dab","c");
  else
    while (t.contains("cc")) {
      t = t.replace("cc","c");
      System.out.print("x");
    }
  System.out.println(t);
}

// Hint:  This code prints out exactly 2 lines.
 


 

Part IB:  Write just a few lines of code (not entire methods) for each of these problems.

Assume all variables are already declared and initialized.
 

10.  Print “yes” if both String s and String t are non-null and the two strings have the same number of characters, and “no” otherwise.


 

11.  Print the the last odd value that occurs in values, an array of ints.  If the array is null, or if  it does not contain any odd values, print nothing at all.



 

Part IC:  What will the following code snippet paint?  Draw your answer inside the 100x100 window provided for you.  Do not worry about being pixel-perfect.  Within a few pixels is fine.
 

12.
page.drawRect(30,30,30,30);
int y = 50;
for (int x=10; x<100; x+=20) {
  page.fillOval(x-5,y-10,10,20);
  y = (y + 20)%100;
}

 











 


 

Part ID:  Answer the following questions in general, and in just a few words of plain English.  No credit will be given for missing the generality, and stating what the code (obviously) does at a lower level.
 

13.In general, when does this method return “true”?

public static boolean h(int[] a, int[] b) {
  for (int i=0; i<a.length; i++)
    if (a[i] % 2 == 0)
      for (int j=0; j<b.length; j++)
        if (b[j] == a[i])
          return true;
  return false;
}


 

14.In general, when does this method return “true”?

public static boolean p(int x) {
  int y = 0, z = 0;
  x = Math.abs(x);
  while (x > 0) {
    y = 10*y + x%10;
    z = 10*z + x/10%10;
    x /= 100;
  }
  return (y == z);
}


 

Part 2:  2 Questions  (15 points each / 30 points total)

 

15.  Monte Carlo (Risk)
In a simplified form of the game of Risk, two players roll dice.  Player A rolls 3 dice, and Player B rolls 2 dice.  Player A wins if the highest value of her 3 rolls is greater than the highest value of Player B’s 2 rolls.  Note that Player B wins in a tie.  So:
  * if A rolls [3, 4, 2] and B rolls [3, 1], A wins (4 beats 3).
  * if A rolls [3, 4, 2] and B rolls [3, 4], B wins (4 ties 4).
  * if A rolls [3, 4, 2] and B rolls [6, 1], B wins (6 beats 4).

Write a method that uses Monte Carlo techniques to compute and return the odds that Player A wins one round of this game.  (You must use Monte Carlo techniques, even if you know how to solve this another way.)


 

16.  Animation
Write an animation as follows:
  * A red circle moves back-and-forth (not wrapping around!) horizontally along the top edge of the screen;
  * A blue square moves bottom-to-top (with wraparound) along the right edge of the screen.
  * Each time the user hits ‘s’, the circle and the square swap colors.

 

17.   Bonus/Optional:  What will the following code paint?

class MidtermBonus extends JComponentWithEvents {
  private int[] a = { 8, 3, 4, 2, 1, 6, 7, 9, 5 };
  public int s() { return (int)Math.round(Math.sqrt(a.length)); }
  public int r(int i) { return s()-1-i/s(); }
  public int c(int i) { return i%s(); }
  public int i(int r, int c) { return s()*(s()-1-r) + c; }
  public boolean sr(int tr) { int s=2*a[0]; for (int c=0;c<s();c++) s-=a[i(tr,c)]; return s<=0; }
  public void paint(Graphics2D page) {
    page.setFont(new Font("Arial",Font.BOLD,16));
    for (int i=0;i<a.length; i++) {
      if (sr(r(i))) page.drawOval(20*c(i)+10,20*r(i)+10,20,20);
      drawCenteredString(page,""+a[i],20*c(i)+10,20*r(i)+10,20,20);
    }
  } 
  public static void main(String[] args) { launch(120, 120); }
}