CS 122   Winter 1999,  Instructor:  Jeffrey Horn     FINAL EXAM     SAMPLE QUESTIONS


GENERAL INTRUCTIONS:

  1. Open book, open notes, internet, web, etc.  (E XCEPT "open neighbor"!)
  2. SHOW your WORK!!! as much as possible

 GENERAL JAVA
 
What value would each of these return?
 
 
  return  8 > Math.rand();
  public  String  theAnswer() {  int x = 42;    return ""+ x;}
   public  String  theQuestion0() {  int x = 42;    return "x";}
public boolean theQuestion1(boolean toBe) {return toBe || !toBe ;}
 public boolean theQuestion2(boolean toBe) {return toBe && !toBe ;}
public boolean theTruth(boolean toBe) 
                             {return theQuestion1(toBe)   || theQuestion2(toBe);}
          Why won't this code compile?
 

                  class simple
                      {

                          public String greeting(boolean happy)
                                  {
                                     if(happy)  return "Yo Ho!";
                                    return "bugger off!"
                                    }
                        }

INTERFACES

Given the following interface hierarchy:
   interface Warrior
               {
                  public int get_Strength();
                  public int get_Intelligence();
                  public void reduce_hit_points(int h);
                  public boolean are_you_dead_yet();
                }
  interface Fighter extends Warrior
               {
                  public WeaponType getWeapon();
                }
  interface Magic_User extends Warrior
               {
                  public int get_Magic_Resistance();
                  public void conduct_spell_dual(Magic_User m);
                  public boolean learn_new_spell(Spell s);
                }
 interface Druid extends Magic_User
               {
                  public boolean Can_Heal?(Warrior w);
                }


Answer these questions:
 

  1. Draw out the hierarchy of interfaces as a tree, with arrows indicating which interfaces extend which interfaces.
  2. List all of the methods that a Magic_User must implement.
  3. Can a Druid learn a spell?  (why or why not?)
  4. Let's say someone designs a class, called "Apprentice", that implements Magic_User.  Do you know if an Apprentice (that is, an object that implements the Apprentice class) can heal a warrior?  Is it possible?  Why or why not?
  5. Let's say we want to create a "Barbarian" class.  Barbarians are warriors with no weapons (very primitive).  Does the above hierarchy allow the creation of a class that implements just the warrior interface?  Why or why not?
  6. Write the syntax for implementing a class "Elf" that implements as many of the above interfaces as possible.

 USING SUN's JAVA site
    Goto http://java.sun.com/products/jdk/1.2/docs/api/index.html\ Answer the following questions about the Java 1.2 platform:
     
    1. Write down ALL of the interfaces  in the java.lang package.
    2. How many classes in Java 1.2 are known to implement the "Shape" interface?
    3. Which interfaces does the "LinkedList" class implement?
    4. From what other classes does LinkedList inherit methods?
    5. Name TWO methods of LinkedList that are implemented by ALL Java objects.
    6. What is the return type of LinkedList's "getClass" method?
    7. Since what version of Java has the class "Object" been around?