Document last modified:
1) For the following program, diagram the Stack class.
2) Add Stack objects a and b to the diagram as they exist at the
end of execution of b.push(-3) the main method.
public class SimpleStack
{
public static void printTop( Stack s) {
System.out.println( s.pop( ) );
}
public static void main(String args[])
{
Stack a = new Stack();
a.push(-4);
a.push(7);
a.push(9);
Stack b = new Stack();
b.push(8);
b.push(-3);
printTop( b );
a.top = -5;
}
}
|
class Stack
{ private double data[];
private int top;
public Stack()
{ top = -1;
data = new double[5];
}
public void push ( double e )
{ top++;
data[top] = e;
}
public double pop()
{ top--;
return data[top+1];
}
}
|
Answer to 1 & 2
3) For the above, what is the result of printTop( b ); ? -3
Is a.top = -5; valid? NO
4) List all possible print results. A, B, C, E
public class Test2 {
public static void main(String args[]) {
for( int i=1; i<=2; i++) {
new Printabcde();
new Print12345();
}
}
}
class Printabcde implements Runnable
{
Printabcde() {
new Thread(this).start();
}
public void run(){
System.out.print("a");
System.out.print("b");
System.out.print("c");
System.out.print("d");
System.out.println("e");
}
}
|
class Print12345 extends Thread
{
Print12345() {
new Thread(this).start();
}
public void run(){
System.out.print("1");
System.out.print("2");
System.out.print("3");
System.out.print("4");
System.out.println("5");
}
}
|
5) List all possible print results. A, B
public class Test2 {
public static void main(String args[]) {
Question5 q5 = new Question5();
for( int i=1; i<=3; i++)
new Print(q5);
}
}
class Question5 {
void begin() {
System.out.print("a");
System.out.print("b");
System.out.print("c");
System.out.print("d");
System.out.println("e");
}
}
class Print implements Runnable {
Question5 q;
public Print(Question5 q) {
this.q = q;
new Thread(this).start();
}
public void run(){
q.begin();
}
}
|
|
6) List all possible print results. A
public class Test2 {
public static void main(String args[]) {
Question6 q6 = new Question6();
for( int i=1; i<=3; i++)
new Print(q6);
}
}
class Question6 {
synchronized void begin() {
System.out.print("a");
System.out.print("b");
System.out.print("c");
System.out.print("d");
System.out.println("e");
}
}
|
class Print implements Runnable {
Question6 q;
public Print(Question6 q) {
this.q = q;
new Thread(this).start();
}
public void run(){
q.begin();
}
}
|
7) Add some, not all, of the statements at right to the begin() and
end() methods to force the threads to print in the order of 1 2 3 4 5.
public class Test2 {
public static void main(String args[]) {
Question7 q7 = new Question7();
for( int i=1; i<=5; i++)
new Print(q7, i);
}
}
class Question7 {
int n = 1;
synchronized void begin(int i) {
b. while(i>n)
c. try{ wait(); }
d. catch(Exception e) {}
}
synchronized void end(int i) {
e. n++;
f. notifyAll();
}
}
class Print implements Runnable {
Question7 q7;
int i;
public Print(Question7 q7, int i) {
this.q7 = q7;
this.i = i;
new Thread(this).start();
}
public void run(){
q7.begin(i);
System.out.println(i);
q7.end(i);
}
}
|
|
8) For the following pseudocode program:
| int I, J;
void P1() { void P2() { void main() { a: { |
|
9) Assuming that Class9 a = new Class9(3);
creates a Class9 object at address 123, graphically illustrate how the following
Java program uses pass-by-value:
a) in executing f(a)
b) in executing a.op2()
public class Q9 {
public static void main(String s[]) {
Class9 a = new Class9(3);
f(a);
System.out.println( a.op2( ) );
}
public static void f( Class9 b) {
b.op1(5);
}
}
class Class9 {
int n;
Class9(int n) { this.n = n; }
public void op1(int n) { this.n = n; }
public int op2( ) { return this.n; }
}
|
|
10a) Graphically diagram the inheritance hierarchy of the following:
class Q10 { void printQ10() { System.out.println("Q10"); }
class Class10a extends Q10 { void printClass10a() { System.out.println("Class10a"); }
class Class10b extends Q10 ...
class Class10c extends Class10a ...
class Class10d extends Class10b ...
class Class10e extends Class10b ...
class Class10f extends Class10d ...
class Class10g extends Class10e ...
class Class10h extends Class10g ...
|
10b) Is the following valid syntax? Q10 X = new Class10a( ); Yes. Class10a is a Q10.
10c) Is the following valid syntax? Class10a Y = new Q10( ); No. Q10 is not Class10a.
10d) Is the following valid syntax? Class10a Z = (Class10a) (new Q10( )); Yes. Q10 is cast as a Class10a.
10e) With the above definitions, is the following valid syntax? X.printClass10a(); No. X is a Q10 object, no printClass10a method.
10f) With the above definitions, will the following compile and execute correctly? X.printQ10(); Yes. X is Q10 class that references a Class10a.
10g) With the above definitions, will the following compile and execute correctly? Class10a Z = (Class10a) (new Q10( )); Compiles but does not execute. Cannot cast a Q10 into a Class10a object.
10h) With the above definitions and Question 10b, will the following compile and execute correctly? ((Class10a) X).printClass10a(); Yes. X is Q10 but references a Class10a, the compiler is instructed to cast as Class10a.
11. Complete the following:

12a. Circle and label as 1, 2, ... the scoping levels on the following:
|
|
12b. Static nesting level of definition: a) N 1 b) b 1 c) c 1 d) val 2 12c. Static distance of identifiers at the given lines: Line 12, N 0 Line 10, N 1 Line 9, val 0 12d. Fill in stack information below through line 10 execution Stack Address
| |428
| |427
| |426
| |425
| |424
| |423
| |422
| |421
| |428
| |427
| |426
| 415 |425 SL
| |424 IP
AR(c) | 419 |423 DL
| 5.8 |422 val
AR(b) | 415 |421 SL
| 6 |420 IP
| 415 |419 DL
| OS |418 SL
AR(a) | 14 |417 IP
| OS |416 DL
| 1 |415 N
|
13. List the output from the following:
public class Question12 {
static void f1() throws Exception {
System.out.print("1");
try {
System.out.print("2");
f2();
System.out.print("3");
}
catch (Exception e) { System.out.print("4"); throw e; }
finally { System.out.print("5"); }
System.out.println("6");
}
static void f2 () throws Exception {
if (true) throw new Exception();
}
public static void main(String s[]) throws Exception {
try{
System.out.println("7");
f1();
System.out.println("8");
}
catch (Exception e) { System.out.print("9"); throw e; }
finally { System.out.print("10"); }
}
}
7
1 2 4 5 9 10
Exception in thread "main" java.lang.Exception
at Question12.f2(Question12.java:15)
at Question12.f1(Question12.java:6)
at Question12.main(Question12.java:21)
|