Certification help

We all hate them, but let’s look at certification questions and answers

Q: I, a beginner in the Java field, plan to take the Java Certified Programmer test soon. With that in mind, would you please answer the following four questions?

1. Given the following class definition:

class A 
{
  protected int i;
  A(int i) 
  {
    this.i = i;
  }
}

Which of the following would be a valid inner class for this class? Select all valid answers.

  1. class B {}
    
    
  2. class B extends A {}
    
    
  3. class B 
    {
      B() 
      { 
        System.out.println("i = " + i);
      }
    }
    
    
  4. class B 
    {
      class A {}
    }
    
    
  5. class A {}
    
    

A: Answers B, D, and E are invalid. Answers D and E are invalid for the same reason: inner classes can not have the same name as the enclosing type.

In my opinion, answer B represents the more interesting mistake. Notice that class A does not define a no-argument constructor. Now, note that the inner class B does not define a constructor. Thus, class B relies on the default constructor B(). Class B‘s default constructor looks like this:

public B() {}

However, there is a gotcha! Constructors implicitly call super(). Thus, class B‘s default constructor actually looks like this:

public B()
{
  super();
}

Unfortunately, class A does not define a no-argument constructor. Thus, this definition is incorrect and will not compile. However, class B would be correct if changed to:

class B
{
  B()
  {
    super(1); // pass it any integer    
  }
  // or
  B(int number)
  {
    super(number);
  }
}

You could also add a no-argument constructor to class A and leave class B as is.

2.

public class A
{
  A()
  {
    System.out.println("Hello Mr A!");
  } 
     
  public static void main(String []args)
  {
    A a = new A();
  }
}

Java specs define that there is no instance of the class in its static method. Why can I use

  A a=new A();

in a main method?

A: You’re right, there is no instance of the class in its static methods! But this doesn’t mean what you think. It means that you cannot access a this reference from within a static method. If we look at what static means, the reason is clear.

A static qualifier turns a given method or member into a class method or class member. This means that the member or method is class based, not instance based. When declared as static, all instances of the given class share the member or method. You can also access the static methods or members directly through the class reference — that is, ClassName.method(). Since the member or method is shared in this way, it makes no sense to have a this reference inside of a static method. Which this would the this reference? Where would the this even come from if we make a static method call on the class itself?

So, you can’t say something like this.toString() from within a static method. You can create and manipulate objects within a static method. Thus, it is entirely valid to say A a = new A(); from within the static main method.

3. Given the following class:

class Counter 
{
  public int startHere = 1;
  public int endHere = 100;
  public static void main(String[] args) 
  {
    new Counter().go();
  }
  void go() 
  {
    // A
    Thread t = new Thread(a);
    t.start();
  }
}

What block of code can you replace at line A above so that this program will count from startHere to endHere?

Select all valid answers.

  1. Runnable a = new Runnable() 
    {
      public void run() 
      {
        for (int i = startHere; i <= endHere; i++) 
        {
          System.out.println(i);
        }
      }
    };
    
    
  2. a implements Runnable 
    {
      public void run() 
      {
        for (int i = startHere; i <= endHere; i++)
        {
          System.out.println(i);
        }
      }
    };
    
    
  3. Thread a = new Thread() 
    {
      public void run() 
      {
        for (int i = startHere; i <= endHere; i++) 
        {
          System.out.println(i);
        }
      }
    };
    
    

A:

Answers A and C are correct. Remember that a thread’s target is a Runnable or ThreadGroup (Thread implements Runnable). Thus, another thread may be a target as well.

Answer A simply defines an anonymous Runnable inner class. Answer C simply creates a thread and overrides the run method to do some useful work.

Answer B is incorrect because the local inner class is incorrectly defined. The proper definition is:

class A implements Runnable 
{
  public void run() 
  {
    for (int i = startHere; i <= endHere; i++)
    {
      System.out.println(i);
    }
  }
};
Runnable a = new A();

4. Given the following method:

1.  public void method( ){ 
2.  String a,b; 
3.  a=new String("hello world"); 
4.  b=new String("game over"); 
5.  System.out.println(a+b+"ok"); 
6.  a=null; 
7.  a=b; 
8.  System.out.println(a); 
9.  } 

In the absence of compiler optimization, which line is the earliest point at which the object a is definitely eligible to be garbage collected?

A

An object is eligible for garbage collection when nothing is left with a reference to the object. Thus, "hello world"is eligible for garbage collection once all references to the string are cut. Here, this happens when a is set to null, since a is the only reference to "hello world".

By the way, this is bad Java String code. You should almost never create a String with a new call. If you do use new, you are wasting memory. To understand why, you need to understand how Java treats Strings.

Say you have the following code:

. . .
String a = "hello";
String b = "hello";
String c = new String("hello");

a is set to a String object, "hello". The JVM implicitly goes out, calls new, and allocates the string. b is set to reference the same "hello" String object. Instead of implicitly calling new and allocating a new String, b is set to reference the same String object that a references. We can get away with this because Strings are immutable.

However, c does not share the reference. Instead, the call to new forces the construction of a second String object. Thus, you now have two String objects when you only need one.

Tony Sintes is a senior consultant at ObjectWave Corporation, specializing in telecommunications. Tony has worked with Java since 1997 and is a Sun-certified Java 1.1 programmer and Java 2 developer.

Source: www.infoworld.com