printable version
Quiz 2
[
1]
[
2]
[
3]
[
4]
Problem Q2.1.
[8 pts]
What distinguishes an instance method from a static
method? Give an example of each from what we've seen in
class.
An instance method, such as the GOval class's
move method, is something that only an individual
object of that class can perform; by contrast, a static method,
like the Math class's sqrt method, is something
that the class itself performs.
Thus, we can write Math.sqrt(4) in a program, asking
the Math class to perform the sqrt method; but
GOval.move(1, 1) would be illegal — we would need the
name of a GOval object on the left side, such as
ball.move(1,1), if ball were a
GOval.
Problem Q2.2.
[8 pts]
Perform each of the following conversions.
| a. |
100101(2) to decimal |
| b. |
53(10) to binary |
| c. |
the eight-bit two's-complement pattern 11100110 to decimal |
| d. |
the number −23(10) to an
eight-bit two's-complement pattern |
| a. |
100101(2)
is 37(10) |
| b. |
53(10) to binary
is 110101(2) |
| c. |
the eight-bit two's-complement pattern 11100110
is −26(10) |
| d. |
−23(10)
is represented as 11101001 |
Problem Q2.3.
[6 pts]
When executed, what will the below program display?
public class Problem extends Program {
public void run() {
int a = 2;
int b = this.compute(a);
this.println(a);
this.println(b);
}
public int compute(int c) {
this.println(c);
c = 3 * c;
this.println(c);
return 5 * c;
}
}
Problem Q2.4.
[8 pts]
Complete the below program so that it displays
1/√1 + 1/√2 + 1/√3 + … + 1/√100.
public class SumSquareInverses extends Program {
public void run() {
double result = 0.0;
// your solution here
this.println("Sum is " + result);
}
}
for(int cur = 1; cur <= 100; cur++) {
result = result + 1.0 / Math.sqrt(cur);
}