Wednesday, October 18, 2006

Mission SCJP Accomplished ... !!

SCJP .... finally done with it. SCJP is all about concentration. Its as much about your skill as it is about identifying traps in each question. I may sound like a lecturer but I have identified some traps that might be useful .... MIGHT .... :)

1). First of all see if any non static members are being accessed from a static context. Invariably, the main method tries to access a non static method or variable.

2). String manipulations. A String is manipulated but its return value is ignored.

3). Inner classes can access final variables of its enclosing context. But still, the value of such variables should not be changed ... :)

4). Threads ... just check out exactly what code is synchronized. Often, the run method is not synchronized. But some code inside the run method is synchronized and 2 threads are running simulataneously. In such a scenario, both threads will be using the run method concurrently. Watch out for infinite loops in the run method that are part of a synchronized code. Also, wait and notify should be in a synchronized block.

5). Watch out for the way in which inner classes are instantiated. No inner class should be instantiated using some object of the outer class. I mean while declaration. Example, if u have an Outer class and an Inner class ....
If Inner is non static then the correct way to instantiate it is
either Outer.Inner i = new Outer().new Inner() OR
Outer.Inner i = o.new Inner() where o has already been created.
Watch out for stuff like o.Inner i = new Inner();

6). Assertions .... first check the expression after the : .... It should not be void in any way.

7). When u dont get an answer from the given options, start thinking in the opposite way ... I mean what cannot be the answers .... this might help.

8). Watch out for code that sends a String reference to a method and it is accepted by the method with the same name. This can be fatal in Threads.
Example :

String name = "Something"; (this is a member variable)
method(name);

public void method(String name) {
do some manipulation with local variable name
}

print name in the run method of the thread .... it should take the member variable value not the one that method changed .... coz strings are anyway immutable.

9). switch(1) is allowed ... :) ... I didnt know this.

10). any operation on byte or a short requires a cast unless u use compound assignment.

11). No wrapper class for primitive types has an empty constructor .... beware. Plus, no wrapper class can be extended coz they all are final. For that matter, even Math.

12). If a local variable is not initialized, it will give a compilation error when u try to use it. But this constraint is relaxed for arrays. Very important.

13). Variables passed to methods are never garbage collected within the method.

14). If u have a class that extends Thread and u write a start method of ur own in that, then the original start method will be over ridden. Any call to thread.start() will then invoke this new method and not the run method.

15). In case of any inheritance questions, first check all the constructors all classes involved in the inheritance. Look out for non default constructors. Many times, an instance of a subclass is created like SubClass s = new SubClass().
The SubClass doesnt have any constructors but the superclass has a non default constructor, so this wont compile.

16). Make sure you know the in and out of >> and >>> especially handling of negative numbers.

17). Watch out for try catch blocks where a special kind of exception is caught but the code written in try does not throw that kind of exception. In this case, the code wont compile.

18). If you have a class that implements Runnable, and then invoke start on an instance of this class, it will NOT invoke the run method. To invoke the run method, you always need a Thread instance.

19). No keyword of Java has any uppercase letter. Dont be confused by instanceOf.

20). Watch out for usage of equals method of any object. One of the prerequisites of equals method is that both the arguments to it need to be of the same class. So if you have something like String s = new String("true") and Boolean b = new Boolean("true"), invoking s.equals(b) will always return false. Even if you replace Boolean by Object, it will not make any difference unless you have assigned a String to that Object and then that String also contained "true". For that matter, even if u replace Boolean by StringBuffer, it will always return false.

21). If obj instanceof A returns true, it does mean that obj is an instance of class A but it can also be an instance of some subclass of A. There are quite a few questions on this.

22). Math.floor(-0.5) is -0.0 and not 0.0 .... :)

23). Watch out for code like
if(i++ < 10) {
int j = i;
}
over here, while comparing in the if statement, the value of i will not be incremented but inside the block, j will be assigned the latest value of i.

24). Interfaces can only "extend" other interfaces. Classes can never "extend" interfaces.

25). You cannot have static, private, public or protected variables in a local scope.

26). Arrays have a length field, not a length method.

27). Top level classes can only be public or default, not private or protected.

Well, I dont know how useful this is. There are quite a few traps. Im sure I have not covered all, but the bottomline is that you need to focus a lot while solving the questions. If you think of it, all questions are relatively easy. Its just that you have to find the catch.

Cheers ... !!

No comments: