Select chapter



basics (7.papers)
constructors (1.papers)
packages (1.papers)
inheritance (7.papers)
misc (3.papers)
exceptions (5.papers)
threads (4.papers)
overloading (1.papers)
jsp (3.papers)
servlets (9.papers)
jdbc (2.papers)
util (1.papers)
xml (2.papers)
IO stream (3.papers)
Hide

Sponsored Links


  

java interview questions and answers

Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
8
What is the difference between ensurecapacity(int len) and setlength(int len) in StringBuilder class?
  1. both are same internally.
  2. ensurecapacity(int len) is to request java to set aside, that much sized array for you. this is the minimum size of your array. where as with setlength(int len) existing length will be changed to new length, if new length is greater than old length then characters after old length till new length will become null '\u0000' characters.
  3. with ensurecapacity(int len) existing length will be changed to new length, if new length is greater than old length then characters after old length till new length will become null '\u0000' characters. setLength(int len)  is to request java to set aside that much sized array for you. this is the minimum size of your array.
  4. none
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
14
How to convert string to date in Java?
  1. SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
     String dateInString = "31-Dec-2013";
     
     try {
     
      Date date = formatter.parse(dateInString);
      System.out.println(date);
      System.out.println(formatter.format(date));
     
     } catch (ParseException e) {
      e.printStackTrace();
     }
  2. Date d =new Date();
    String dateInString = "31-Dec-2013";
    Calender c ;
    d = c.getDate();
    d.toString();
    System.out.println(d);
  3. SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
     String dateInString = "31-Dec-2013";
     
     try {
     
      Date date = formatter.covert(dateInString);
      System.out.println(date);
      System.out.println(formatter.format(date));
     
     } catch (ParseException e) {
      e.printStackTrace();
     }
  4. SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
     String dateInString = "31-Dec-2013";
     
     try {
     
      Date date = formatter.toDate(dateInString);
      System.out.println(date);
      System.out.println(formatter.format(date));
     
     } catch (ParseException e) {
      e.printStackTrace();
     }
Show Answer
Share Facebook gmail Twitter
15
What is compareTo method in string class? How to use it?
  1. compareTo method compares two strings lexicographically. It will compare the characters in the string of current object on which it is called with the characters in the string which is passed as parameter to this method.
  2. This method returns -ve value if lexicographical value of first string is lesser than second string. It returns +ve value if lexicographical value of first string is more than second string. It returns 0 if lexicographical value of both the strings are same.
  3. Below code will show how to use compareTo method
            String str1 = "Hello";
            String str2 = "Hello";
            System.out.println(str1.compareTo(str2));
    
  4. all are true.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
17
What are the disadvantages of a String?
  1. I can share string with multiple threads, so that all threads can read it with out modifying it.
  2. If I want to do too many modifications on string, it is not recommended to use as it generates separate object each time I try to modify it.
  3. Strings when stored in string constant pool, uses memory efficiently due to its reusability property.
  4. I can't inherit String class to my own class and modify its behavior.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
19
What is the difference between stringbuilder and stringbuffer?
  1. StringBuilder is mutable & synchronized
  2. StringBuffer is mutable & not synchronized.
  3. StringBuilder is immutable & not synchronized
  4. StringBuffer is mutable & synchorinzed
Show Answer
Share Facebook gmail Twitter
20
In StringBuilder what is the difference between length() and capacity() functions?
  1. Both length & capacity will give you the total size of that array.
  2. Capacity will give you the total size(capacity) of the array, where as length will give the number of characters in that array.
  3. Capacity will give you the total number of characters in that array, where as length will give you the total length(capacity) of that array.
  4. capacity will be always less than or equal to total length of that array.
Show Answer
Share Facebook gmail Twitter
Read more papers :