1 / 12

Java Interview Questions - Answers

Crack your Java interview by knowing all the questions that your interviewer may ask. Brush up your knowledge with this superb set of Java interview questions

Download Presentation

Java Interview Questions - Answers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Interview Q & A By www.bestonlinetrainers.com

  2. 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

  3. 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

  4. 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.}

  5. 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");

  6. 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+");

  7. 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.

  8. 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.

  9. 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.

  10. Q29: In Java, what is the default value of byte datatype? • Answer: 0 is the default value of byte datatype.

  11. 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);

  12. Want to learn Java? Visit : http://www.bestonlinetrainers.com/programming/java-training.html Email : info@bestonlinetrainers.com Phone : USA: +(1) 6783896789 UK: +(44)- 2032393637 India: +(91) 9246449191 Ask for a free demo TODAY : http://www.bestonlinetrainers.com/demo/index.html

More Related