Interview Questions and Answers - Order By Rating Asked multiple times in Capgemini. Q61. Why Char array is preferred over String for storing password? Core Java
Ans. String is immutable in java and stored in String pool. Once it's created it stays in the pool until unless garbage collected, so even though we are done with password it's available in memory for longer duration and there is no way to avoid it. It's a security risk because anyone having access to memory dump can find the password as clear text. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   string class   string   immutable  immutability   string pool   garbage collection   advanced Asked in 6 Companies Expert Q62. Why String is popular HashMap key in Java? Core Java
Ans. Since String is immutable, its hashcode is cached at the time of creation and it doesnt need to be calculated again. This makes it a great candidate for key in a Map and its processing is fast than other HashMap key objects. This is why String is mostly used Object as HashMap keys. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   string class   string   immutable  immutability   hashmap   immutable  immutability   hashcode   hash code   advanced Asked in 2 Companies expert   frequent Q63. public class a {
public static void main(String args[]){
final String s1=""job"";
final String s2=""seeker"";
String s3=s1.concat(s2);
String s4=""jobseeker"";
System.out.println(s3==s4); // Output 1
System.out.println(s3.hashCode()==s4.hashCode()); Output 2
}
}
What will be the Output 1 and Output 2 ? Core Java
Ans. S3 and S4 are pointing to different memory location and hence Output 1 will be false.
Hash code is generated to be used as hash key in some of the collections in Java and is calculated using string characters and its length. As they both are same string literals, and hence their hashcode is same.Output 2 will be true. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   string   hashcode   hash code   string comparison   string pool Try 1 Question(s) Test Q64. What is the use of HashCode in objects ? Core Java
Ans. Hashcode is used for bucketing in Hash implementations like HashMap, HashTable, HashSet etc. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   string   hashcode   hash code   string comparison  hashtable Asked in 17 Companies basic   frequent Q65. What does String intern() method do? Core Java
Ans. intern() method keeps the string in an internal cache that is usually not garbage collected.
Moreover provide reference for scp object for corresponding string object present in heap memory. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   string class   string   intern method   garbage collection   advanced Asked in 2 Companies expert Q66. How substring() method of String class create memory leaks? Core Java
Ans. substring method would build a new String object keeping a reference to the whole char array, to avoid copying it. Hence you can inadvertently keep a reference to a very big character array with just a one character string. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   string class   string   substring   memory leaks   jvm   memory management   advanced   architecture   technical architect   technical lead expert Very frequently asked in HCL Tech ( Based of 4 inputs ) Q67. Write a program to reverse a string iteratively and recursively ? Core Java
Ans. Using String method -
new StringBuffer(str).reverse().toString();
Iterative -
public static String getReverseString(String str){
StringBuffer strBuffer = new StringBuffer(str.length);
for(int counter=str.length -1 ; counter>=0;counter--){
strBuffer.append(str.charAt(counter));
}
return strBuffer;
}
Recursive -
public static String getReverseString(String str){
if(str.length <= 1){
return str;
}
return (getReverseString(str.subString(1)) + str.charAt(0);
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   string   reverse   stringbuffer   string class   code Asked in 6 Companies   frequent Q68. What will be the output of the following code ?
String s1 = "Buggy Bread";
String s2 = "Buggy Bread";
if(s1 == s2)
System.out.println("equal 1");
String n1 = new String("Buggy Bread");
String n2 = new String("Buggy Bread");
if(n1 == n2)
System.out.println("equal 2"); Core Java
Ans. equal 1 Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   string class   string   string pool   code   coding basic   frequent Q69. What will be the output of following Code ?
class BuggyBread {
public static void main(String[] args) {
String s2 = "I am unique!";
String s5 = "I am unique!";
System.out.println(s2 == s5);
}
} Core Java
Ans. true, due to String Pool, both will point to a same String object. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   code   coding   tricky questions   interesting questions   string   string pool   .equal   == intermediate   frequent Q70. what will be the output of this code ?
public static void main(String[] args){
StringBuffer s1=new StringBuffer("Buggy");
test(s1);
System.out.println(s1);
}
private static void test(StringBuffer s){
s.append("Bread");
} Core Java
Ans. BuggyBread Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   code   coding   stringbuffer   string   method calling   pass by reference Q71. what will be the output of this code ?
public static void main(String[] args) {
String s1=new String("Buggy");
test(s1);
System.out.println(s1);
}
private static void test(StringBuffer s){
s.append("Bread");
} Core Java
Ans. Buggy Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   code   coding   stringbuffer   string   method calling   pass by reference Q72. what will be the output of this code ?
public static void main(String[] args) {
StringBuffer s1=new StringBuffer("Buggy");
test(s1);
System.out.println(s1);
}
private static void test(StringBuffer s){
s=new StringBuffer("Bread");
} Core Java
Ans. Buggy Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   code   coding   stringbuffer   string   method calling   pass by reference Q73. Which methods of the Pattern class have equivalent methods in the String class? Core Java
Ans. split() and macthes() Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   io   string class   string   pattern Q74. public class BuggyBread1{
public static void main (String args[]) {
Set<String> mySet = new TreeSet<String>();
mySet.add(""1"");
mySet.add(""2"");
mySet.add(""111"");
for(String d: mySet){
System.out.println(d);
}
}
} Core Java
Ans. 1
111
2
TreeSet maintains the elements in the ascending order which is identified by the compareTo method. compareTo method in String has been defined such that it results in the natural alphabetic Order. Here the elements in the TreeSet are of String and not of Integer. In String Natural Order, 111 comes before 2 as ascii of 1st character first determines the order. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   code   coding   set   treeset   string   integer Q75. Give an Example for Builder Pattern ? Core Java
Ans. String str = new StringBuilder().append("Buggy").append("Bread").toString(); Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   builder pattern   design pattern   stringbuilder   string Asked in 3 Companies Try 1 Question(s) TestNot frequently asked as it was introduced with Java 8. Ans. StringJoiner is a util method to construct a string with desired delimiter. This has been introduced with wef from Java 8.
Sample Code
StringJoiner strJoiner = new StringJoiner ( "." );
strJoiner . add ( "Buggy" ). add ( "Bread" );
System . out . println ( strJoiner ); // prints Buggy.Bread
Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   java8   java 8   string   stringjoiner Q77. Have you ever used StringTokenizer ? Core Java
Ans. No, Alternatively I have used String.split("\\s") for achieving the same function. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   string   stringtokenizer Q78. Name few String related classes and interfaces ? Core Java
Ans. http://www.buggybread.com/2015/01/java-string-classes-and-interfaces.html Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   string Q79. How many elements will be there in TreeSet after the last line and Why ? TreeSet set = new TreeSet(); set .add(new String("abc")); set .add(new String("abc")); Core Java
Ans. One. As we haven't specified the type of TreeSet, it being evaluated with the first element insertion. Once it's identified that it's of type String and as no comparator has been defined, the comparison is done using the String compareTo method. String compareTo method compares the elements by the content / value. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   set   treeset   string   compareto   coding   code Q80. Strings in switch were introduced in Which Java version ? a. Java 5 b. Java 6 c. Java 7 d. Java Core Java
Ans. Java 7 Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   switch   string in switchRarely asked as it was introduced with Java 8. Q81. Which of the following has been introduced with Java 8 ? a. StringBuffer b. StringBuilder c. StringFilter d. StringJoiner Core Java
Ans. StringJoiner Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   java8   java 8   string   stringjoiner Q82. Write a method to convert all elements of a set to lower case. Core Java
Ans. http://javasearch.buggybread.com/CodeSnippets/searchCodeSamples.php?keyword=Method+to+convert+all+elements+of+a+collection&category=code Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  code  coding  string  String.toLowerCase  set  collections Q83. Write java code to Get all words from a String and display them Core Java
Ans. http://javasearch.buggybread.com/CodeSnippets/searchCodeSamples.php?keyword=+Get+all+words+from+a+String&category=code Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  string   get all words from strings   string.split Q84. What will be the output of following code ?
String str1 = "String1";
String str2 = "String1";
System.out.print(str1 == str2);
System.out.print(str1.equals(str2));
str1 = str2;
System.out.print(str1 == str2); Core Java
a. falsetruetrue b. falsefalsetrue c. truetruetrue d. falsefalsefalseAns.c. truetruetrue
a. String b. StringBuffer c. StringBuilder d. None of these create immutable objects.Ans.a. String
a. String b. StringBuffer c. StringBuilder d. None of theseAns.b. StringBuffer
Q87. Which memory segment holds String Pool ? Core Java
a. Stack b. Heap c. Code Segment d. Class SegmentAns.b. Heap
Q88. Which of the following class cannot be extended or inherited ?
StringBuffer , StringBuilder Core Java
a. StringBuffer b. StringBuilder c. Both cannot be extended d. Both can be extendedAns.c. Both cannot be extended