15-100 Sections S-V / Fall 2008
Quiz 2 / 35 Minutes
Quiz Date:  Fri 12-Sep-2008

1.       Each of these Java snippets is error-free. Provide the output of each snippet.
Note: the Unicode value for ‘A’ is 65, ‘a’ is 97, and ‘0’ is 48.

a)

  char c = (char)('9' + 9);

  int i = 'C' - c;
  System.out.println(c + "," + i);

 

b)
  int x = 8 + 5 % 3;

  int y = 11 / x / 9;

  x--;

  y += 2;
  double z = Math.pow(x,y);

  System.out.println(x + "," + y +
                     "," + z);

 

c)
  String s = 1 + 2 + "3" + 4 + 5;

  double d1 = Integer.parseInt(s);

  double d2 = Double.parseDouble(s);

  System.out.println(d1 + "," + d2);

 

d)
  String s = "a\"";

  String t = s + 2 * s.length();
  double d = 0.5 * t.length();

  int i = (int) (d + (int) d);

  System.out.println(i + t + d);

 

e)
  System.out.println(2 + 3);

  System.out.println('2' + 3);

  System.out.println('2' + '3');

  System.out.println("2" + '3');

 

2.       Each of these Java snippets is error-free. Provide the output of each snippet.
Note: the Unicode value for ‘A’ is 65, ‘a’ is 97, and ‘0’ is 48.

a)

  int x = 7;

  int y = 1;

  while (x > 0) {

    x -= y;

    y++;

  }
  System.out.println(x + "," + y);

 

b)
  int x = 1, i = 1;

  for (i=0; i<=5; i+=2)

    x+=i;

  System.out.println(i + "," + x);

c)
  int x = 3;

  String s = "ab";

  while (x % s.length() < 2) {

    x += x;

    s += x;

  }

  System.out.println(s + "," + x);

 

d)
  String s = "Dog1Cat2Cow4";

  int i, x = 0;

  for (i=0; i<s.length(); i++) {

    if (s.charAt(i) < 'A')

      x += (s.charAt(i) - '0');

  }

  System.out.println(x + "," + i);

e)
  double d = 0.5, e = 1.5, f;

  for (f=e-d; f>d-e; e-=d) {

    d += f;

    f *= 2;

  }

  System.out.println(d + "," + e +
                     "," + f);

 

3.       aRewrite the following “for” loop as an equivalent “while” loop:
   for (int x = 3; x<10; x++)
     foo(x);


b)  In just a few words, explain why the following code will not run forever, but instead prints “Done!”
     int x = 3;
     while (x > 0) x++;
     System.out.println("Done!");
 

4.       Write isPalindrome, a method that takes a String and returns true if it is a palindrome (same forwards as backwards) and false otherwise.  This version does not ignore spaces or case, so “Aa” and “a ba” are not palindromes.  Note that the empty string is a palindrome but the null string is not.

public static boolean isPalindrome(String s) {

}

5.       Write isPerfect, a method takes takes an int value and returns true if it is a perfect number and false otherwise, where a perfect number equals the sum of its proper divisors.  So 6 is perfect, because its proper divisors are 1, 2, and 3, and 1+2+3=6.  8 is not perfect, because its proper divisors are 1, 2, and 4, and 1 + 2 + 4 = 7 != 8.  Note that no non-positive numbers are perfect.

public static boolean isPerfect(int n) {

}

6.     Bonus/Optional:
a) For booleans x and y, write an expression equivalent to ((x && !y) || (!x && y)) that does not use && or ||.

b)  A binary boolean function takes two boolean parameters and returns a boolean result. First, argue that there are exactly 16 unique binary boolean functions. Second, argue that every one of them can be implemented by applying some combination of &&, ||, and ! to the two parameters. As this result generalizes to any number of parameters, we see that Java can compute any boolean function using just &&, ||, and !.


carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem