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) { |
b) |
c) |
d) |
e) |
|
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 y = x - 8; x++; y--; int z = Math.max(y/x, Math.abs(x/y));
System.out.println(x + "," + y + |
c) int i = Integer.parseInt(s); double d = Double.parseDouble(s);
System.out.println(i + "," + d); |
d) 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'); |
|
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