exit final exam

NAME

 
CSE 214: Final Term Exam
 
Tu, Dec 9, 3:30—5:18
Open Notes, Open Books
The exam consists of eight problems
Answers not appearing in designated spaces WILL NOT be graded

Problem #1 (10 points)
Show the output of the following program.
import java.util.Stack; 
class Program { 
  public static void main(String [] args){ 
     Stack <String> v = new Stack <String> (); 
     v.push( ”aaa” ); 
     v.push( ”bbb” ); 
     v.push( ”ccc” ); 
     System.out.println( v.peek() ); 
     v.push( ”ddd” ); 
     System.out.println( v.pop() ); 
     v.push( ”eee” ); 
     System.out.println( v.peek() + ”   ” + v.pop() ); 
     System.out.println( v.peek() + ”   ” + v.pop() ); 
} }
Problem #2 (10 points)
Show the output of the following program.
class Program { 
   public static void main(String [] args){ 
      Helper a = new Helper( 1 ), 
             b = new Helper( 2 ); 
      a.help( b ); 
      b.help( a ); 
      a.help( a ); 
      b.help( b ); 
}  } 
class Helper { 
     int value; 
   Helper( int v ){ 
     value = v; 
   } 
   void help( Helper h ){ 
      value = value + h.value; 
      System.out.println( value ); 
}  }

Problem #3 (10 points)
Show the output of the following program.
class Program { 
   public static void main(String [] args){ 
      Helper a = new Helper( 1 ), 
             b = new Helper( 2 ); 
      Helper.help( b ); 
      Helper.help( a ); 
      Helper.help( a ); 
      Helper.help( b ); 
}  } 
class Helper { 
     static int value; 
   Helper( int v ){ 
     value = v; 
   } 
   static void help( Helper h ){ 
      value = value + value; 
      System.out.println( value ); 
}   }
Problem #4 (10 points)
Show the output of the following program.
class A{ 
  String name = ”my name is A”; 
  String getName(){ return name; } 
} 
class B extends A{ 
  String name = ”my name is B”; 
  String getName(){ return name; } 
} 
class C extends A{ 
  String name = ”my name is C”; 
  String getName(){ return name; } 
} 
class Program{ 
   public static void main (String [] args){ 
      A a = new A(); 
      B b = new B(); 
      C c = new C(); 
      System.out.println( a.name +  ”  ” + a.getName() ); 
      a = b; 
      System.out.println( a.name +  ”  ” + a.getName() ); 
      System.out.println( b.name +  ”  ” + b.getName() ); 
      System.out.println( c.name +  ”  ” + c.getName() ); 
}  }

Problem #5 (10 points)
Show the output of the following program.
class Program{ 
   public static void main(String [] args){ 
      Pet dog = new Pet(), 
          cat = new Pet(); 
      Food cookie = new Food( ), 
           chocolate = new Food(); 
      Person dan = new Person(), 
             diana = new Person(); 
      dan.buy(chocolate, 10); 
      dan.buy(cookie, 5); 
      diana.buy(chocolate, 2); 
      dog.get( cookie ); 
      dog.eat(); 
      cat.get( chocolate ); 
      cat.eat(); 
      cat.eat(); 
      cookie.show(); 
      chocolate.show(); 
      dog.show(); 
      cat.show(); 
}  } 
class Food { 
     int count = 0; 
   void add( int amount ){ count += amount; } 
   void show(){ 
      System.out.println( count + ” servings ”); 
}  } 
class Pet { 
     int count; 
     Food jar; 
   void get(Food junk){ jar = junk; } 
   void eat(){ jar.add(-1); count++; } 
   void show(){ 
      System.out.println( 
         ”ate ” + count + ” servings”); 
}  } 
class Person { 
   static int count = 0; 
   void buy(Food junk, int amount){ 
      junk.add( amount ); 
}  }

Problem #6 (10 points)
Write a program that prints 20 different randomly generated integer values smaller than 1000. The program is not allowed to use arrays.

Note. The random number generator may be invoked more than 20 times. For instance, an output ‘1 2 4 3 5 7 6 8 9 10 11 12 13 14 15 16 17 18 19 20’ may result from the numbers ‘1 2 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 20’.

Problem #7 (10 points)
Provide the missing code so that the given program will compile with no error.
class Program { 
   public static void main(String [] args){ 
      Helper a = new Helper(), 
             b = new Helper( 2 ); 
      a.one( b ); 
      Helper.three( a ); 
      String s = a.two( a, b ); 
      System.out.println( s ); 
}  }

Problem #8 (10 points)
The given incomplete program should produce the following output.
10  
a  
0  
1a

Provide the missing code of the program.

class Program{ 
   public static void main(String [] args){ 
     Hex a = new Hex(); 
     Hex b = new Hex( 10 ); 
     Hex c = new Hex( ”10” ); 
     System.out.println( c ); 
     System.out.println( b ); 
     System.out.println( a ); 
     a.plus( b ); 
     a.plus( c ); 
     System.out.println( a ); 
}  }

Note. The java.lang.Integer class offers the following pair of possibly useful methods.

          static String   toHexString(int i) 
          static int      parseInt(String s, int radix)