What will be the output of the following program?
public class Test{
public static void main(String args[]){
String str1 = "one";
String str2 = "two";
System.out.println(str1.concat(str2));
}
}
public class Test{
public static void main(String args[]){
String str1 = "one";
String str2 = "two";
System.out.println(str1.concat(str2));
}
}
A. one
B. two
C. onetwo
D. twoone
E. None of these
Answer: Option C
Solution(By Examveda Team)
Concat() method appends one String to the end of another. The method returns a String with the value of the String passed into the method appended to the end of the String which is used to invoke this method.
Join The Discussion
Comments ( 2 )
In Java, which class is used to represent a sequence of characters as a string?
A. String
B. StringBuilder
C. StringSequence
D. StringArray
What is the correct way to create a new empty String object in Java?
A. String emptyString = "";
B. String emptyString = new String();
C. String emptyString = " ";
D. String emptyString = null;
E. Both A and B
Which of the following methods is used to compare two strings for equality in Java?
A. equals()
B. compareTo()
C. equalsIgnoreCase()
D. compare()
the ans given is wrong beacuse str1 and str2 both are in string constant pool and refer diffrent object. so according to the question there is no change int str1 and output will be str1. If str1=str1.concat(str2) was given then the new object of str1 is formed and output will be onetwo.
Since Str1 =one , Str2 = two..both are allocated in constant pool of string pool....if they concate then where do they store with what reference str1 or str2?