JMU JMU - Department of Computer Science
Help Tools
Sample Questions for Exam 2


    Design Patterns
  1. In Java, give an example of a method in the ServerSocket class that behaves like a factory (i.e., constructs and returns an object).
  2. Threading
  3. The application in class Question below uses the two classes TickReader and TickWriter. Trace the execution of this application by writing numbers/letters next to each line indicating the thread it is executed in and the order in which it is executed. Lines in the first thread should be numbered using 1,2,3... Lines in the second thread (if there is one) should be numbered using A,B,C... Lines in the third thread (if there is one) should be numbered using a,b,c...
      import java.io.*;
    
    
    
      /**
       * The application
       *
       */
      public class Question
      {
    
    
        /**
         * The entry point of the application
         *
         * @param args   The array of command-line arguments
         */
        public static void main(String[] args)
        {
            BufferedReader  br;
            FileReader      fr;
            PrintWriter     pw;
    	TickReader      in;
    	TickWriter      file, screen;
    
    
    
    
    	try {
    
                fr = FileReader("question.tik");
                br = new BufferedReader(fr);
                in = new TickReader(br);
    
                pw = new PrintWriter(System.out, true);
    
                screen = new TickWriter(pw);
    
                in.setTickObserver(screen);
    
                in.readTick();
    
                screen.start();
    
                in.readTick();
    
    
    	} catch (Exception exception) {
    
    	    System.err.println(exception);
    	}
        }
      }
    
    
    
      
      import java.io.*;
      import java.util.*;
    
      /**
       * A class for reading Tick information from a BufferedReader
       *
       */
      public class TickReader
      {
        protected BufferedReader   reader;
        protected TickWriter       observer;
    
    
    
        /**
         * Construct a new TickReader
         *
         * @param reader   The BufferedReader to read from
         */
        public TickReader(BufferedReader reader)
        {
    	this.reader = reader;
    	observer = null;
        }
    
    
    
        /**
         * Set the observer
         *
         * @param observer      The new observer
         */
        public void setTickObserver(TickWriter observer)
        {
    	this.observer = observer;
        }
    
    
    
    
        /**
         * Notify the observer of a Tick
         *
         * @param tick          The tick to notify them of
         */
        public void notifyTickObserver(String tick)
        {
    	observer.handleTickNotification(tick);
        }
    
    
    
    
        /**
         * Read one Tick object and notify the observer
         *
         */
        public void readTick() throws IOException
        {
    	String tick;
    
    	tick = reader.readLine();
    	notifyTickObserver(tick);
        }
      }
    
      
      import java.io.*;
      import java.util.*;
      /**
       * A class for writing Tick information to a PrintWriter
       */
      public class TickWriter implements Runnable
      {
        protected LinkedList    queue;
        protected PrintWriter   writer;
        protected Thread        ctrl;
    
        /**
         * Construct a new TickWriter
         *
         * @param Writer   The BufferedWriter to use
         */
        public TickWriter(PrintWriter writer)
        {
    	this.writer = writer;
    	queue = new LinkedList();
            ctrl = new Thread(this)
        }
    
    
        /**
         * Handle notification from a TickReader
         *
         * @param tick      The tick to be handled
         */
        public void handleTickNotification(String tick)
        {
    	queue.addLast(tick);
        }
    
    
        /**
         * Print the tick on the writer
         *
         * @param tick  The tick to print
         */
        private void printTick(String tick)
        {
    	writer.println(tick);
        }
    
    
        /**
         * Code to run in the control thread
         */
        public void run()
        {
    	String    tick;
    
    	if (queue.size() > 0) {
    
    	    tick = (String)(queue.removeFirst());
    	    printTick(tick);
    	}
        }
      }
    
    
      /**
       * Starts the control Thread
       */
      public void start()
      {
          ctrl.start();
      }
      
    The file question.tik contains the following information:
      MSFT,STOCK,129.175,b
      C1997Z,FUTURE,253.3,a
      MSFT,STOCK,130.175,s
      
  4. JMU has subscribed to a clipping service that provides them with data about when the University is mentioned in a newspaper article or blog. Prof. Bernstein wants to use these data to find references to himself (which is not surprising given the size of his ego). Since it is a large amount of data, he wants you to be able to construct an object that can search through a portion of the data in its own thread of execution.

    Write a class named Searcher for this purpose. It will be passed a CopyOnWriteArrayList of String objects, each of refers to JMU. It must construct a synchronized List to hold the String objects that refer to Prof. Bernstein.

    The Searcher object must search through the CopyOnWriteArrayList for String objects that contain "Bernstein". It must use 10 different threads of execution to conduct the search, each of which must search through a distinct portion of the CopyOnWriteArrayList. Each time such a String is found it must be added to the synchronized List.

    The Searcher object must have a start() method that starts the process (and returns immediately). It must also have a getResults() method that returns the synchronized List when all of the threads have completed their work, blocking until that is the case.

  5. In the previous question:
    1. Why was CopyOnWriteArrayList an appropriate choice for the List of String objects? What additional thread-safety does it provide over a String[]? How necessary is that additional safety in this case?
    2. Why is it necessary to use some form of thread-safe List for the results? How does a synchronized List provide that thread-safety? What other kind of thread-safe Collection might be used instead?
  6. Given the following implementation of the Printer class:
    public class Printer implements Runnable
    {
        private String    s;
    
        public Printer(String s)
        {
            this.s = s;
        }
    
        public void run()
        {
            System.out.println(s);
        }
    }
    

    list all possible outputs for each of the following fragments (assuming that no threads are interrupted).

    1.         Thread a, b;
      
              a = new Thread(new Printer("A"));
      
              try
              {
                  a.join();
                  System.out.println("M");
              }
              catch (InterruptedException ie)
              {
                  // Shouldn't get here
              }
      
    2.         Thread a, b;
      
              a = new Thread(new Printer("A"));
              a.start();
      
              try
              {
                  a.join();
                  System.out.println("M");
              }
              catch (InterruptedException ie)
              {
                  // Shouldn't get here
              }
      
    3.         Thread a, b;
      
              a = new Thread(new Printer("A"));
              a.start();
      
              b = new Thread(new Printer("B"));
      	b.start();
      
              try
              {
                  a.join();
                  System.out.println("M");
              }
              catch (InterruptedException ie)
              {
                  // Shouldn't get here
              }
      
    4.         Thread a, b;
      
              a = new Thread(new Printer("A"));
              a.start();
      
              b = new Thread(new Printer("B"));
      	b.start();
      
              try
              {
                  a.join();
                  b.join();
                  System.out.println("M");
              }
              catch (InterruptedException ie)
              {
                  // Shouldn't get here
              }
      
  7. Explain the most important differences between monitors (i.e., implicit locks) and explicit locks.
  8. Explain the most important difference between barriers and latches.
  9. Explain the most important difference between barriers and joining (i.e., using the join() method).
  10. Explain the primary benefit of executors.
  11. List two important capabilities objects implementing the Callable interface have over objects implementing the Runnable interface.
  12. Text I/O
  13. Explain the difference(s) between channel-based I/O and stream-based I/O.
  14. Explain the roles of the File class and the Files class.
  15. Write a class named ClipOrganizer that is given a file name, reads all of the "lines" in that file (each of which is no more than 80 characters long and is terminated by the operating system's line termination character(s)), and constructs a List of String objects in each of the following ways.
    1. Using a BufferedReader.
    2. Using a convenience method in the Files class.
    3. Using a SeekableByteChannel.
  16. Networking
  17. Carefully explain:
    1. What UDP "adds" to IP.
    2. Two differences between TCP and UDP.
    3. Why HTTP 1.0 is said to be a stateless protocol.
    4. What it means when we say that a protocol is connection-oriented and how this concept is related to I/O streams.
    5. What it means when we say that a protool is synchronous/asynchronous.
    6. What it means when we say that a protocol is streaming.
  18. Create a UML state diagram for that describes the dynamic behavior of a simple, single-threaded, non-caching HTTP 1.0 server. Your diagram must include an AcceptingConnection state, a Reading state, and a Transmitting state. It may include other states as well.
  19. Explain why the Domain Name System (DNS) is considered an application layer system.
  20. Explain the difference between connection-oriented and connectionless transport layer protocols. Is this the same as the differene between stream-oriented and message-oriented protocols?
  21. Compare and contrast UDP, TCP and SCTP.
  22. When we discussed TCP we used a state diagram. When we discussed UDP we didn't. Why?
  23. Text Transport
  24. Write an application that repeatedly reads a "text" basketball score from standard input and transmits it to a list of receivers using UDP. The list of receivers must be read from a file when the application is started.
  25. Write a server that responds to TCP connections on port 8462 by sending the title of the "movie of the day". Your server must use the (hypothetically) existing MovieMarketer class. This class has a single method with the following signature:

      public static String getMOTD()

    that returns the "movie of the day". The server need only handle one connection at a time.

  26. One way to implement "weak access control" in a client-server application is to hard-code a series of prompts and replies. Implement a TCP server and client (in Java) that use the following protocol.

      If the client receives a prompt of "Net:" it must respond with a reply of "Sandra". If the client receive a prompt of "Fish:" it must respond with a reply of "swordfish". If the client receives any other prompt it must reply with a string that contains 12 characters, two of which must be the null character (i.e., '\0') followed by the ampersand character (i.e., '&'). The client will always be prompted exactly twice, but the prompts can occur in any order.

      The server must generate exactly two prompts, one of which must be "Net:" or "Fish:", the other of which must be something else. It must generate two random numbers, one to determine which prompt is first and the other to determine whether "Net:" or "Fish:" is used. The server must be able to handle multiple connections at the same time.

  27. Indicate whether each of the following statements is true or false:
    (1) _____ In Java, the Socket and ServerSocket classes use a SocketImplFactory to facilitate customization.
    (2) _____ In Java, the DatagramSocket class is declared final.

Copyright 2020