15-100 Sections S-V / Fall 2008
Midterm Exam #1 (Written Portion)
Exam Date: Tue 14-Oct-2008
20 Questions + 2 Bonus Questions / 50 Minutes
All questions are equally weighted.
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 % 4);
System.out.println(3 / 2 * 3 + 3.0 / 2 * 3);
2.
System.out.println(Math.pow(2,3) + (4/3)*30);
System.out.println(1 + (int)(10 * 5.6789));
3.
char c = 'A';
int i = 'A';
System.out.println(c + "," + i);
4. System.out.println("a\tbc\nde\t\\\nfg\t\"");
5.
String s = "" + (100 * 100);
System.out.println(s.length() + (s.length() > 5 ? "ab" : "cd"));
6.
String s = "13579";
for (int i=0; i<s.length(); i+=2)
System.out.print(s.charAt(i) - '0');
7.
for (int i=1; i<5; i++)
for (int j=2; j<i; j++)
System.out.println(i + "," + j);
8.
Scanner s = new Scanner("2 4 -1 5");
int SENTINEL = -1;
int i = 0, x = 0;
while ((i = s.nextInt()) != SENTINEL) {
x += i;
System.out.println(i);
}
System.out.println(x);
9.
String s = "bbbcbcd";
while (s.indexOf("bc") > 0) {
s = s.replace("bc","c");
System.out.println(s);
}
10.
String s =
"abc";
s = s + s + s;
while (s.lastIndexOf('c') > s.indexOf('c')) {
s = s.substring(2);
if (s.charAt(0) != 'c')
System.out.println(s);
}
11.
String[] a =
{ "ab", "cd", "ef", "gh", "ab" };
int n = a.length;
for (int i=0; i<n; i++)
if (a[i].equals(a[n-1-i]))
System.out.println(a[i]);
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.
12. Demonstrate short-circuit evaluation.
13. Print “empty” if the String s is empty or “null” if it is null. Print nothing otherwise.
14.
Assume this method is already written:
public static int f(int x)
Using this method, print all the
numbers x between 1 and 100, inclusive, for which f(x) returns an odd number.
15. Assuming a is an array of int’s, swap the first two elements (or do nothing if the array is null or is less than length 2).
16. Assuming the array of int’s a is non-null, rotate all the values in the array one to the right with wraparound. So if a at first contains { 1, 2, 3, 4 }, afterwards it would contain { 4, 1, 2, 3 }.
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.
17.
page.drawRect(40, 0, 20, 100);
int startAngle = 45;
int extentAngle = 90;
page.fillArc(0, 0, 100, 50, startAngle, extentAngle);
18.
int x = 25, y
= 25;
for (int i=0; i<3; i++) {
page.drawOval(x-25, y-25, 50, 25);
x = (x + 50) % 100;
y += 75 * ((i+1)/2);
}
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);
}
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 absolute value of x is equal to x.”
19.
In general, what does this method do?
public static int g(int x) {
int y = 0;
String s = "" + Math.abs(x);
for (int i=0; i<s.length(); i++) {
char c = s.charAt(i);
if (s.substring(0,i).indexOf(c) < 0)
y++;
}
return y;
}
20.
In general, when does this method return “true”?
public static boolean f(int[] a, int[] b) {
if ((a == null) || (b == null) ||
(b.length + b.length != a.length))
return false;
for (int i=0; i<a.length; i+=2)
if (a[i] != b[i/2])
return false;
return true;
}
21.
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);
}
22.
Bonus/Optional:
Write just a few lines of code (not entire methods) for this problem.
Assume all variables are already declared and initialized.
Using Monte Carlo methods, print the odds (as a percentage) that if you choose
two random numbers between 0 and 100, inclusive, their difference will be no
larger than 10.
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem