15-110 Lecture 4 / Spring 2010 / Midterm Exam #1
Thu 4-Mar-2010

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.
Also note:  all code compiles and runs without errors.
 

Part IA:  What will the following code snippets print?
 

1.    System.out.println(10 % 4 + 4 % 10);
System.out.println(10 % 4 / 4 % 10);
System.out.println(10 % 4 * 4 % 10);


 

2.    int i = 1000*1000*1000; // one billion
System.out.println("a\nb " + (i < 0));
i += i;
System.out.println("c\td " + (i < 0));
i *= 2;
System.out.println("e\\f " + (i < 0));
i /= 2;
System.out.println("g\"h " + (i < 0));

 

3.    int foo = 97;
System.out.println("x" + foo + 3);
System.out.println("y" + (foo + 3));
System.out.println("z" + (char)(foo + 3));
char c = 'B';
System.out.println(c + "c" + (int)c);

 

4.    String s = "abcde";
char done = s.charAt(s.length()/2);
int n = 0;
while (s.charAt(n) != done) {
  System.out.println(s.charAt(n));
  n = (n + 3) % s.length();
}


 

5. String s = "123";
String t = "";
int x = 0;
for (int i=0; i<s.length(); i++) {
  t += s.charAt(i);
  x += Integer.parseInt(t);
  System.out.println(x);
}

 

6.    String s = "abcde";
String t = "";
for (int i=s.length()-1; i>=0; i--)
  t += ((i > 0) ? s.charAt(i/2) : s.charAt(s.length()-1));
System.out.println(t);


 

7.  // Hint:  This code prints exactly 5 lines. 
for (int x=10; x<40; x+=10)
  for (int y=x; y<=2*x; y+=20)
    System.out.println(x + "," + y);


 

8.    // Hint:  This code prints exactly 4 lines.
String s = "";
int x = 0;
while (true) {
  if (s.length() > x)
    break;
  s += x*x*x;
  x++;
  System.out.println(x + "," + s);
}
 


 

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.
 

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


 

10.  Print just the tenths digit of the double d.  So if d equals 1.234 print 2, if d equals -1.234 also print 2, and if d equals 2.0 print 0.



 

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.
 

11.
for (int y=10; y<100; y+=20) {
  int w = 5+Math.abs(50-y);
  page.drawLine(50-w,y,50+w,y);
}
 











 


 

12.
int arcWidth = 30, arcHeight = 30;
int startAngle = 135;
int extentAngle = 90;
page.drawRoundRect(0,20,60,80,arcWidth,arcHeight);
page.drawLine(0,20,60,80);
page.fillArc(0,20,60,80,startAngle,extentAngle);
 











 


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 f(String s) {
  for (int i=0; i<s.length(); i++) {
    char c = s.charAt(i);
    boolean b = true;
    for (int j=0; j<s.length(); j++)
      if (s.charAt(j) == c)
        b = !b;
    if (b == false)
      return false;
  }
  return true;
}


14. In general, when does this method do?

  // Note:  most of this is boilerplate code.  The "interesting" part is highlighted below.

  public static BufferedImage g(BufferedImage image) {
    // standard code to create a new target image
    Dimension dim = getImageSize(image, 1, 0);
    int width = dim.width;
    int height = dim.height;
    BufferedImage targetImage =
       new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    for (int x=0; x<width; x++)
      for (int y=0; y<height; y++) {
        // standard code to decode a pixel    
        int rgba  = image.getRGB(x,y);
        int alpha = (rgba >> 24) & 0xff;
        int red   = (rgba >> 16) & 0xff;
        int green = (rgba >>  8) & 0xff;
        int blue  = (rgba      ) & 0xff;

        // here is the "interesting" part
        int g = (red/128)*(green/128)*(blue/128)*255;
        red = g;
        green = g;
        blue = g;
        
        // standard code to encode a pixel
        rgba = (alpha << 24) | (red << 16) | (green << 8) | (blue);
        targetImage.setRGB(x,y,rgba);
      }
    return targetImage;
  }
 

Part 2:  2 Questions  (30 points)


15.  isSquareFree
Write a method named isSquareFree that takes an int and returns true if it is square-free and false otherwise.  An int is square-free if it is positive and is not divisible by any perfect square except 1.  The smallest square-free numbers are: 
1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, 23, 26, 29, 30, 31, 33, 34, 35, 37, 38, 39, ...


16.  uniqueLetterCount
Write a method named uniqueLetterCount that takes a String and returns the number of unique letters, ignoring case, that occur in that String.  For example, "Wowee" contains the letters 'e', 'o', and 'w' (ignoring case), so uniqueLetterCount("Wowee") should return 3.  Ignore all non-letters.  The null string contains no unique letters.

 

17.   Bonus/Optional:  What will the following code print?
    // this compiles and runs without errors
    int x = 0, y = 250;
    for ( ; x<=100*y; x+=1,y-=1)
      for ( ; x<=10*y; x+=10,y-=10)
        for ( ; x<=y; x+=100,y-=100)
          ; // do nothing!
    System.out.println(x + "," + y);

 

18.   Bonus/Optional:  In general, when does this method return “true”?

public static boolean g(int x, int y) {
  if ((x < 1) || (y < 1)) return false;
  for (int i=0; i<y; i++)
    if ((x == 0) || (x % 10 != y))
      return false;
    else
      x /= 10;
  return (x == 0);