CIT591 Midterm Exam
Fall, 2004
Name_________________________________________

Please keep all your answers short and to the point. Do not provide extra information that was not asked for.

  1. (24 points) Suppose:
        MyAbstractClass is an abstract class that defines int k and int foo();
         MyInterface is an interface that declares int bar();
         MyClass is a (non-abstract) class that extends MyAbstractClass, implements MyInterface,
                    and defines double baz()
         MySubclass extends MyClass and declares int i, double d, MyClass c, and MySubclass sc

    The following declarations and statements all occur in some method of MySubclass. Write BAD next to illegal statements/declarations, OK next to the ones that are could be correct.
    1. _OK__ d = k;
    2. _OK__ d = i;
    3. _BAD_ k = d;
    4. _OK__ k = i;
    5. _OK__ c = sc;
    6. _BAD_ sc = c;
    7. _OK__ MyAbstractClass myAC;
    8. _OK__ MyInterface myIF;
    9. _OK__ MyClass myC;
    10. _BAD_ c = new MyAbstractClass();
    11. _BAD_ c = new MyInterface();
    12. _OK__ c = new MyClass();
    1. _BAD_ sc = new MyAbstractClass();
    2. _BAD_ sc = new MyInterface();
    3. _BAD_ sc = new MyClass();
    4. _OK__ d = foo();
    5. _OK__ d = bar();
    6. _OK__ d = baz();
    7. _OK__ int foo() { return 5; }
    8. _OK__ int foo(int x) { return x; }
    9. _BAD_ int bar() { return 5; }
    10. _OK__ int bar(int x) { return x; }
    11. _BAD_ int baz() { return 5; }
    12. _OK__ int baz(int x) { return x; }

  2. (12 points) The following code consists of a sequence of assignment statements. After each statement, tell what value has just been assigned.
    String s = "^(^12^3^,-4^56^^)".replaceAll("^", "");  "(123,-456)"
      
      
    int i = s.indexOf(",");  4
      
      
    String first = s.substring(1, i);  "123"
      
      
    int number1 = new Integer(first).intValue();  123
      
      
    String second = s.substring(i + 1, s.length() - 2);  "-45"
      
      
    int number2 = new Integer(second).intValue();  -45
      
    
  3. (3 points) Assuming that the programmer wanted to extract the two numbers, there is a logic error in the above code. How would you correct it?

    Change -2 to -1 in the next to last line

  4. (12 points) In the following you will write a complete, syntactically correct program, one or two lines at a time, according to the comments.
    // Declare the class
    class Question4 {
     
         // Write the header for the main method
         public static void main(String[] args) {
     
            // Declare an integer variable "number"
            int number;
     
            // Set the number to a value gotten from the command line (args[0])
            number = new Integer(args[0]).intValue();
     
            // Begin a loop whose test is at the bottom
            do { 
     
                // Print the current value of "number"
                System.out.println(number);
     
                // If the number is even...
                if (number % 2 == 0) {
     
                    // Divide it by 2
                    number = number / 2;
     
                // But if the number is odd...
                } else {
     
                   // Multiply it by 3 and subtract 1
                   number = 3 * number - 1;
                }
          // Exit the loop if the number is 1
          } while (number != 1);
     
    // End the method and the program
        }
    }
    
  5. (5 points) Suppose you wanted to name something "upper limit." Write this name, using capitals and underscores as appropriate, if it is the name of...

    1. a class ___UpperLimit____

    2. an interface ___UpperLimit____

    3. a method ___upperLimit____

    4. a static variable ___upperLimit____

    5. a final variable __UPPER_LIMIT__

  6. (3 points) In the following legal code segment:
         private Fiddle(int x) {
             super(x + 1);
             . . . }

    what does the second line do?
    Calls the superclass constructor with the argument x+1

  7. (2 points) Which two wrapper classes have longer names than their primitive counterparts?
    Integer and Character

  8. (2 points) In hexadecimal, what is the next number after 39FF ?   3A00

  9. (6 points) Draw a line from each call to the method that it uses; cross out any illegal calls:

    double d = root(3.1416); X

    Object o = root("3.1416"); 3

    float f = root(3/5); 5

    long n = root(12L); 4

    double dd = root(555L); 4

    Float ff = root(new Float(2.5)); X
    1 float root(float x) {...}

    2 Object root(Object y) {...}

    3 String root(String z) {...}

    4 int root(long m) {...}

    5 long root(int i) {...}
     

  10. (2 points) Of the four kinds of inner classes, member classes and anonymous classes are the most useful, because they can...?
    access the instance variables of the enclosing class

  11. (4 points) Suppose you find the (legal) statement int n = Foo.bar().length(); in a program. Classify each of the following statements as true (T), probably true (P), unlikely (U), or false (F):

    1. __P__ bar() is a static method of Foo.

    2. __P__ bar() is defined in class Foo.

    3. __F__ Foo.bar() returns a String as a result.

    4. __P__ Foo.bar(x).length() returns an int result.

  12. (8 points) True or false:

    1. __T__ Classes, methods and instance variables can be declared final.

    2. __F__ An interface can contain only public abstract methods, and nothing else.

    3. __T__ protected classes are accessible from more places than "package" classes.

    4. __T__ If m is an int[], then m instanceof Object is true.

    5. __F__ A .java file may contain only one class.

    6. __T__ A .java file may contain only one public class.

    7. __F__ A private constructor can never be used.

    8. __F__ A private class can never be used for anything.

  13. (5 points) What is the result of each of the following expressions? (Some may be illegal.)

    1. ____20________ 5 << 2

    2. _____1_________ 5 & 3

    3. ____illegal_____ 5 && 3

    4. ____true_______ "abc" == "a" + "bc"

    5. ____3_________ 1 < 2 ? 3 : 4


  14. (2 points) Are private instance variables inherited by subclasses? Briefly defend your answer.

    Yes, because those variables in objects of the subclass can be accessed by methods in the superclass.


  15. (10 points) Briefly define each of the following:

    1. shadowing (a variable)

      Declaring a variable with the same name as an inherited variable

    2. actual parameter

      A parameter in the call to a method, as opposed to one in the method header

    3. scope

      The part of the program in which a name can be accessed

    4. overloading

      Declaring two methods with the same name in the same class but with different signatures

    5. casting

      Specifying that a value is to be treated as, or converted to, a different type