ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
info       Did you received any Funny E-Mails from your Friends and like to share with rest of our friends? Yeah!! you can post that stuff   HERE
Google
 
Categories  >>  Software  >>  Core Java  >>  Java J2EE  >>  Java Related
 
 


 

 
 Core Java interview questions  Core Java Interview Questions
 Advanced Java interview questions  Advanced Java Interview Questions
 Swing interview questions  Swing Interview Questions
 EJB interview questions  EJB Interview Questions
 Servlets interview questions  Servlets Interview Questions
 Struts interview questions  Struts Interview Questions
 JDBC interview questions  JDBC Interview Questions
 JMS interview questions  JMS Interview Questions
 SunOne interview questions  SunOne Interview Questions
 J2EE interview questions  J2EE Interview Questions
 Weblogic interview questions  Weblogic Interview Questions
 Websphere interview questions  Websphere Interview Questions
 Java Networking interview questions  Java Networking Interview Questions
 Java J2EE AllOther interview questions  Java J2EE AllOther Interview Questions
Question
String is an immutable object.
Then how can the following code be justified.

String s1 = ?ABC?;
String s1 = s1+?XYZ?;
s.o.p(s1);

The output is ABCXYZ, which is the value of s1 ?

 Question Submitted By :: Biju
I also faced this Question!!     Rank Answer Posted By  
 
  Re: String is an immutable object. Then how can the following code be justified. String s1 = ?ABC?; String s1 = s1+?XYZ?; s.o.p(s1); The output is ABCXYZ, which is the value of s1 ?
Answer
# 1
in the later case a different overloaded contructor is 
invoked while in the prior case a different.As in the later 
case a different constructor initializes the string s1 with 
different values hence the result
 
Is This Answer Correct ?    1 Yes 1 No
Vishal
 
  Re: String is an immutable object. Then how can the following code be justified. String s1 = ?ABC?; String s1 = s1+?XYZ?; s.o.p(s1); The output is ABCXYZ, which is the value of s1 ?
Answer
# 2
here we define s1="ABC".That means one new string is 
created.And this string value is stored in heap its 
refrence variable is s1.when we say s1+"XYZ".Then string s1 
i.e ABC and XYZ are concadinated by using concadination 
operater.It forms a new string i.e "ABCXYZ" in heap.But 
again we assign this value to the refrence variable 
s1.Then "ABCXYZ" value is assign to s1 and the string "ABC" 
which is garbage collected.
 
Is This Answer Correct ?    4 Yes 0 No
K.shiva
 
 
 
  Re: String is an immutable object. Then how can the following code be justified. String s1 = ?ABC?; String s1 = s1+?XYZ?; s.o.p(s1); The output is ABCXYZ, which is the value of s1 ?
Answer
# 3
Compilation ERROR
Duplicate Variable s1.
 
Is This Answer Correct ?    0 Yes 1 No
Ramesh
 
  Re: String is an immutable object. Then how can the following code be justified. String s1 = ?ABC?; String s1 = s1+?XYZ?; s.o.p(s1); The output is ABCXYZ, which is the value of s1 ?
Answer
# 4
above program is incorrect ,it will give compile time error 
as "Duplicate Variable s1".
it should b 
String s1 = "ABC";
String s = s1+"XYZ";
which is valid in string ,as we are creating new String 
object
 
Is This Answer Correct ?    2 Yes 0 No
Paras Bist
 
  Re: String is an immutable object. Then how can the following code be justified. String s1 = ?ABC?; String s1 = s1+?XYZ?; s.o.p(s1); The output is ABCXYZ, which is the value of s1 ?
Answer
# 5
It will Give Compile time Error...

We can not declare again s1. 

So the Compiler will give

      s1 is already defined in main(java.lang.String[])
 
Is This Answer Correct ?    0 Yes 0 No
Sivadasan
 

 
 
 
Other Core Java Interview Questions
 
  Question Asked @ Answers
 
Can an unreachable object become reachable again?  3
Which containers use a FlowLayout as their default layout?  3
Define interface?  2
Why do we use public static with the main function in Java? Infosys7
Does garbage collection guarantee that a program will not run out of memory?  1
What is the difference between set and list?  3
Difference between array and arraylist. Sai-Softech4
While opening the file, what type of exceptions can be caught?  3
AWT event listeners extends what interface?  1
Explain Event handling in AWT? TCS1
What is difference between hash mapping and hash table? NDS3
Why Java is a platform independent language? Zensar4
how can we import the user defined package without classpath?  1
Explain the difference between the Boolean & operator and the && operator?  1
what is Thread?  6
What is a Java package and how is it used?  2
How are this and super used?  6
What are batch updates. in jdbc Corent-Technology2
How the elements are organized in CardLayout?  5
how to handle exceptions in core applications? CSS2
 
For more Core Java Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com