SDSU CS 596 Java Programming
Fall Semester, 1998
Midterm Exam Answers
To Lecture Notes Index
© 1998, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 19-Nov-98

CS 596 Midterm Exam - Fall 1998

Each problem is worth 10 points.
1. What is the difference between the “==” operator and the “equals()” method for String objects?

The "==" operator determines if two String objects are in the same memory location. That is if two String references point to the same object in memory. The "equals()" method determines if two String objects are equal character by character.

2. In writing a general purpose sort method in Java, the sort method needs to compare objects. There are two ways to give the sort method the code to compare objects. Briefly, describe the two ways.

One is to provide the sort method an object that can compare the types of objects to be sorted, like sdsu.compare.Comparer or JDK 1.2 Comparator.

The other is two insure that the objects to be sorted know how to compare themselves with other objects of the same type. JDK 1.2 does this by having classes implement the Comparable interface.

Doc 26, Midterm Exam Answers Slide # 2
3. Given class A and B below.

class A {
   public void bar() {
      System.out.println( "A" );
   }
}
class B extends A {
   public void bar() {
      System.out.println( "B" );
   }
}

Parts a and b refer to the following two lines of code:

      A test = new B();
      test.bar();

a. If all method access in Java were determined statically then what would the two lines of code above print out?

A

b. If all method access in Java were determined dynamically then what would the two lines of code above print out?

B

c. When is method access in Java determined statically?

There are three cases:

1. Static methods
2. private methods
3. methods accessed using super

Doc 26, Midterm Exam Answers Slide # 3
4. We have class A and class B, a subclass of A. Class A has a method called foo. Class B overrides the foo method. Briefly, what are the restrictions placed on the return type, the parameters and the exceptions thrown by the foo method in class B?

Return type must be the same as the return type in A's foo method

The parameters must be the same as the return type in A's foo method

All exceptions thrown by B's foo must be thrown by A's foo or subclasses of an exception thrown by A's foo.


5. Given the class definition of the class File below, what implications does this have for the file structure, compiling the class, and using the class in a program?

package sdsu.whitney.io;
public class File {
   //code not shown
}

1. The class must be in a file called File.java
2. The file File.java must be in a directory called io
3. The directory io must be in a directory called whitney
4. The directory whitney must be in a directory called sdsu
5. The directory containing the whitney directory must be in the classpath
6. The program that uses File must import sdsu.whitney.io.File or always refer to the class by its fullname.

Doc 26, Midterm Exam Answers Slide # 4
6. Circle the assignment statements, "Line 1" through "Line 10", in the following code that illegally access a field.

package Exam;
public class Parent {
   protected String protectedVar = "Protected";
   String packageVar = "Package";
}
package Exam;
public class Uncle {
   public  void  SampleAccess( Parent parameter )  {
      parameter.protectedVar  =  "Line 1";
      parameter.packageVar  =  "Line 2";
   }
}
package Quiz;
public class Aunt {
   public  void  SampleAccess( Exam.Parent parameter)  {
      parameter.protectedVar  =  "Line 3"; <- illegal
      parameter.packageVar  =  "Line 4";   <- illegal
   }
}
package  Quiz;
public class Child  extends  Exam.Parent {
   public  void  SampleAccess( Exam.Parent parentType,
                  Child  childType)  {
      parentType.protectedVar  =  "Line 5"; <- illegal
      parentType.packageVar  =  "Line 6"; <- illegal
      protectedVar  =  "Line 7";
      packageVar  =  "Line 8";  <- illegal
      childType.protectedVar  =  "Line 9";
       childType.packageVar  =  "Line 10"; <- illegal
   }
}

Doc 26, Midterm Exam Answers Slide # 5
7. In the following code, what are the values of a, b, c, and name when the System.out.println statement is executed.

a
0
b
not defined, random
c[0]
0 (or 0 0 0 0 0 for the entire array)
name
null

class DefaultValues {
   int a;
   String name;
   public void aMethod() {
      int b;
      int[] c = new int[5];
      System.out.println( "Start Here");
   }
}

8. What does it mean to say that the ListIterator is fail-fast?

If the list backing the iterator is changed without going through the list iterator, the first time the list iterator access an element in the list, it throws an exception.

Doc 26, Midterm Exam Answers Slide # 6
9a. What does the following code print out when we execute "new A()"?

a =
17
b =
14
c =
0
d =
17

class A {
   static int a = 0;
   int b = a++;
   {
      a = a + 2;
   }
   
   static int c = a;
   
   static {
      a = a + 5;
   }
   int d = a;
   public A() {
      System.out.println( "a =" + a );
      System.out.println( "b =" + b );
      System.out.println( "c =" + c );
      System.out.println( "d =" + d );
   }
   
   static {
      a = a + 9;
   }
}

Doc 26, Midterm Exam Answers Slide # 7
9b. Given the classes below, what will be the output of statement "new Child()"?

Answer:
In Parent Block
In Parent
In Child block
In Child

class Parent {
   public Parent() {
      System.out.println( "In Parent");
   }
   
   {
      System.out.println( "In Parent Block");
   }
}
class Child extends Parent {
   {
      System.out.println( "In Child block");
   }
   
   public Child() {
      super();
      System.out.println( "In Child");
   }
}


Doc 26, Midterm Exam Answers Slide # 8
10a. What will be the result of trying to compiling and executing the following program if FooException is properly defined as a subclass of java.lang.Exception?

The class ExceptionQuestion does not compile. An exception will be thrown, as it does not handle the checked exception in aMethod.

class ExceptionQuestion {
   public void aMethod() {
      System.out.println( "In aMethod" );
      throw new FooException();
   }
   public static void main( String[] args ) {
      try {
         System.out.println( "Start" );
         ExceptionQuestion x = new ExceptionQuestion();
         x.aMethod();
         System.out.println( "After method" );
      } catch ( FooException error ) {
         System.out.println( "In handler" );
      }
      System.out.println( "End" );
   }
}


Doc 26, Midterm Exam Answers Slide # 9
10b. Assume that the exception FooException is properly defined. What will be the output of compiling and executing the main method of the following class?
Answer:
Start
In aMethod
in first catch
Finally
After method
End

class ExceptionQuestionB {
   public void aMethod() throws FooException {
      try {
         System.out.println( "In aMethod" );
         throw new FooException();
      } catch ( FooException error ) {
         System.out.println( "in first catch" );
         throw new FooException();
      } finally {
         System.out.println( "Finally" );
         return;
      }
   }
   public static void main( String[] args ) {
      try {
         System.out.println( "Start" );
         ExceptionQuestionB x = new ExceptionQuestionB();
         x.aMethod();
         System.out.println( "After method" );
      } catch ( FooException error ) {
         System.out.println( "In handler" );
      }
      System.out.println( "End" );
   }
}

Copyright © 1998 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
All rights reserved.

visitors since 19-Nov-98