public class SimpleStack
{
   public static void printTop( Stack s) {
      System.Console.WriteLine( s.pop( ) );
   }

   public static void Main( )
   {
        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];
        }
}
  1. For the above program, diagram the Stack class.

test2 1

  1. Add Stack objects a and b to the diagram as they exist at the end of execution of b.push(-3) the main method.

Answered above.

  1. For the above, what is the result of printTop( b );? Answer: -3
    Is a.top = -5; valid? Answer: NO

using System;
using System.Threading;

public class Test2 {
  public static void Main( ) {
    for( int i=1; i<=2; i++) {
       new Printabcde();
       new Print12345();
    }
  }
}

class Printabcde
{
       public Printabcde() {
	   new Thread(new ThreadStart(run)).Start();
       }

       public void run(){
                System.Console.Write("a");
                System.Console.Write("b");
                System.Console.Write("c");
                System.Console.Write("d");
                System.Console.WriteLine("e");
        }
}



class Print12345
{
       public Print12345() {
	   new Thread(new ThreadStart(run)).Start();
       }

       public void run(){
                System.Console.Write("1");
                System.Console.Write("2");
                System.Console.Write("3");
                System.Console.Write("4");
                System.Console.WriteLine("5");
        }
}
  1. List all possible print results of the above program.

    A

    abcde
    abcde
    12345
    12345

    B

    12345
    12345
    abcde
    abcde

    C

    112233445
    5
    aabbccdde
    e

    D

    54321
    54321
    abcde
    abcde

    E

    1a2b3c4d5e
    12345
    abcde

Answer: A, B, C, E

using System;
using System.Threading;

public class Test2 {
  public static void Main( ) {
    Question5 q5 = new Question5();
    for( int i=1; i<=3; i++)
       new Print(q5);
    }
}

class Question5 {
        public void begin() {
                System.Console.Write("a");
                System.Console.Write("b");
                System.Console.Write("c");
                System.Console.Write("d");
                System.Console.WriteLine("e");
        }
}

class Print {
        Question5 q;
        public Print(Question5 q) {
		this.q = q;
		new Thread(new ThreadStart(run)).Start();
	}

        public void run(){
                q.begin();
        }
}
  1. List all possible print results of the above.

Answer: A, B

A

abcde
abcde
abcde

B

aaabcde
bbcde
cde

C

edcba
edcba
edcba

D

123
123
123

E

123
abcde
123
abcde
123
abcde

using System;
using System.Threading;

public class Test2 {
  public static void Main( ) {
    Question6 q6 = new Question6();
    for( int i=1; i<=3; i++)
       new Print(q6);
    }
}

class Question6 {
   public void begin() {
           lock(this) {
                System.Console.Write("a");
                System.Console.Write("b");
                System.Console.Write("c");
                System.Console.Write("d");
                System.Console.WriteLine("e");
           }
    }
}



class Print {
        Question6 q;
        public Print(Question6 q) {
                this.q = q;
	     new Thread(new ThreadStart(run)).Start();
        }

        public void run(){
                q.begin();
        }
}
  1. List all possible print results of the above.

Answer: A

A

abcde
abcde
abcde

B

aaabcde
bbcde
cde

C

edcba
edcba
edcba

D

123
123
123

E

123
abcde
123
abcde
123
abcde

using System;
using System.Threading;

public class Test2 {
        public static void Main( ) {
                Question7 q7 = new Question7();
                for( int i=1; i<=5; i++)
                        new Print(q7, i);
        }
}

class Question7 {
        int n = 1;
        public void begin(int i) {

	    lock(this) { // <h>
             b.     while(i>n)
	       	Monitor.Wait(this); // <c>
                  }
        }

        public void end(int i) {
	    lock(this) { // <h>
             e.      n++;
	        Monitor.PulseAll(this); // <f>
                  }
        }
}

class Print  {
        Question7 q7;
        int i;
        public Print(Question7 q7, int i) {
                this.q7 = q7;
                this.i = i;
	     new Thread(new ThreadStart(run)).Start();
        }

        public void run(){
                q7.begin(i);
                System.Console.WriteLine(i);
                q7.end(i);
        }
}
  1. Add some, not all, of the statements below to the begin() and end() methods to force the threads to print in the order of 1 2 3 4 5.

    1. if (i > n)

    2. while(i > n)

    3. Monitor.Wait(this);

    4. catch(Exception e) {}

    5. n++;

    6. Monitor.PulseAll(this);

    7. Monitor.Pulse(this);

    8. lock(this)

    9. try

int I, J;

void P1() {
        int Y;
        Y = J+I;
        cout << Y;
}

void P2() {
        int I;
        I = 3;
        P1( );  // *
}

void main() {
    I = 8;
    J = 1;
    P1( );   // **

    a: {
            int J;
            J = 2;
            P2( ); // ***
        }
}
  1. For the above pseudocode program:

    1. What are the values printed using static scoping? Answer: 9 9

    2. What are the values printed using dynamic scoping? Answer: 9 5

    3. Give the contour diagrams showing dynamic scoping. Answer: Diagrammed below
      test2.1

    4. Give the contour diagrams showing static scoping. Answer: Diagrammed below
      test2.2

using System;

public class Q9 {
  public static void Main( )  {
      Class9 a = new Class9(3);
      f(a);
      System.Console.WriteLine( 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; }
}
  1. Assuming that Class9 a = new Class9(3); creates a Class9 object at address 123, graphically illustrate how the following program uses pass-by-value:

    1. in executing f(a) Answer: below

    2. in executing a.op2() Answer:
      test2.3

class Q10 { void printQ10() { System.Console.WriteLine("Q10"); }
class Class10a : Q10 { void printClass10a() { System.Console.WriteLine("Class10a"); }
class Class10b : Q10 ...
class Class10c : Class10a ...
class Class10d : Class10b ...
class Class10e : Class10b ...
class Class10f : Class10d ...
class Class10g : Class10e ...
class Class10h : Class10g ...
  1. Graphically diagram the inheritance hierarchy of the above: Answer:
    test2.4

    1. Is the following valid syntax? Q10 X = new Class10a( ); Answer: Yes

    2. Is the following valid syntax? Class10a Y = new Q10( ); Answer: No

    3. Is the following valid syntax? Class10a Z = (Class10a) (new Q10( )); Answer: Yes

    4. With the above definitions, is the following valid syntax? X.printClass10a(); Answer: No

    5. With the above definitions, will the following compile and execute correctly? X.printQ10(); Answer: Yes

    6. With the above definitions, will the following compile and execute correctly? Class10a Z = (Class10a) (new Q10( )); Answer: Compiles but does not execute. (Cannot cast)

    7. With the above definitions and Question 10b, will the following compile and execute correctly? ((Class10a) X).printClass10a(); Answer: Yes.

using System;

public class Question12 {
 static void f1()  {
   System.Console.Write("1");
   try {
     System.Console.Write("2");
     f2();
     System.Console.Write("3");
   }
   catch (Exception e) { System.Console.Write("4"); throw e; }
   finally {  System.Console.Write("5"); }
   System.Console.WriteLine("6");
 }

 static void f2 () {
   if (true) throw new Exception();
 }

 public static void Main( )  {
   try{
	System.Console.WriteLine("7");
	f1();
	System.Console.WriteLine("8");
   }
   catch (Exception e) { System.Console.Write("9"); throw e; }
   finally {  System.Console.Write("10"); }
 }
}

List the output from the above.

Answer:

7
1 2 4 5 9
Exception was unhandled
  1. Show reference counting after Reference Counting

(You’ll have to do this one yourself!)

using System;
using System.Threading;
using System.Threading.Tasks;
class Example
{
    static void Main()
    {
        Parallel.For(0, 3, i =>
        {
                Console.Write(i);
        });
    }
}
  1. What are all possible printing of the above?
    Hint: P(3,3)=3!=6

Answer:

012
021
102
120
201
210