| CIT591 Final Exam Fall, 2002 |
Name_________________________________________ |
System.out.println
statement. Then fill in the following table, telling (1) the simplest
possible way to print the variable, and (2) what value will be printed for
it. If a variable cannot be accessed, tell why.
public class Major {
int a = 1, b = 2, c = 3;
Extra extra = new Extra();
public static void main(String args[]) {
int b = 4, d = 5;
Minor minor = new Minor(b);
minor.printAll();
}
}
class Minor extends Major {
int c = 6, e = 7;
static int f = 8;
Minor(int param) { b = e = param; }
void printAll() {
System.out.println( expression from table below );
}
}
class Extra {
int f = 9;
Extra() { f = 10; }
class Inner { int g = 11; }
Inner inner = new Inner();
}
|
| Variable | Simplest expression to access variable | Value |
a in Major |
a |
1 |
b in Major |
b |
4 |
b in main |
Cannot access local variable
outside function |
|
c in Major |
super.c |
3 |
c in Minor |
c |
6 |
d in main |
Cannot access local variable
outside function |
|
e in Minor |
e |
4 |
f in Minor |
f |
8 |
f in Extra |
extra.f |
10 |
g in Inner |
extra.inner.g |
11 |
boolean divisible(int
x, int y) {
return x % y ==
0;
} final" modifierpublictransient)matcheslookingAtfind [+-]?\d+
or [+-]?[0-9]+final Point(float x) {
this(x / 2, 2
* x);
}
1 interface Inter { 2 int number(); 3 } 4 abstract class Abs { 5 static int foo = 12; 6 int number() { return 5; } 7 abstract int ace(); 8 } 9 final class Sub extends Super { 10 Sub(int bar) { foo = bar; } 11 public int number() { return 10; } 12 int ace() { return 13; } 13 int dubble(int i) { return 2 * i; } 14 } 15 public class Super extends Abs implements Inter { 16 public int number() { return 11; } 17 public static void main(String args[]) { 18 Super s1 = new Super(); 19 Super s2 = new Sub(16); 20 System.out.println( ); 21 } 22 int twice(int x) { return 2 * x; } 23 public int thrice(int x) { return 3 * x; } 24 int ace() { return 1; } 25 String dubble(String s) { return s + s; } 26 }
number() number() in Sub overrides
number() in Super(), and you cannot makeSub is created in line 19)Super (only)| s1.ace() 1 | s2.ace() 13 | s1.foo 16 | s2.foo 16 |
| s1.twice(3) 6 | s2.twice(3) 6 | s1.dubble(6) error | s2.dubble(7) error |
| s1.number() 11 | s2.number() 10 | s1.dubble("8") 88 | s2.dubble("9") 99 |