Java Interview Q & A By www.bestonlinetrainers.com
Q21: How are strings compared Using “==” or equals ()? Answer: • “==” tests if references are equal and equals () tests if values are equal. To check if two strings are the same object, equals() is always used. Learn Java. Ask for a free demowww.bestonlinetrainers.com
Q22: Char is preferred over String for security sensitive information. Why? Answer: • Strings are immutable, that is once they are created, and they stay unchanged until Garbage Collector kicks in. Its elements can be explicitly changed with an array. Hence, security sensitive information like password will not be present anywhere in the system. Learn Java. Ask for a free demowww.bestonlinetrainers.com
Q23:WCan string be used to switch statement? • Answer: String can be used to switch statement to version 7. From JDK 7, string can be used as switch condition. Before version 6, string cannot be used as switch condition . view source print? 01.// java 7 only! 02.switch (str.toLowerCase()) 03.case "a": 04.value = 1; 05.break; 06.case "b": 07.value = 2; 08.break; 09.}
Q24:Can string be converted to int? • Answer: If the HashCode() is not overridded , the object will not be recovered from the HashMapif used as key. Yes .It can be .But its frequently used and ignored at times. view source print? 1.int n = Integer.parseInt("10");
Q25: How can a string be split with white space characters? • Answer: String can be slit using regular expression. White space characters are represented as “\s”. view source print? 1 .String  strArray = aString.split("\\s+");
Q26: What does substring() method do ? • Answer: The existing String is represented by the substring() method which gives a window to an array of chars as in JDK 6.A new one is not created. An empty string needs to be added to create a new one. print ? 1.str.substring(m,n)+ "" This creates a new char array representing a new string. This method can help to code faster because the Garbage Collector collects the unused large string and the keeps the substring.
Q27: What is String vsStringBuildervsStringBuffer? • Answer: In String vsStringBuilder, StringBuilder is mutable, that is it means it can be modified after its creation. In StringBuildervsStringBuffer, StringBuffer is synchronized, that is it means it is thread-safe but would be slower than StringBuilder.
Q28: How is a string repeated ? • Answer: In Python, to repeat a string just multiple a number. In Java, the repeat() method of StringUtils from Apache Commons Lang package can be used.
Q29: In Java, what is the default value of byte datatype? • Answer: 0 is the default value of byte datatype.
Q30: In a string how to count # of occurrences of a character? • Answer: StringUtils from apache commons lang can be used. view source print? 1.int n = StringUtils.countMatches("11112222", "1"); 2.System.out.println(n);
Want to learn Java? Visit : http://www.bestonlinetrainers.com/programming/java-training.html Email : firstname.lastname@example.org Phone : USA: +(1) 6783896789 UK: +(44)- 2032393637 India: +(91) 9246449191 Ask for a free demo TODAY : http://www.bestonlinetrainers.com/demo/index.html