Java Language Assessment

Instructions:

Answer the following questions without referencing any Java programs or reference materials. If you do not know the answer to a question, leave it blank.

 This assessment serves a dual purpose. First, it helps us to gauge the level of your language experience at the onset of the ObjectSpace interview process. Secondly, it alludes to our commitment to identify people that are best qualified for the challenging and exciting positions that are available at ObjectSpace. You are strongly encouraged to complete this assessment without any assistance in order to help both you and us get an accurate picture of your basic skills.

 Once you have completed the test, send the test along with your answers to:

ObjectSpace, Inc.

Recruiting

14881 Quorum Drive, Suite 600

Dallas, TX 75240

Fax: (972) 715-9081

hr@objectspace.com

 

Candidate Name: Hasan Timucin Ozdemir _

Candidate Phone Number: (315) 428 8668 _

Date: Apr. 20th, 1999 _

Recruiter Name: Lori Davis _

 

Multiple Choice

For each of the following questions, select the one answer which best applies.

 

1. (1 point) What will be the output of running the following code?

 

public class A {

String name = "Bob";

A(){ String name = "Frank"; }

String getName() { return name; }

public static void main( String[] args ) {

System.out.println ( new A().getName() );

}

}

 

    1. Frank
    2. Bob (*) (name in constructor is local variable)
    3. A
    4. This example throws an exception or won’t compile
    5. none of the above

2. (1 point) Is the following legal?

interface Harpo { double vision(); }

 

interface Foreigner { double vision(); }

 

class Combined implements Harpo, Foreigner {

public double vision() { return 42.0; }

}

 

    1. Yes (*)
    2. No

3. (1 point) What must be true for a class to be serializable?

 

  1. nothing, all classes are already serializable
  2. the class must implement the Serializable but need not implement any additional methods.
  3. the class must implement the Serializable interface and implement readExternal() and writeExternal() (*) (I think these methods should be readObject() and writeObject() instead of Externalizible(extends Serializable) methods)
  4. the class must implement the Streamable interface and implement streamIn() and streamOut()

4. (1 point) What type must you use in place of "???" for the following example to work?

interface Harpo { float vision(); }

 

interface Foreigner { double vision(); }

 

class Combined implements Harpo, Foreigner {

public ??? vision() { return 42.0; }

}

    1. float
    2. long
    3. Object
    4. double
    5. none of the above (*)

5. (1 point ) Which of the following modifiers must be used in place of "???" to prohibit access to "field" from outside of Bar?

class Bar {

??? int field;

}

    1. Nothing
    2. protected
    3. final
    4. private (*)
    5. none of the above

6. (1 point) What happens if an unchecked exception is thrown from a finalize () method?

    1. The exception is ignored. (*)
    2. All exceptions in finalize() must be caught, so this may not occur.
    3. The program prints an error message and terminates, as with any other uncaught exception.
    4. The Cubs will win a pennant.
    5. None of the above.

7. (1 point) What may be true of class Timer for the following to work:

 

Timer timer = new Timer();

Thread thread = new Thread( timer );

thread.start();

 

    1. Timer extends Thread
    2. Timer implements Runnable (*)
    3. Timer does not implement Serializable
    4. a or b
    5. a or b, and c

 

8. (1 point) What method(s) are declared by the Serializable interface?

    1. public void serialize();
    2. public void serialize( OutputStream out ); and public void deserialize( InputStream in )
    3. public void readFrom( InputStream in ); and public void writeTo( OutputStream out );
    4. no methods are declared by the Serializable interface
    5. none of the above (*)

9. (1 point) What does the following code output?

Vector v = new Vector();

v.addElement( 10 );

v.addElement( 20 );

System.out.println( v );

 

    1. Vector( 10, 20 )
    2. 20]
    3. 20}
    4. throws exception
    5. won’t compile (*)(no main)

10. (1 point) We want to iterate through a Vector, v, that contains multiple objects. Which of the following will do this?

a) Enumeration e = v.getElements();

while( e.hasMoreElements() )

System.out.println( v.nextElement() );

 

b) Enumeration e = v.elements();

while( e < v.size() )

System.out.println( v.nextElement() );

 

(*) c) Enumeration e = v.elements();

while( e.hasMoreElements() )

System.out.println( e.nextElement() );

 

d) Enumeration e = new Enumeration( v );

while( e.hasMoreElements() )

System.out.println( e.nextElement() );

 

e) none of the above

11. (1 point) Which container class is not included in the 1.1.1 JDK?

    1. Vector
    2. Dictionary
    3. Stack
    4. (*) Queue
    5. Hashtable

 

12. (1 point) Considering the following code, what happens when B is run?

 

class A {

int a;

A() { init(); }

void init() { System.out.print( "A" ); }

}

 

class B extends A {

int b;

B() { init(); }

void init() { System.out.print("B"); }

 

public static void main( String[] args ) { new B(); }

}

 

    1. "B" is printed to the console
    2. "AB" is printed to the console (*)
    3. "BB" is printed to the console
    4. an exception is thrown
    5. the code will not compile

13. (1 point) What has to be true for an inner class to reference a local variable?

    1. The local variable must be declared static.
    2. The local variable must be declared public.
    3. The local variable must be declared final.
    4. none of the above (*)

14. (1 point) Consider the following code:

import java.util.*;

class B {

static Vector v = new Vector();

//other stuff here

 

public void finalize() {

v.addElement( this );

}

}

 

The intent of this code is to prevent any B from ever being Garbage Collected. Comment on this.

  1. It will work as intended.
  2. It won’t work, the B’s memory has already been reclaimed and finalize() is only being called for cleanup purposes.
  3. It won’t compile, new references to this cannot be created inside of finalize() because the memory has already been reclaimed.
  4. It won’t work because static members cannot be called inside of finalize().
  5. None of the above.

The following code is used for the next two questions:

Frame frame = new Frame();

GridBagLayout layout = new GridBagLayout();

frame.setLayout( layout );

GridBagConstraints gbc = new GridBagConstraints();

gbc.anchor = GridBagConstraints.CENTER;

gbc.gridwidth = 2;

Button button = new Button( "Hello" );

 

frame.add( button );

frame.show();

15. (1 point) In order to have the constraints apply to the button, what should appear in the blank line?

    1. gbc.setConstraints( frame, button );
    2. frame.setConstraints( button );
    3. gbc.setConstraints( button, frame );
    4. (*) frame.setConstraints( button, gbc );
    5. layout.setConstraints( button, gbc );

16. (1 point) Assume that the grid width is to be 2. What code needs to be added to make the button stretch across the entire screen?

  1. gbc.weightx = 1.0;
  2. gbc.fill = GridBagConstraints.HORIZONTAL;
  3. (*) none, it already fills both cells
  4. a and b
  5. none of the above

All That Apply

For each of the following questions, select all the answers which are true.

 

17. (2 points) Which of the following are part of the Java threading model?

  1. notifyAll() (*)
  2. interface Serializable
  3. synchronized (*)
  4. class Thread (*)
  5. interface Runnable (*)

18. (2 points) Considering the following, which of the following expressions evaluate to true?

 

String s1 = "string";

String s2 = s1;

String s3 = new String( s1 );

 

    1. s1 == s2
    2. s1 != s2 (*)
    3. s1.equals( s2 ) (*)
    4. !s1.equals( s2 )
    5. s3 == s1
    6. s3 != s1 (*)
    7. s3.equals( s1 ) (*)
    8. !s3.equals( s1 )

Written

For each of the following questions, provide a written answer. Make sure to answer each part of a question. Be careful not to confuse "how" answers with "why" answers.

 

19. (2 points) Is an object's finalize() method guaranteed to be called? Why or why not?

Nope. GC determines when to collect garbage.

20. (2 points) What are the methods used for Applet lifecycle control?

init(), start(), stop(), and destroy().

21. (2 points)Using the following code

class Foo {

public void tryIt( int j ){

try {

try {

int i = 9;

int k = i/j; // throws ArithmeticException, a subclass of RuntimeException

throw new IOException( "Uh OH!" );

}

catch( IOException exception ) {

System.out.println( "Hi-ho, hi-ho, I just threw IO! " );

}

finally {

System.out.println( "Whew! Finally!" );

}

}

catch( ArithmeticException exception ) {

System.out.println( "Divide By Zero! " );

}

}

}

 

What would be the output of:

 

Foo foo = new Foo();

foo.tryIt( 0 );

foo.tryIt( 1 );

 

  1. (Divide-by-zero, therefore run finally) -> System.out.println( "Whew! Finally!" );
  2. (catches-ArithmeticException ) -> System.out.println( "Divide By Zero! ");
  3. (j>0, execute throw and catch IOExc.) -> System.out.println( "Hi-ho, hi-ho, I just threw IO! " );
  4. (execute finally again) -> System.out.println( "Whew! Finally !" );

22. (2 points) Write an example of using an inner class. Why are inner classes useful? Where are they commonly used?

 

  public class A extends JTable {

    public A() {} 

    ....

    class Student {
      String name;
      Student(String name) { this.name; }
    }
  }
Prevents name clashing and extensive number of files if the required process and object is local. It gives narrower scope than package and can be put into the definition of class where it is being used. As above, if the Student object is only needed and used by class A, then it is unncessary to define this object in the same package with A since none of the objects in the package is gonna use it anyway. It can also give extra data abstraction to the peer classes in the same package.