Thursday, May 23, 2013

Java Logic Questions Part - II

Posted by Sharath Chandra
Question 3:

public class A {

public static void main (String []s) {

for (int i = 0; i < 4; i = ++i ) 
     System.out.println ( " In for loop : "+i); 

for (int m = 0; m < 4; m = m++) 
     System.out.println (" In for loop : "+m);

}

}


Question 4: 

public class B {

public B (Object o) {
    System.out.println ("In Object Constructor");
}


public B (String o) {
    System.out.println ("In String Constructor");
}


public static void main (String []s) {

B b = new B(null);

}

}


Result? 

|
|
|
|
|
|
|
|


|
|
|
|
|
|
|
|


|
|
|
|
|
|
|
|


|
|
|
|
|
|
|
|


|
|
|
|
|
|
|
|


Question 3: 
Answer
It will print 


In for loop : 0
In for loop : 1
In for loop : 2 
In for loop : 3

In for loop : 0 ...... n times - As it will land in Infinite loop. 

Question 4: 
Answer
In String Constructor

Because null will catch the first child constructor itself - because compiler will search from the child level to parent level for constructors. 


Please share it because - Sharing is Gaining in Knowledge

PART - I CLICK HERE