Examveda
Examveda

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);
        } 
}

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


This Question Belongs to Java Program >> Strings

Join The Discussion

Comments ( 8 )

  1. Umesh Singh
    Umesh Singh :
    1 year ago

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

  2. Mukhesh Sure
    Mukhesh Sure :
    3 years ago

    I have a boubt can we add different data types??

  3. Aishu R
    Aishu R :
    7 years ago

    x +=y that implies x=x+y
    =>x=hellow
    => y=9
    x=hellow+9
    answer: hellow9
    9 is concatenated with hellow

  4. Smita Raut
    Smita Raut :
    7 years ago

    plz give the solution.............

  5. Kumar Chandan
    Kumar Chandan :
    8 years ago

    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.

  6. Arpitha L
    Arpitha L :
    8 years ago

    Yes it is an error. Because in the printing statement " " are missing.
    So it will not print.

  7. Examveda
    Examveda :
    8 years ago

    Yes Mr. Samil you are correct we are updating our answer. Thanks!

  8. Şamil Kahraman
    Şamil Kahraman :
    8 years ago

    Wrong answer,Correct answer is B.

Related Questions on Strings