Examveda
Examveda

The output of the following fraction of code is
public class Test{
        public static void main(String args[]){
	        String s1 = new String("Hello");
		String s2 = new String("Hellow");
		System.out.println(s1 = s2);
	}
}

A. Hello

B. Hellow

C. Compilation error

D. Throws an exception

E. None of these

Answer: Option B

Solution(By Examveda Team)

The output of the given code will be:

In the code, two String objects s1 and s2 are created with the values "Hello" and "Hellow" (note the typo in "Hellow"), respectively. Then, the System.out.println() statement prints the result of the assignment s1 = s2. This assignment sets the value of s1 to the same value as s2, which is "Hellow". As a result, the println statement prints the value of s1, which is "Hellow".

This Question Belongs to Java Program >> Strings

Join The Discussion

Comments ( 10 )

  1. Kalpana Yamini
    Kalpana Yamini :
    8 months ago

    Here they used s1=s2. So, we get output as Hellow.
    If they give s1==s2.then, we may get false.

  2. Akshay Kumar
    Akshay Kumar :
    2 years ago

    How it is possible, output should be false or true

  3. Abisha Benjamin
    Abisha Benjamin :
    5 years ago

    string s2 is assigned to string s1.So now the s2 (Hellow) is now present in s1 and prints op asHellow

  4. Lavanya Sasapu
    Lavanya Sasapu :
    6 years ago

    String is immutable.
    So reference is allocated.
    That what hellow is the answer

  5. Manoj Kumar
    Manoj Kumar :
    6 years ago

    actually reference of s2 is being assigned to s1. there are Hellow is not being assigned to s1. reference of s2 is being assigned to s1. It means s1 lost its connection with Hello string and connects with Hellow

  6. Ankit Kumar
    Ankit Kumar :
    6 years ago

    here the string s2="hellow" is being assigned to s1.hence s1="hellow" and
    System.out.println(s2==s3);//hellow

  7. Abhilesh Singh
    Abhilesh Singh :
    7 years ago

    what is happening actually here assignment operator(right to left associative)so value of s2 assign to s1 and print s1

  8. Jadeja Ravirajsinh
    Jadeja Ravirajsinh :
    7 years ago

    s1 = s2. first, value of s2 it assign to s1 and it prints value of s1.

  9. SaiDeepak Muthyala
    SaiDeepak Muthyala :
    7 years ago

    Why is this the case, would this not return an error?

  10. SaiDeepak Muthyala
    SaiDeepak Muthyala :
    7 years ago

    Why is this the case, would this not return an error?

Related Questions on Strings