Java is the future !: November 2010

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

Quiz of the Day | November 6, 2010

What value is printed out by executing the above code?

a. 2.7 
b. 1.7 
c. 0.0 
d. -1.0

Solution : Choice A is the correct answer.

In Java, all parameters are passed by value. In case of primitives, the copy of the variable is passed, while in case of object references, it's the copy of the reference that is passed. 

When the argument is a primitive type, pass-by-value means that the method can change the value of the passed argument (in the method scope) but, the called method cannot change the value of the variable in the calling method.

When the argument is of reference type, pass-by-value means that the method cannot change the object reference, but can invoke the object's methods and modify the accessible variables within the object.

In the example above, d is passed as value, hence the change done in the doMinus() method doesn't reflect on d outside the doMinus() method. Thus, the value printed is 2.7

Source : "Facebook community"

Friday, November 5, 2010

Quiz of the Day | November 5, 2010 [2]

What will be printed on standard output if the following class is executed using the command "java Test 1 two 3" ?

a. 1 

b. two 
c. NumberFormatException 
d. ArrayIndexOutOfBoundsException 
e. Code does not compile

Solution : Choice C is the correct answer. 

In Java, command line arguments are stored in the args array which is an argument to the main method. The index of the args array starts with 0 and the first command line argument is the word after the classname. Thus, args[0] will be 1 and args[1] will be "two".

The parseInt() method of the Integer class parses the String argument to the int type. The String passed to the parseInt() method should have only digits, with an exception that it can start with '-' to indicate a negative number. Otherwise, NumberFormatException will be thrown. Thus, when "two" is passed to the parseInt() method, NumberFormatException will be thrown.

Please note that there is nothing wrong with the definition of main, the static word can come after or before public keyword. 

Source : "Facebook community"