Tuesday, February 24, 2015

Java Quiz

1) What will be the output when you execute below code ?

for (int i = 0; i >= (-1) * 5; i--) {
          System.out.println(i);
}

Output : 0 -1 -2 -3 -4 -5


for loop repetitive control structure flow :
for (initialize; boolean_expression; update) {
}

  • Initialize will be called only once in the for loop to be initialize for the first time loop starts. You can leave blank by providing semicolon(;) placeholder.
  • boolean_expression will be called next to initialize to validate expression and if its true body of the for loop exucutes. False loop exits
  • update will be called upon completion of the body execution of for loop.
  • Once update complete it calls the boolean expression to validate to proceeds body and body completion will call update and moves on back to booleanExpression

Note : There is no difference between i-- or --i or i++ or ++i in the for loop update.


2) What will be the output when you execute below code ?
int i = 1;
int j = ++i;
System.out.println("i: " + i + " j: " + j);

and

int i = 1;
int j = i++;
System.out.println("i: " + i + " j: " + j);

Ouput : 
i: 2 j: 2
i: 2 j: 1


Why Difference?
    ++i increments the value first and then return it
    i++ return the value first and then increments it

Usually, try to use i = i + 1 for clear understanding in the code.

3) What will be the output when you execute below code ?
String testSplit = "hello,,world";
String[] testSplits = testSplit.split(",");
System.out.println(testSplits[2]);

Output : world

String testSplit = "hello,world,";
String[] testSplits = testSplit.split(",");
System.out.println(testSplits[2]);

Output : java.lang.ArrayIndexOutOfBoundsException: 2

String testSplit = "hello,world,";
String[] testSplits = testSplit.split(",", -2);
System.out.println(testSplits[2]);

Output :      (empty)

Note : By default, split drops all empty trailing columns, so any attempt to access the final column will result ArrayIndexOutOfBoundsException. Passing negative number (limit) as the second arg to split causes it to retain the trailing empty columns.

4) What will be the output when you execute below code ?
Pattern p = Pattern.compile("^([\"']?)\\d\\d:\\d\\d\\1,([\"']?)[A-Z]\\w+\\2,.*$");
  String regexpInput = "1:23,Logout Now";
  if (p.matcher(regexpInput.toString()).matches()) {
   System.out.println("Good");
  } else {
   System.out.println("Bad");
  }
Ouput : Bad

Why Bad ?
1. hour has only 1 digit 1 instead 2 digit
2. 2nd column has space in regexp which is not mentioned
3. Regexp expects 3rd column with comma(,) after some text 1 or more which is missing

\\1 and \\2 is backreference to repeat the grouping element
group starts from 1, 2, 3 Backreference also considered as group in regexp.

Difference between iBatis and Hibernate ? (Both are persistence framework)
iBatis / myBatis is sql driven model. It means based on sql you want to control application changes (SqlMap XML file)
Hibernate is object driven model. It means you design your object and create fields in database. (hbm - hibernate mapping xml file)
Pros and Cons :
iBatis is database dependent due to SQL usage, but faster development and lighter with cache support.
Hibernate is database independent due to HQL based approach. Its more heavy compare to JPA / iBatis where as highly scalable with advance cache support.

iBATOR - Code generator for iBatis.

castor - Castor is open source java data binding  framework. Moving data from XML to Java programming language objects and from Java to database. same as JAXB.

Difference between JPA and Hibernate ? 
JPA is specification / interface based on JSR
Hibernate / iBatis is implementation using JPA.

No comments :

// Below script tag for SyntaxHighLighter