15-100 Sections S-V / Fall 2008
Quiz 8 / 15 Questions / 30 Minutes
Quiz Date: Tue 18-Nov-2008
- Write a single line of
code total, and not one line per part, that demonstrates all
of the following, each of which must be clearly labeled:
- Assignment
Conversion
- Method Invocation
Conversion
- Promotion
- Casting
- \b is the escape
sequence for _______________________.
- The smallest byte value
is ______________ and the largest char value is _________________.
- In the code below, “a”
is a(n) _______________ parameter and “b” is a(n) _______________ parameter.
public int foo(int a) {
int b = a – 32;
return Math.abs(b);
}
- Give an example of each
of the following in Java’s libraries (and not your own classes):
- A static method
- An instance method
- An overloaded
method
- A static field
- The following code
demonstrates overflow. What, exactly, will it print?
SHOW YOUR WORK!
byte b1 = 76;
byte b2 = 100;
byte b3 = (byte) (b1 + b2);
System.out.println(b3);
- Write one line
of code that shuffles the values in an array “a” of Strings.
- Unlike arrays,
ArrayLists are _____________________________.
- Unlike ArrayLists,
HashSets are not _______________ and do not contain ____________________.
- Wrapper classes allow
the JCF to work with __________________________.
- In just a few words of
plain English, what does the following method do?
public static int g(ArrayList<Integer> list) {
return (new HashSet<Integer>(list)).size();
}
- Given the following
code, what will the call
f(4, 3, -1, 6, -3, -2, 17)
print?
public static void f(int... a) {
int sum = 0;
for (int i : a)
if (i % 2 == 0)
sum += i;
else
System.out.print((i < 0) ? i : ""); // note: not println
System.out.println(sum);
}
- What will the following
code print?
int x = 0, y = 10;
while (true) {
System.out.println((x++) + "," + y); // note x++
if (x < 3)
continue;
else if (--y < 9) // note --y
break;
}
System.out.println(x + "," + y);
- What will the following
code print?
int x = 0, y = 7;
for ( ; x<=y; x++,y-- ) // note unusual usage
switch (x) {
case 0:
case 2: System.out.print("A");
break;
case 3: System.out.print("B");
x++;
// note: no break! fall-through!
case 1: System.out.print("C");
y++;
break;
default:
System.out.print("D");
}
System.out.println(x + "," + y);
- Write the following
method (which may be a helper method from Tetris): This method takes a 2d
array of booleans, the “piece”, and returns another 2d array of booleans
representing that same piece rotated 90 degrees counterclockwise. Do not
worry about changing the piece’s left or top to keep its center fixed, just
create the new 2d array.
public boolean[][] rotate(boolean[][] piece)
{
}
- BONUS: What will the
following code print?
int x = 0, y = 250;
for ( ; x<=100*y; x+=1,y-=1)
for ( ; x<=10*y; x+=10,y-=10)
for ( ; x<=y; x+=100,y-=100)
; // do nothing!
System.out.println(x + "," + y);
carpe diem -
carpe diem - carpe diem - carpe diem
- carpe diem - carpe diem -
carpe diem - carpe diem - carpe
diem