Midterm Solutions

  1. How would one create an array of String class objects
    (a) new String[10];
    (b) String s[10];
    *(c) String s[10] = new String[10];
    (d) both (b) and (c) are correct
  2. In inheritance a sub-class DOES NOT inherit what from the super-class
    (a) protected data/methods
    (b) public data/methods
    *(c) the super classes constructor
    (d) the super classes finalizer
  3. Given a class called House, if in some Java code we saw House.size( );
    clearly size( ) is a
    *(a) static method
    (b) static member data
    (c) static class
    (d) non-static class
  4. If a class has an instance variable called x and a while loop, within
    the class, has a local variable called x this would be an example of
    (a) an overriden variable
    *(b) a hidden variable
    (c) this is not possible, it wouldn't compile
    (d) the two x 's would be the same
  5. What is the duration of an instance variable
    (a) the entire execution time of the program
    *(b) the same as the duration of the class object it belongs to
    (c) it depends on its scope
    (d) as long as an object is instantiated
  6. Which keyword refers to access
    (a) final
    *(b) protected
    (c) static
    (d) extends
  7. Given a super-class Person and a sub-class Employee which would be correct
    *(a) Person a = new Employee( );
    (b) Employee a = new Person( );
    (c) Employee a = (Employee) new Person( );
    (d) both (a) and (c)
  8. A class cannot inherit from more than one super-class. How can this be overcame
    (a) it cannot
    *(b) one can use interfaces
    (c) in fact, a class can inherit from more than one super-class
    (d) by defining the class twice
  9. If we have the declaration Shape a[10]; then the references in this
    array may refer to
    (a) any object of sub-class of Shape
    (b) any object of type Shape
    (c) any object of a class that implements Shape
    *(d) all of the above
  10. What would be the output of the following code
    StringTokenizer t = new StringTokenizer("Hello, my name is Randolph!");
    System.out.println( t.nextToken(", ")+t.nextToken(", ") );
    (a) Hello, my name is Randolph!
    (b) HellomyRandolph!
    *(c) Hellomy
    (d) Hellomyname
  11. Which would return true given
    String string1 = "hello", string2 = "hello";
    (a) string1 == string2
    (b) string1.compareTo(string2)==true
    *(c) string1.equals(string2)
    (d) string1 = string2
  12. What keyword must be placed before a method definition so that it may
    not be overridden in sub-classes
    (a) final
    (b) static
    (c) final
    (d) protected
  13. Why are instance variables called instance variables
    (a) because all instances of the class share one copy of them
    (b) because they have method scope
    *(c) because each instance of the class has its own copy of them
    (d) because they can never be instantiated
  14. Suppose we have a class Shape with three constructors
    public Shape()
    public Shape(char a)
    public Shape(char a, int b)
    If we define a subclass that extends Shape and do not define a
    constructor for this subclass, which constructor of Shape will be
    explicitly called
    *(a) public Shape()
    (b) public Shape(char a)
    (c) public Shape(char a, int b)
    (d) one cannot tell
  15. What keyword should be used in the definition of a subclass to refer
    to a method or variable in the superclass
    (a) extends
    (b) final
    (c) implements
    *(d) super
  16. When does dynamic binding occur
    (a) when the Java code is written (i.e. typed)
    (b) when the Java code is compiled
    *(c) when the Java program is ran
    (d) after the execution of the Java program
  17. static methods may use
    (a) instance variables
    (b) local variables
    (c) static variables
    *(d) both (b) and (c)
    For the remaining questions consider the following code
    public class Alien
      {
      String nameOfAlien;
      public Alien(String name)
        {     
        setNameOfAlien(name);
        }
      public String getNameOfAlien()
        {
        return nameOfAlien;
        }
      public void setNameOfAlien(String name)
        {
        nameOfAlien=name;
        }
      }
    
    public class Martian extends Alien
      {
      double martianMoney; 
      public Martian(String name, double money)
        {
        super(name);
        setMartianMoney(money);
        }
      public void setMartianMoney(double money)
        {
        martianMoney=money;
        }
      public double getMartianMoney()
        {
        return martianMoney;
        }
      }
  18. Given
    Alien a = new Martian("Harry",23000);
    which is legal
    (a) a.getMartianMoney()
    (b) a.super.setAlienName("Susan")
    *(c) ( (Martian) a).getMartianMoney()
    (d) none of the above
  19. If we define a class CalifornianMartian that extends Martian how
    do we make the constructor of CalifornianMartian directly call the
    constructor of Alien
    *(a) we cannot
    (b) super("Harry");
    (c) super.super("Harry")
    (d) Alien.super("Harry")
  20. If we delete the line of code
    super(name)
    from the definition of the constructor for Martian why will it not
    compile because
    *(a) there is not an Alien constructor that accepts no argument
    (b) we always must explicitly call the superclass constructor
    (c) the instance variable name would not be initialized
    (d) none of the above
  21. (a)Define an abstract class called Shape. Have an instance variable
    that keeps track of how many Shape objects are currently instantiated
    (define the constructor and finalizer aproprietly). Have an abstract
    toString method and a getNumberOfShapes method that can be called as
    Shape.getNumberOfShapes() and that can not be overriden.
    public abstract class Shape
      {
      private static int numberOfShapes;
      public Shape()
        {
        numberOfShapes++;
        }
      public static int getNumberOfShapes()
        {
        return numberOfShapes;
        }
      public abstract String toString();
      protected void finalizer()
        {
        numberOfShapes--;
        }
      }
    (b)Define a subclass of Shape called Rectangle. Its toString method should
    return "Rectangle". Define Rectangle in such a way that it cannot be extended
    (be a superclass).
    public final class Rectangle extends Shape
      {
      public String toString()
        {
        return "Rectangle";
        }
      }
  22. Write a class called Editor that has a main method. The main method
    should allow the user to enter in a sentence in one JOptionPane. The
    words of the sentence should then be displayed in a JTextArea with spaces
    but no punctuation. For instance the sentence
    Hello, my name is Randolph.

    is displayed as
    Hello my name is Randolph

    The possible punctuation signs are . and , and ?
    public class Editor
      {
      public static void main(String args[])
        {
        String sentence;
        StringTokenizer tokens;
        JTextArea list = new JTextArea(20,20);
        sentence = JOptionPane.showInputDialog("Enter a sentence");
        tokens = new StringTokenizer(sentence);
        while(tokens.hasMoreTokens())
          {
          list.append(tokens.nextToken(" ,?.")+"\n");
          }
        JOptionPane.showMessageDialog(null,list,"",JOptionPane.INFORMATION_MESSAGE);
        }
      }
  23. Suppose we have a class Word that has the constructor
    public Word(String aWord).
    Define a class Sentence that has a main method. The main should have an
    Word array of size 4. The array should store the words: "one", "two",
    "three", "four". The array should then be displayed on a JTextArea
    public class Sentence
      {
      public static void main(String args[])
        {
        Word words[] = new Word[4];
        JTextArea list = new JTextArea(20,20);
        words[0] = new Word("one");
        words[1] = new Word("two");
        words[2] = new Word("three");
        words[3] = new Word("four");
        for(int j=0; j<4; j++)
          list.append(words[j].toString()+"\n");
        JOptionPane.showMessageDialog(null,list,"",JOptionPane.INFORMATION_MESSAGE);
        }
      }