CS670K Homework Set 5 - Fall 1998

Due: Tuesday, December 8

© Copyright University of New Haven 1998

  1. What is printed in the following program?
         class Ball      extends Exception  { int id;  Ball(int i)   { id= i;  } }
         class BeachBall extends Ball       {     BeachBall(int i) { super(i); } }
    
         class X
         {
    	     static void f() throws BeachBall { throw new BeachBall(2); }
    	     static void g() throws Ball
    	     {           
    	           try             { f();  throw new RuntimeException(); }
    	           catch(Ball   b) { System.out.println("B" + b.id); }
    	           catch(RuntimeException r) { System.out.println("R"); }
    	           finally         { System.out.println('G'); }
    
    	           throw new BeachBall(4);
    	     }
    
    	     public static void main(String s[])
    	     {
    	          try
    	          {
    	             int i;
    
    	             for (i= 2;  i >= 0;  --i)
    	                try      { if (i==2) { f(); g(); }  else { g(); f(); }  }
    	                catch(BeachBall bb) { System.out.println("BB" + bb.id); }
    	                catch(RuntimeException r)   { System.out.println("RR"); }
    	                finally             { System.out.println("F" + 2/i);    }
    	          }
    	          catch(Ball   b)           { System.out.println("BBB" + b.id); }
    	          catch(RuntimeException r) { System.out.println("RRR"); }
    	          finally                   { System.out.println('M'); }
              }
         }
    
  2. Give ALL possible outputs of the following program:
    	class X extends Thread
    	{
    		int id;
    
    		X(int i) { id= i; }
    	
    		public void run()
    		{
    			while (--id > 0)
    			{
    				System.out.print(id);
    				new X(id).start();
    			}
    		}
    
    		public static void main(String s[]) { new X(4).start(); }
    	}
    
Home Page