Friday, April 10, 2015

Tricky Codes used to test your knowledge on Java

I have came a cross few tricky Java coding. It's not difficult stuff, but pretty much tricky, where anybody will easily miss.

1.

package array;

public class ChangeIt {

static void doIt(int[] z) {
z = null;
}
}


package array;

public class TestIt {
public static void main(String[] args) {

int[] myArray = { 1, 2, 3, 4, 5 };

ChangeIt.doIt(myArray);

for (int i = 0; i < myArray.length; i++) {
System.out.print(myArray[i] + " ");
}
}
}


Output :::
----------------

1 2 3 4 5 


inside method "ChangeIt.doIt(myArray);" array reference is set to null. But it will be  a copy of the object , not the reference.

But if you try to loop array inside method, then there will be NullPointerException
 eg : I have highlighted the code which will make NullPointerException.
package array;

public class ChangeIt {

static void doIt(int[] z) {
z = null;
for (int i = 0; i < z.length; i++) {
System.out.print(z[i] + " ");
}
}
}

Output
------------

Exception in thread "main" java.lang.NullPointerException
at array.ChangeIt.doIt(ChangeIt.java:8)
at array.TestIt.main(TestIt.java:8)

---------------

2. Above ChangeIt class sets the for "null", let's look at copying to separate array.


package array;

public class ChangeIt {

static void doIt(int[] z){
int A[] =z;
A[0] =99;
}
}

package array;

public class TestIt {
public static void main(String[] args) {

int[] myArray = { 1, 2, 3, 4, 5 };

ChangeIt.doIt(myArray);

for (int i = 0; i < myArray.length; i++) {
System.out.print(myArray[i] + " ");
}
}
}

output
==============================
99 2 3 4 5

==============================

Even though  we are changing the value of newly create array, it still has the reference to passes array (in this case "myArray" or the array comes from the parameter of that method). you may miss that it still has the reference and give output something like "1 2 3 4 5 " which is wrong.




3. Using "this"  inside static context -  this will not allowed inside static context.
This will have compile error.

----------------------------------------------------------------------

public static void main(String[] args) {
System.out.println(this);

}

----------------------------------------------------------------------

But you can use "this" out of static context.

-----------------------------------------------------------------------
public class Circle {
private String name;

public String getName() {
               // this is allowed
System.out.println(this);
return name;
}

public void setName(String name) {
this.name = name;
}

}
---------------------------------------------------------------------------


4. Java uses the call by value. What is the value that is being passed into routine by the method call in the following ??

double[] rats ={1,2,3};

routine(rats);

correct Answer : A reference to the array object rats.

you may try to deviate with first sentence  all by value and may give an answer like "A copy of the array rats" , which would be wrong.

5. Array length
---------------------------------------

int[] myArray = { 1, 2, 3, 4, 5 };

ChangeIt.doIt2(myArray.length);

------------------------------------------

length of the array is taken as property with "lenght" , but with our normal method standards ,we might choose an option like "myArray.length()" - word length with parenthesis.
Remember : length does not have parenthesis.

6. Retrieve values from an array
Most important point in an array is its index starts with "0" - zero. We all know , but some how down the line we miss it. when thing become complicated you will probably my lose this fact.
Output of the below code segment is 12, but you may miss it and select 13, which is wrong- you miss with the index.
So array values starts with 5  and goes with 5,6,7,8,  ... until the end.
we retrieve the index "7", that means 8th element , which is value equal to 12.
-----------------------------------------------
                int[] num5 = new int[9];
for (int i = 0; i < num5.length; i++) {
num5[i] = i+5;
}
System.out.println(num5[7]);
------------------------------------------------

output
=========================
12
======================

So the highest value associated with the array would be one less than length.
eg : if we define array like

byte[] value = new byte[x];

the highest value for the index would be "x-1"


No comments:

Post a Comment