What could be output of the following fragment of code?
public class Test{
public static void main(String args[]){
String x = "hellow";
int y = 9;
System.out.println(x += y);
}
}
public class Test{
public static void main(String args[]){
String x = "hellow";
int y = 9;
System.out.println(x += y);
}
}
A. Throws an exception as string and int are not compatible for addition
B. hellow9
C. 9hellow
D. Compilation error
E. None of these
Answer: Option B
Join The Discussion
Comments ( 8 )
Related Questions on Strings
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()
yes, we can add two different datatype and also concate with the string
String s="Java";
int a=10;
float b=10.6f;
System.out.println(s+a+b);
I have a boubt can we add different data types??
x +=y that implies x=x+y
=>x=hellow
=> y=9
x=hellow+9
answer: hellow9
9 is concatenated with hellow
plz give the solution.............
Hi Arpitha,
Here, program is printing value of a variable with System.out.println. So, need of statement under " ". You can run this program by yourself you can understand it.
Here println = Print with a new line.
Yes it is an error. Because in the printing statement " " are missing.
So it will not print.
Yes Mr. Samil you are correct we are updating our answer. Thanks!
Wrong answer,Correct answer is B.