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];
}
}
-
For the above program, diagram the Stack class.
-
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.
-
For the above, what is the result of
printTop( b );
? Answer:-3
Isa.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");
}
}
-
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();
}
}
-
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();
}
}
-
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);
}
}
-
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
.-
if (i > n)
-
while(i > n)
-
Monitor.Wait(this);
-
catch(Exception e) {}
-
n++;
-
Monitor.PulseAll(this);
-
Monitor.Pulse(this);
-
lock(this)
-
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( ); // *** } }
-
For the above pseudocode program:
-
What are the values printed using static scoping? Answer:
9 9
-
What are the values printed using dynamic scoping? Answer:
9 5
-
Give the contour diagrams showing dynamic scoping. Answer: Diagrammed below
-
Give the contour diagrams showing static scoping. Answer: Diagrammed below
-
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; }
}
-
Assuming that Class9 a = new Class9(3); creates a Class9 object at address 123, graphically illustrate how the following program uses pass-by-value:
-
in executing
f(a)
Answer: below -
in executing
a.op2()
Answer:
-
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 ...
-
Graphically diagram the inheritance hierarchy of the above: Answer:
-
Is the following valid syntax?
Q10 X = new Class10a( );
Answer:Yes
-
Is the following valid syntax?
Class10a Y = new Q10( );
Answer:No
-
Is the following valid syntax?
Class10a Z = (Class10a) (new Q10( ));
Answer:Yes
-
With the above definitions, is the following valid syntax?
X.printClass10a();
Answer:No
-
With the above definitions, will the following compile and execute correctly?
X.printQ10();
Answer:Yes
-
With the above definitions, will the following compile and execute correctly?
Class10a Z = (Class10a) (new Q10( ));
Answer:Compiles but does not execute. (Cannot cast)
-
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
-
Show reference counting after
(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);
});
}
}
-
What are all possible printing of the above?
Hint: P(3,3)=3!=6
Answer:
012 021 102 120 201 210