<!doctype html public "-//w3c//dtd html 4.0 transitional//en">

CS 476/576
Systems Programming
Fall 2004
Final Exam
Time 2 & 1/2 hours
Open Book & Notes

 

 

Name:                              
Login:

 

 

NOTE: Please Do NOT write any include statements in your code.


Question 1: (25 points)

What is a “possible” output of the following program?

public class q1 extends JApplet  {

  static int flip = 0;

  static int tot = 0;

  static int totnum = 0;

  static Random rand = new Random();

  int[] a;

  public void init() {

    Container cp = getContentPane();

    cp.setLayout(new GridLayout(5,5));

    a = new int[25];

    System.out.println("length of a = " + a.length);

    while (tot < 25) {

        flip = rand.nextInt(25);

        totnum++;

        if (a[flip] < 1) {

                a[flip]=1;

                tot++;

                cp.add(new JButton("Button " + flip));

        }

    }

    System.out.println("totnum: " + totnum );

 }

 public static void main(String[] args) {

    Console.run(new q1(), 600, 500);

 }

}

Ansewer:


Question 2: (25 points)

Write a program that creates a 25 buttons arranged as a 5x5 grid. When a user clicks on button i, the program sends the message “button i” to a udp server running at host “cash” and port “1234”.

Ansewer:


Question 3: (25 points)

Write a udp server that runs at port “1234” and displays the received messages in a text area.

Ansewer:


Question 4: (25 points)

A. What is a “possible” output of the following program?

public class q4 extends Thread {

  private int countDown = 5;

  private static int threadCount = 0;

  private int threadNumber = ++threadCount;

  public q4() {

    System.out.println("Making " + threadNumber);

  }

  public void run() {

    while(true) {

      System.out.println("Thread " +

        threadNumber + "(" + countDown + ")");

        if(--countDown <= 0) return;

    }

  }

  public static void main(String[] args) {

    for(int i = 0; i < 5; i++)

      new q4().start();

    System.out.println("All Threads Started");

  }

}

Ansewer:


B. What is a “possible” output of the above program
if the word “static” is added before the variable “countDown”?

Ansewer: