15-110 Sections M-Q / Fall 2009 / Quiz 2
(version A)
30 Minutes
· Only write your final answer in the answer column. Place scratch work outside the answer boxes.
· You may not use conditionals or loops or String methods except length() and charAt().
· All code examples compile and run without errors.
· Unicode ‘A’ is 65, ‘a’ is 97, and ‘0’ is 48.
|
1. (10 pts) Writing Code |
Answer #1 |
|
|
2. (10 pts) Writing Code
Write a method that takes one parameter, a Graphics2D “page”, and paints
an unfilled cobalt oval that just fits in the top-right quadrant of the window.
The RGB values for cobalt are 0, 71, and 171.
|
Answer #2 |
|
3. (20 pts)
Tracing int x = 11, y = 6; System.out.println("result=" + 9/y + 9.0/y); System.out.println(x%y+y%x); System.out.println(x%y*y%x); x%=y; y++; System.out.println(x + "," + y); } |
|
Answer #3 |
|
4. (10 pts)
Mystery Method return (x/10 == x%10); } |
Answer #4 |
|
|
|
5. (10 pts)
Mystery Method String score = "Cubs 10, Pirates 9"; char c1 = score.charAt(6); char c2 = score.charAt(score.length()-1); return ((c < c1) || (c > c2)); } |
Answer #5 |
|
6. (20 pts)
Tracing int x = 10; double d = x; int z = 3*1000*1000*1000; // 3 billion System.out.println(z<z/2); System.out.println(Math.pow(Math.sqrt(17), x/4)); String s = "a\"\t\\b\n\td"; System.out.println(s.length() + s); char c = "Cat".charAt(1); System.out.println(c + ("c+1") + (c+1)); } |
|
Answer #6 |
7. (10 pts) De Morgan’s Law
State one of De Morgan’s Laws, and then prove it using truth tables. You must
use appropriate helper columns.
|
Answer #7 |
|
8. (10 pts) Div
and Mod |
|
||
|
Answer #8a: |
|
Answer #8b: |
|
|
9.
Bonus/Optional: (5 pts) Mystery
Method assert(n > 0); int x = 2; int answer = 0; int counter; for (counter=1; counter<=n; counter++) { if ((counter+1)/x == (counter+1)%x) { x*=2; answer++; } } return answer;
} |
Answer #9 (Bonus) |
|
|