Powered By Blogger

Saturday, June 25, 2016

Certification programs explained


----------------------------------------------------------------------------------------------------------------
1. String behaviour.

package com.thebeautifuljava.certificationprograms;

public class Test1 {

 public static void main(String[] args) {

  String str1 = "abc"; // object is created in string constant pool
  String str2 = "def"; // object is created in string constant pool

  String str3 = str1.concat(str2); // str3 = "abc".concat("def") 
                                  // str3 points to the new object "abcdef" in pool and heap 
                                  // (Refer String chaper in Java 6 Kathy Sierra and Bert Bates)

  str1.concat(str2); // "abc".concat("def") => "abcdef" (No reference). This object is already present in pool and
                     // new object "abcdef" is created in heap also!
                      // (Refer String chaper in Java 6 Kathy Sierra and Bert Bates)

  System.out.println(str1); // str1 still points to object "abc" as 
                           // str1 reference is not updated!!
 }

}

----------------------------------------------------------------------------------------------------------------
2. ceil() and round() methods of Math class.

package com.thebeautifuljava.certificationprograms;

public class Test2 {
 public static void main(String args[]) {
  int i;
  float f = 2.3f;
  double d = 2.7;
  i = ((int) Math.ceil(f)) * ((int) Math.round(d)); // i = ((int) 3.0) * ((int) 3) => i = // (3 * 3) => i = 9
  System.out.println(i); // prints 9
 }
}
----------------------------------------------------------------------------------------------------------------
3. Post decrement operator behaviour.



package com.thebeautifuljava.certificationprograms;

public class Test3 {

 public static void main(String args[]) {
  int i = 1;
  do {
   i--; // i=1
  } while (i > 2); // i=0, (0 > 2)? - false
  System.out.println(i); // prints 0
 }

}

----------------------------------------------------------------------------------------------------------------
4. Difference between == operator and equals() method of Object class.



package com.thebeautifuljava.certificationprograms;

public class Test4 {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  String s1 = "abc"; // memory is allocated in string constant pool
  String s2 = new String("abc"); // memory is allocated in heap and "abc" literal is created in pool also!
                                 // (Refer String chaper in Java 6 Kathy Sierra and Bert Bates)
  if (s1 == s2) // false - as s1 and s2 points to different memory locations
   System.out.println(1);
  else
   System.out.println(2); // prints 2
  if (s1.equals(s2)) // true - ("abc".equals("abc"))? 
                    // as equals method  compares String object's contents
   System.out.println(3); // prints 3
  else
   System.out.println(4);
 }

}

----------------------------------------------------------------------------------------------------------------
5. Post increment and decrement operator.



package com.thebeautifuljava.certificationprograms;

public class Test6 {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

  int i = 1, j = 1;
  try {
   i++; // i=1
   j--; // j=1, i=2
   if (i == j) // j=0 => (2==0)? - false
    i++;
  } catch (ArithmeticException e) {
   System.out.println(0);
  } catch (ArrayIndexOutOfBoundsException e) {
   System.out.println(1);
  } catch (Exception e) {
   System.out.println(2);
  } finally {
   System.out.println(3); // prints 3 (because no exception)
  }
  System.out.println(4); // prints 4 (executes after finally block)
 }

}

----------------------------------------------------------------------------------------------------------------
6. Pre increment and post decrement operator.



package com.thebeautifuljava.certificationprograms;

public class Test7 {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

  int i = 0, j = 2;
  do {
   i = ++i; // i=1 => i=2
   j--; // j=1 => j=0
  } while (j > 0); // (1 > 0)? - true => (0 > 0)? - false
  System.out.println(i);
 }

}

----------------------------------------------------------------------------------------------------------------
7. switch-case statement.



package com.thebeautifuljava.certificationprograms;

public class Test8 {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

  int i, j = 1;
  i = (j > 1) ? 2 : 1; // (1 > 1)?2 : 1 - false and i=1
  switch (i) { // switch(1)
  case 0:
   System.out.println(0);
   break;
  case 1:
   System.out.println(1); // prints 1 (because no break statement after case 2)
  case 2:
   System.out.println(2); // prints 2
   break;
  case 3:
   System.out.println(3);
   break;
  }
 }

}

----------------------------------------------------------------------------------------------------------------
8. Assignment operator, wrapper class and array.



command line - java Test9 5

package com.thebeautifuljava.certificationprograms;

public class Test9 {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

  Integer intObj = Integer.valueOf(args[args.length - 1]); // args[1-1] => args[0]
  int i = intObj.intValue(); // args[0] = 5 - command line argument
  if (args.length > 1) // false - args.length = 1 (1 > 1)?
   System.out.println(i);
  if (args.length > 0) // true - args.length = 1 (1 > 0)?
   System.out.println(i - 5); // executes i=i-5, updated i's value is 0
  else
   System.out.println(i - 2);
 }

}

----------------------------------------------------------------------------------------------------------------
9. for-loop working.



package com.thebeautifuljava.certificationprograms;

public class Test10 {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

  for (int i = 0; i < 2; i++) { // First iteration i=0, Second iteration i=1
   for (int j = 2; j >= 0; j--) { // 2 iterations j=2,1, 1 iteration
    if (i == j) // Second iteration of inner loop - i=1,j=1 condition is true
     break;
    System.out.println("i=" + i + " j=" + j); // prints i=0,j=1 i=0,j=2 i=1,j=2 for 3 iterations
   }
  }
 }

}

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

No comments:

Post a Comment