Java is the future !: 2010

Sunday, December 5, 2010

Top 5 reasons that make SCJP Certification Exam tough

1.You are not in reality of the exam preparation
Most of the time preparation for this certification exam is approached in the conventional style especially the way you did preparation back in school and college days. However the reality of this exam is that you are no longer in school or in college (some exceptions maybe there) you are in a full time job and you have to manage job responsibilities and also are going to study after a break. Hence the preparation approach must be also different.

Solution : Plan ahead
Rather than beating yourself up for not studying the way you did earlier design a new approach as the circumstances are different now. Make a study plan keeping the reality of the current circumstance in mind.

2.You only prepare theoretical aspects of the exam
The SCJP exam now-a-days having more coding questions. Small code snippets are given and either with compilation errors or code logic errors need to be answered.

Solution : Practice practice practice...
SCJP questions can be best answered if you have done hands on programming in Java and if not then you will need to practise many such questions that give you exposure of coding. Look for mock tests/sample practise tests that have you practise a lot of coding.

3.Preparing Alone
Preparing for the certification exam alone is a trap you will never be able to study alone as you have never done that in school/college etc you had your entire class friends preparing with you.

Solution : get a group of committed people to take the exam with you
Get a friend/colleague or register in a Java forum to belong to a group of people and prepare along with them. Not only will it be fun but it" also give you the required competition and keep you on your toes.

4.Nothing happens over night
You might have heard of this great person in your office who passed the exam in 1 week or 1 month and now you are impatient as to how long it will take for you to be certified. You plan for 1 month and soon the plan goes to the dogs so now you are really impatient and in your mind you have made it a very tough exam that will take lots to pass.

Solution : Build brick by Brick
Don't try to learn the entire language in a week/month, take your time. Keep the objective of learning and not just somehow passing the certification exam. Learn "why" something works a given way, not just that it works one way. This will surely see you long way in your journey as a Java professional.

5. Am I ready to take the test?
This is a tough one to answer. And exam will always be an exam meaning it will always give butterfly in your stomach and make you feel like throwing up the moment you think of taking the exam.

Solution : Don"t be guided by you feelings
Here again I will point to the first point, keep in touch with the reality of your exam preparation. Let the scores in the practise tests/mock tests guide you whether you are ready for the exam or not. If you are scoring 85% + in the mock tests do go take the exam.

Friday, December 3, 2010

Quiz of the Day | December 3, 2010

What will be the output when you compile and execute the following program? 
Choose one answer. 
a. Prints true 
b. Prints false 
c. Prints 9 
d. Prints 2 
e. Compiler error : Incorrect declaration of main method 
f. None of these

Solution : Choice F is the correct answer. 

The code does not compile because the && operator works only with boolean operands. If both operands are true, the && operator returns true and false in all the other cases. In this case, integer operands arepassed to the && operator, so the code does not compile. Since the code does not compile, choices A to D are incorrect.

Choice E is incorrect because there is nothing wrong with the declaration of the main method. The modifiers public and static can appear in any order. Also, the main method takes a String[] as the argument.

Source : "Facebook Community"

Sunday, November 7, 2010

Quiz of the Day | November 7, 2010

What will happen when you attempt to compile and run the above code? (Assume that the code is compiled and run with assertions enabled.)

a. It will print odd and even numbers from 0 to 9 correctly (0 even and 1 odd). 
b. It will print odd and even numbers from 0 to 9 incorrectly (0 odd and 1 even). 
c. Compilation error at line 9. 
d. Compilation error at line 10. 
e. It will result in an infinite loop.

Solution : Choice A is the correct answer.

The code will compile successfully and it will print odd and even numbers from 0 to 9 correctly, without reversing even and odd.

Let's look at the execution of the code. The assert statement at line 9 asserts that "i%2==0" must be true, which will be true for all the even numbers in the range 0 to 9 (0, 2, 4, 6, and 8), and false for all the odd numbers in the range 0 to 9 (1, 3, 5, 7, and 9). When the assert statement is evaluated to true, the second expression on the right hand side of the colon is not evaluated. On the other hand, when the assert statement is evaluated to false, the expression on the right hand side of the colon is evaluated and an AssertionError is thrown.

When the for loop starts with i=0, the assert condition at line 9 is evaluated to true, hence the other expression i-- is not evaluated, the value of i (0 here) is printed at line 10. Next, i is incremented to 1 in the next iteration of the for loop. The assert condition at line 9 is false in this case, hence the expression i--is evaluated (i becomes 0) and an AssertionError is thrown. The print statement at line 10 is not executed in this case. The catch statement at line 12 catches this error and the print statement at line 14 increments i with ++i and prints its value (which is 1 in this case). The same process is repeated for all the numbers, thus effectively printing all the even and odd number correctly.

The key here lies in understanding when the right-hand side expression of an assert statement gets evaluated. It gets evaluated *only* when the boolean left-hand side expression is evaluated to false. Hence, the loop will not result in an infinite loop. 

Source : "Facebook community"

Saturday, November 6, 2010

Converting a 2d array of ints to char and string in Java

Question How can i convert the ints in a 2d array into chars, and strings?

If i copy ints to a char array i just get the ascii code.
For example
public int a[5][5]
//some code
public String b[5][5] = public int a[5][5]
Answer : Quite frankly, the easiest approach would simply leave your array as-is... and convert int values toString's when you use them:

Alternatively, you could start with a String[][] array in the first place, and simply convert your intvalues to String when adding them:
a[5][5] = new String(myInt);
If you really do need to convert an array of type int[][] to one of type String[][], you would have to do so manually with a two-layer for() loop:
String[][] converted = new String[a.length][];
for(int index = 0; index < a.length; index++) {
    converted[index] = new String[a[index].length];
    for(int subIndex = 0; subIndex < a[index].length; subIndex++){
        converted[index][subIndex] = Integer.toString(a[index][subIndex]);
All three of these approaches would work equally well for conversion to type char rather thanString.

Source : Stack-Overflow