Saturday, May 25, 2013

Java Logic Questions Part - IV

Posted by Sharath Chandra
Question 7:

public class A { 

public static void main (String []s) {

  String t1 = "Sharath";
  String t2 = "Sharath";
  boolean b1 = t1 == t2;
  boolean b2 = t1.equals(t2);

  System.out.println(" Scenario 1 - Both are equal ? " + b1);
  System.out.println(" Scenario 2 - Both are equal ? " + t1 == t2);
  System.out.println(" Scenario 3 - Both are equal ? " + t1.equals(t2));
  System.out.println(" Scenario 4 - Both are equal ? " + b2);

}

}



Question 8:
public class A { 

public static void main (String []s) {
boolean choice = true;
  try {
   if (!choice) {
    System.out.println("inside if");
    while (true)
     ;
   } else {
    System.out.println("inside else ");
     System.exit(3444);
   }
  } finally {
   System.out.println("inside finally "); // This will not execute.
  }

}
}


Result? 

|
|
|
|
|
|
|
|


|
|
|
|
|
|
|
|


|
|
|
|
|
|
|
|


|
|
|
|
|
|
|
|


|
|
|
|
|
|
|
|


Question 7: 
Answer
It will print 

Scenario 1 - Both are equal ?  true   (Because both are Sharath having same reference!)
false  (Because it will concatinate Scenario 2 - Both are equal ?  with t1 and will check with == t2 so it will be false)
Scenario 3 - Both are equal ?  true  
Scenario 4 - Both are equal ?  true  

Question 8:
Answer
It will print

inside else

It will not print finally block statments here because - System.exit - This is the scenario where fianlly doesn't invoke.

Please share it because - Sharing is Gaining in Knowledge

PART - I CLICK HERE
PART - II CLICK HERE
PART - III CLICK HERE