15-100 Sections U-V-AA / Spring 2009
Midterm Exam #1 (Written Portion)
Exam Date: Tue
3-Mar-2009
16 Questions + 2 Bonus Questions /
50 Minutes
All questions are equally weighted (5 points each / 80 points total)
Note: Unicode ‘A’ is 65, ‘a’ is 97, and
‘0’ is 48.
Part I: What
will the following code snippets print?
1. System.out.println(1 + 5 % 2
* 3);
System.out.println(99 / 98 / 97 + 96 / 95);
2. double d = 1.93431235412324;
int i = 3/(int)d + (int)(3/d);
d = Math.pow(Math.ceil(d),1+Math.floor(d));
System.out.println(d + "," + i);
3.
char
c = 'b';
int i = 'b';
String s = "A" + c + i;
System.out.println(c + "," + i + "," + s);
4.
String
s = "\\a\tb\nc\te";
System.out.println(s + s.length());
5.
String
s = "abcdef", t = s + s;
for (int i=t.length()-2; i>0; i/=3)
System.out.print(t.charAt(i));
6.
String[]
a = { "abc", "def", "ccc" };
for (int i=0; i<3; i++)
for (int j=i+1; j<3; j++) {
System.out.println(i + ","
+ j);
if (a[i].charAt(j) ==
a[j].charAt(i))
System.out.println(a[i].charAt(j));
}
7.
Scanner
scanner = new Scanner("1234252 + 6728 == 1240980");
scanner.useDelimiter("2");
while (scanner.hasNextInt())
System.out.println(scanner.next());
System.out.println("---");
do
System.out.println(scanner.next());
while (scanner.hasNextInt());
8.
int
i=8, j= 12;
while (true) {
if ((i % 3) == (j % 4))
break;
else if ((i % 4) > (j % 3)) {
j--;
continue;
}
System.out.println(i + "," +
j);
j--;
i++;
}
System.out.println(i + "," + j);
// Hint:
This code prints out exactly 3 lines.
9.
String
s = "abbc", t = s+s+s;
int i = 0;
while (t.indexOf("ca") >= 0) {
t = ((i % 2 == 0) ?
t.replace("bc","c") : t.substring(3));
i++;
System.out.println(i + "," +
t);
}
// Hint: This code prints out exactly 4 lines.
Part II: Write
just a few lines of code (not entire methods) for each of these
problems.
Assume all variables are already declared and
initialized.
1. Print
“empty” if the String s is empty or “null” if it is null. Print nothing otherwise.
2. Print “yes”
if the String s is a palindrome (same forwards as backwards) and “no”
otherwise. Empty strings are
palindromes, but null strings are not.
Also, "Aa" and "ab a" are not palindromes.
3. Assume these
methods are already written:
public static int f(int x)
and
public static int g(int x)
Using
these methods, and assuming that "a" is a non-null array of int’s,
print all the values x in the array a where exactly one of f(x) or g(x) is even
and the other is odd.
Part III: What
will the following code snippets paint?
Draw your answer inside the 100x100 window provided for you. Do not worry about being pixel-perfect. Within a few pixels is fine.
4.
page.drawRect(25,25,25,25);
page.drawRect(50,50,25,25);
for (int x=25; x<100; x+=50)
for (int y=25; y<100; y+=50)
if (x <= y) {
int dx = (x > 50) ? 0 : -10;
page.fillRect(x+dx,y,20,20);
}

5.
page.drawLine(50, 30, 50, 80);
Polygon p = new Polygon();
int[] a = { 2, 4, 6, 8, 9, 7, 5, 3 };
for (int i=0; i<a.length; i+=2)
p.addPoint(10*a[i],10*a[i+1]);
page.drawPolygon(p);

Part IV: Answer
the following questions in general, and in just a few words of plain
English.
For example, consider this method:
public boolean foo(int x) {
return (Math.abs(x) - x == 0);
}
This method “tests if x is non-negative”.
No credit will be given for missing this generality, and stating what
the code (obviously) does at a lower level, as in: “tests if the difference of the absolute
value of x and x is equal to zero.”
6.
In general, when does this method return “true”?
public static boolean h(int[] a, int[] b) {
int x = 0;
int m = Math.max(a.length, b.length);
for (int i=0; i<m; i++) {
if (i < a.length) x += a[i];
if (i < b.length) x -= b[i];
}
return (x == 0);
}
7.
In general, assuming d is in the range (0,1), what
does this method do?
public static void g(double d) {
if ((d < 0) || (d >= 1)) {
System.out.println("out of
range");
return;
}
int max = 1000;
int bestX = 0, bestY = 1;
double bestDiff = -1;
for (int x=0; x<=max; x++)
for (int y=x+1; y<=max; y++) {
double guess = 1.0*x/y;
double diff = Math.abs(guess - d);
if ((bestDiff < 0) || (diff <
bestDiff)) {
bestDiff = diff;
bestX = x;
bestY = y;
}
}
System.out.println(bestX +
"/" + bestY);
}
8. Bonus/Optional:
The preceding problem is needlessly inefficient – it makes on the order of (max2)
guesses, so it runs very slowly as max increases to, say, one million or
so. Explain how to change the code so it
makes on the order of (max) guesses, thus allowing us to efficiently increase
max even to a billion or so.
9. Bonus/Optional: In general, what does this method do?
public static int q(int[] z) {
int a=9,b=8,c=7,d=a*b/c,e=a+b-c,f=d-e;
for (int q=z.length-1; q>=0; q--)
for (int r=Math.abs(z[q]); r>0;
r/=d)
for (int s=r%e;s>0; s/=d)
f+=(s%2);
return f;
}