Thursday, September 24, 2009

JAVA / J2EE / SWING Interview Questions W/ Solutions

Sharing Java interview questions being asked for mid-level to senior-level test engineering positions at well-established companies:
  1. What's the difference between a String and a StringBuffer?
  2. What's the difference between EJB and Hibernate?
  3. What's the difference between final, finally, and finalize?
  4. Difference between ArrayList and HashMap ?
  5. Expalin Swing Action architecture ?
  6. How will you communicate between two Applets ?
  7. How would you detect and minimize memory leaks in Java ? (Very important question)
  8. Explain SOA ?
  9. Explain Web and EJB containers ?
  10. Which Java pattern you have used, explain ?
  11. How to get a pop-up window when clicking on a button ?
  12. Given following code what will be output ?
  13. Explain exception handling in java ?
  14. Garbage collection - Can you force it ? (Very important)
  15. What is RMI ?
  16. How do you check that a date given as a combination of day, month and year is a valid date? For instance a date 2008-02-31 (as in yyyy-mm-dd) would be invalid date ?
Solution:
    Date parseDate(String maybeDate, String format, boolean lenient) {
    Date date = null;
    // test date string matches format structure using regex
    // - weed out illegal characters and enforce 4-digit year
    // - create the regex based on the local format string
    String reFormat = Pattern.compile("d+|M+").matcher(Matcher.quoteReplacement(format)).replaceAll("\\\\d{1,2}");
    reFormat = Pattern.compile("y+").matcher(reFormat).replaceAll("\\\\d{4}");
    if ( Pattern.compile(reFormat).matcher(maybeDate).matches() ) {
      // date string matches format structure, 
      // - now test it can be converted to a valid date
      SimpleDateFormat sdf = (SimpleDateFormat)DateFormat.getDateInstance();
      sdf.applyPattern(format);
      sdf.setLenient(lenient);
      try { date = sdf.parse(maybeDate); } catch (ParseException e) { }
    } 
    return date;
  } 
  // used like this:
  Date date = parseDate( "21/5/2009", "d/M/yyyy", false);
OR
package <>
import java.text.ParseException;
import java.text.SimpleDateFormat;


public class ValidateDate {
  public static boolean isValidDateStr(String date, String format) {
   try {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    sdf.setLenient(false);
    sdf.parse(date);
   } catch (ParseException e) {
    System.out.println(e.getMessage());
    return false;
   } catch (IllegalArgumentException e) {
    System.out.println(e.getMessage());
    return false;
   }
   return true;
  }
  public static void main(String[] args) {
   System.out.println(" 1900-12-13 valid ? "
     + ValidateDate.isValidDateStr("1900-12-13", "yyyy-MM-dd"));
   // "1990-12/13" throws a ParseException
   System.out.println(" 1900-12/13 valid ? "
     + ValidateDate.isValidDateStr("1900-12/13", "yyyy-MM-dd"));
   // "1990-13-12" throws a IllegalArgumentException
   System.out.println(" 1900-13-12 valid ? "
     + ValidateDate.isValidDateStr("1900-13-12", "yyyy-MM-dd"));
   /*
    * output : 1900-12-13 valid ? true 1900-12/13 valid ? false 1900-13-12
    * valid ? false
    */
   //LEAP YEAR
   System.out.println("2009-02-29 valid ? "
     + ValidateDate.isValidDateStr("2009-02-29", "yyyy-MM-dd"));    //EPOCH TIME
   System.out.println("1969-01-01 valid ? "
     + ValidateDate.isValidDateStr("1969-01-01", "yyyy-MM-dd"));
  }
}

How to test Private or Protected methods ?
Solution: You can not test Private methods they have to be made either protected or public. Though using Goovy framework you could achieve it, look for the same on web. Protected methods are for sub-classing so sub-class it and have a public method that will "exercise" the protected method in the unit test of that subclass. thats just a quick thought.

No comments:

Post a Comment