Monday, February 11, 2013

SCJP finalize question

Which statement is true?
A. A class's finalize() method CANNOT be invoked explicitly.
B. super.finalize() is called implicitly by any overriding finalize() method.
C. The finalize() method for a given object is called no more than once by the garbage collector.
D. The order in which finalize() is called on two objects is based on the order in which the two objects became finalizable.


TreeSet subset detail

When manipulating a subSet of a TreeSet, you need to know that the subSet is linked to the original set, which means that if changes are performed in the original set, they will also get reflected in the subSet, and vice-versa.

public static void main ( String args [ ] ) {
  TreeSet t = new TreeSet();
  t.add(1);
  t.add(2);
  t.add(4);
  TreeSet t2 = (TreeSet)t.subSet(2, true, 4, true);
  t.add(3);
  System.out.print(t + " " + t2);
}


This will print:
[1, 2, 3, 4] [2, 3, 4]

But if you do this:
public static void main ( String args [ ] ) {
  TreeSet t = new TreeSet();
  t.add(1);
  t.add(2);
  t.add(4);
  TreeSet t2 = (TreeSet)t.subSet(2, true, 4, true);
  t.add(5);
  System.out.print(t + " " + t2);
}


It will print:
[1, 2, 4, 5] [2, 4]

because the number 5 is out of range of the subset.

Sunday, February 10, 2013

SCJP question

What is the result of this code?


class Person{
  String name="No name";
  public Person(String nm){ name=nm;}
}

class Employee extends Person {
  String empID="0000";
  public Employee(String id) { empID=id;}
}

public class EmployeeTest{
  public static void main(String [] args){
    Employee e=new Employee("4321");
    System.out.println(e.empID);
  }
}


A. 4321
B. 0000
C. An exception is thrown at Runtime
D. Compilation fails

Thread States


FAQs concerning Threads:
Q: What happens if we create a thread, and then call twice the start() method?
A: An IllegalThreadStateException is thrown. This is a RuntimeException.

java.io Class Hierarchy Diagram









Diagram sources:
Gailer-net.de

java.io cheat sheet for SCJP:
(from book SCJP Sun Certified Programmer for Java 6 Exam 310-065)

Example for using these constructors:
How to read lines from a text file, display them and close all resources?

try{
  BufferedReader reader= new BufferedReader (new FileReader(new File("file.txt")));
  String line=null;
  while( (line=reader.readLine())!=null){
    System.out.println(line);
  }
  reader.close();
}catch(Exception e){}


For other package diagrams, check falkhausen.de

Java Exceptions

The Throwable class is the superclass of all errors and exceptions in the Java language.
Then we have Error and Exception classes, that implement the Throwable interface, and there can be checked or unchecked exceptions:
  • Checked exceptions are the exceptions that need to be catch in a try catch block or need to be declared in the method signature where they are thrown.
  • Unchecked exceptions are RuntimeExceptions (or Errors) that do not need to be caught or declared in the method signature.


RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine. RuntimeExceptions that are currently part of the certification objectives are:
ArrayIndexOutOfBoundsException - when attempting to access an array with an invalid index value
ClassCastException - when attempting to cast a reference variable to a type that fails the IS-A test
IllegalArgumentException - when the method receives an argument formatted differently that the method expects
IllegalStateException - when the state of the environment doesn't match the operation being attempted
NullPointerException - when attempting to access an object with a null reference variable
NumberFormatException - when a method that converts a String to a number, receives a String that it cannot convert.

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Errors that are currently part of the certification objectives are:
AssertionError - if the assert boolean test evaluates to false
ExceptionInInitializerError - error while attempting to initialize a static variable ou an initialization block
StackOverflowError - when a method recurses too deeply, and exhausts the stack
NoClassDefFoundError - when the JVM can't find the class that it needs (command line error, classpath issue ou missing .class file)

Collections class diagram

You need to know each one of these for the certification.


Diagram is from book SCJP Sun Certified Programmer for Java 6 Exam 310-065.