Wednesday, May 22, 2013

Java Logic Questions Part - I

Posted by Sharath Chandra
Hi Friends, 

I will post some simple yet logical Java Questions 

These are containing some of my real time experiences and some are learnt from various persons. 


QUESTION 1

public class Question1 {

public static void main(String []s)  {

         System.out.println("Hello");
         http://www.google.com
         System.out.println("World");

}

}

QUESTION 2 

public class Question2 {

public static void m1() {
     System.out.println("In m1()");
}

public static void main(String s[]) {
      Question2 q2 = new Question2(); 
      q2=null;
      q2.m1();
}

}


Result? 

|
|
|
|
|
|
|
|


|
|
|
|
|
|
|
|


|
|
|
|
|
|
|
|


|
|
|
|
|
|
|
|


|
|
|
|
|
|
|
|

Answers:
Question1: It prints
Hello 
World 

Reason: Because http://www.google.com - in this line it will ignore whatever we have written after // and http: to be considered as Label by compiler !


Question2: It prints 
In m1()

Reason: Because m1() is a Static method! No NPE will come.