15-100 Sections S-V / Fall 2008
Quiz 1 / 30 Minutes
Quiz Date:  Fri 5-Sep-2008

1.       Each of these Java snippets contains one error.  Circle the error (carefully) and identify the kind of error as SE for Syntax Error, RE for Runtime Error, or LE for Logical Error.

a)

  public static int foo(x) {
    int y = 2*x;
    return y/3;
  }

 

b)
  int x = 2 + 3 % 4;
  int z = x % (x+1);
  String s = z + " + 3 = " + z + 3;
  System.out.println(s);


 

c)
  int x = 3/4;
  int y = 12/x;
  String s = x + "," + y;
  System.out.println(s);

 

d)
  int x = 32;
  int y = x % 30;
  System.out.print(x + ' % 30 = ' + y);

e)
  int x = 2, y = 3, z = 4;
  int m = Math.min(x, y, z);
  System.out.println("min = " + m);

 



 

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)

  char b = 'B';

  int i = b;

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

 

b)
  int x = 17 % 6;

  int y = x - 8;

  x++;

  y--;

  int z = Math.max(y/x, Math.abs(x/y));

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

 

c)
  String s = "1" + "2";

  int i = Integer.parseInt(s);

  double d = Double.parseDouble(s);

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

 

d)
  String s = "1\\2\n3";

  double d = (0.5 * s.length());

  int i = (int) d;

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

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

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

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

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

 

 

 

3.       a)  In just a few words, what is short-circuit evaluation of boolean expressions?


b)  In just a few words, what is the difference between the null String and the empty String?


c)  In just a few words, why do we have escape sequences in Strings?

4.       Write a method (that does not use “if” statements or the conditional operator ?:), letterGrade, that takes an integer score for a grade and returns a char representing the letter grade for that score, where scores of 90 or higher receive an A, 80-89 receive a B, 70-79 receive a C, 60-69 receive a D, and below 60 receive an E.  The method signature is provided for you.

public static char letterGrade(int score) {

}

5.       Write a method (that does not use “if” statements or the conditional operator ?:), printTime, that takes one parameter, the number of minutes past midnight (which you may assume is non-negative) and prints out the current time in the usual HH:MM format, including leading zeroes when necessary for the minutes but not the hours (so you would print out “four minutes past 9 o’clock” as 9:04.  You do not have print out “am” or “pm”, but you must handle arbitrarily large values for the minutesPastMidnight parameter.

public static void printTime(int minutesPastMidnight) {

}

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



b)  For positive ints x and y, where y>1, under what conditions will ((x – 1) % y) / (y – 1)  be non-zero?


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