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. ______ d = k;
    2. ______ d = i;
    3. ______ k = d;
    4. ______ k = i;
    5. ______ c = sc;
    6. ______ sc = c;
    7. ______ MyAbstractClass myAC;
    8. ______ MyInterface myIF;
    9. ______ MyClass myC;
    10. ______ c = new MyAbstractClass();
    11. ______ c = new MyInterface();
    12. ______ c = new MyClass();
    1. ______ sc = new MyAbstractClass();
    2. ______ sc = new MyInterface();
    3. ______ sc = new MyClass();
    4. ______ d = foo();
    5. ______ d = bar();
    6. ______ d = baz();
    7. ______ int foo() { return 5; }
    8. ______ int foo(int x) { return x; }
    9. ______ int bar() { return 5; }
    10. ______ int bar(int x) { return x; }
    11. ______ int baz() { return 5; }
    12. ______ 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("^", "");
      
      
    int i = s.indexOf(",");
      
      
    String first = s.substring(1, i);
      
      
    int number1 = new Integer(first).intValue();
      
      
    String second = s.substring(i + 1, s.length() - 2);
      
      
    int number2 = new Integer(second).intValue();
      
      
  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?



  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
     
     
         // Write the header for the main method
     
     
            // Declare an integer variable "number"
     
     
            // Set the number to a value gotten from the command line (args[0])
     
     
            // Begin a loop whose test is at the bottom
     
     
                // Print the current value of "number"
     
     
                // If the number is even...
     
     
                    // Divide it by 2
     
     
                // But if the number is odd...
     
     
                   // Multiply it by 3 and subtract 1
     
       
          // Exit the loop if the number is 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 ____________________

    2. an interface ____________________

    3. a method ____________________

    4. a static variable ____________________

    5. a final variable ____________________

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

    what does the second line do?



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


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

  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);

    Object o = root("3.1416");

    float f = root(3/5);

    long n = root(12L);

    double dd = root(555L);

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

    Object root(Object y) {...}

    String root(String z) {...}

    int root(long m) {...}

    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...?


  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. _____ bar() is a static method of Foo.

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

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

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

  12. (8 points) True or false:

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

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

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

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

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

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

    7. _____ A private constructor can never be used.

    8. _____ 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. _______________ 5 << 2

    2. _______________ 5 & 3

    3. _______________ 5 && 3

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

    5. _______________ 1 < 2 ? 3 : 4


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




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

    1. shadowing (a variable)



    2. actual parameter



    3. scope



    4. overloading



    5. casting