Chris Pollett>Old Classes>PIC 20, Winter 2000>Practice Midterm

Winter 2000 PIC 20 Practice Midterm

  1. Short answer.
    1. How may many times is hello printed by the following code?
      public class Hello
      {
         public static void main(String args[])
         {
            int j = 5;
            while ( j<5)
            {
               j++;
               System.out.println("hello");
            }
         }
      }
      
    2. How many times would it be printed if we replace while { ... } with
            do
            {
               j++;
               System.out.println("hello");
            } while  ( j<5);
      
      
      ?
    3. Give an example code fragment that uses a labelled break.
    4. Which of the following lines are legal Java declarations:
      MyClass[] isHappy, rightNow;
      MyClass givesGoodEvaluations[5];
      MyClass acesMidterms[];
      MyClass *isPayingAttention;
      
  2. The function gcd(m,n) is defined to be n if m=0 and gcd(m, n % m) otherwise. Write a recursive method to compute this function.
  3. The function pow2(0) is defined to be 1 and for x>0 an integer pow2(x) is pow2(x/2)*pow(x/2) if x is even and x*pow2(x/2)*pow(x/2) otherwise. Write a recursive method to compute pow2(x) that on input x uses at most 2 log_2 x multiplications.
  4. Consider the following code fragment:
    public class CalledHow
    {
      public static void main (String args[])
      {
    	FooClass a,c,d;
    	int b[] = {1,2,3};
    
    	a =new FooClass("hi there");
            d = new FooClass("hi");
    
            c=twiddle(a,d,b);
    
    
            System.out.println("a.getFoo()="+a.getFoo()+" b[0]="+b[0]);
    	System.out.println("c.getFoo()="+c.getFoo()+" d.getFoo()"+d.getFoo());
      }
    
      static FooClass twiddle( FooClass foo, FooClass foo2, int i[])
      {
          foo.setFoo("hello");
          foo2=foo;
          i[0]++;
          return foo2;
      }
    }
    
    class FooClass
    {
      String s;
    
      public FooClass(String string)
    	{
    	  s=new String(string);
    	}
    
      public String getFoo()
    	{
    	  return s;
    	}
    
      public void setFoo(String string)
    	{ s=new String(string);
    	}
    }
    
    
    What does each System.out.println(...) print? Explain your reasoning.
  5. Explain and give an example of what each of the following keywords does when placed at the start of a method definition: static, public, protected, private, final, and abstract.
  6. Write a short applet which has a button which when clicked causes hi there to be printed starting at location (50,50). Thereafter, if the button is clicked more times, the hi there toggles between being printed and not being printed.
  7. Two methods which are in class Object are toString and finalize. Explain with an example why you might what to override these methods.
  8. Explain with an example what a this reference does.
  9. Describe briefly what the advantage the class Vector has over using a linked list. How can a node in a linked list be represented in Java?
  10. Give a brief example of how to create packages in Java.