Title: Java-Interview-Questions-&-Answers by BestOnlineTrainers (1)
1Java Interview Q A
- By www.bestonlinetrainers.com
2Q21 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 demo
www.bestonlinetrainers.com
3Q22 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 demo
www.bestonlinetrainers.com
4Q23WCan 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.
5Q24Can string be converted to int?
- Answer
- If the HashCode() is not overridded , the
object will not be recovered from the HashMap if
used as key. - Yes .It can be .But its frequently used and
ignored at times. - view source
- print?
- 1.int n Integer.parseInt("10")
6Q25 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")
7Q26 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.
8Q27 What is String vs StringBuilder vs
StringBuffer ?
- Answer
- In String vs StringBuilder, StringBuilder
is mutable, that is it means it can be modified
after its creation. In StringBuilder vs
StringBuffer, StringBuffer is synchronized, that
is it means it is thread-safe but would be slower
than StringBuilder.
9Q28 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.
10Q29 In Java, what is the default value of byte
datatype?
- Answer
- 0 is the default value of byte datatype.
11Q30 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)
12Want to learn Java?
- Visit http//www.bestonlinetrainers.com/programm
ing/java-training.html - Email info_at_bestonlinetrainers.com
- Phone USA (1) 6783896789
- UK (44)- 2032393637
- India (91) 9246449191
- Ask for a free demo TODAY http//www.bestonline
trainers.com/demo/index.html