| Author |
Mock Exam Q
|
Nitin Bhagwat
Ranch Hand
Joined: Sep 09, 2004
Posts: 132
|
|
This is from Valiveru's Exam Q.No.56: How many String objects are created when we run the following code. String s1,s2,s3,s4; s1 = "Hello"; s2 = s1; s3 = s2 + "Pal"; s4 = s3; A.1 B.2 C.3 D.4 E.We can't say. Ans given is C. I think it is B. S1 creates a new string object - Object 1. S2 points to same object as S1. S3 creates new string S2+"Pal" - Object 2 S4 points to same object as S3. Please confirm. Thank you in advance.
|
"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
|
 |
Mike Gershman
Ranch Hand
Joined: Mar 13, 2004
Posts: 1272
|
|
|
The three objects are "Hello", "Pal", and "HelloPal"
|
Mike Gershman
SCJP 1.4, SCWCD in process
|
 |
Sheetal Kaul
Ranch Hand
Joined: Nov 29, 2004
Posts: 47
|
|
Hi, My ans is 4, coz firstly v r creating 4 objects, then pointing the 1 object to the other is the secondary thing. As per my knowledge the answer is 4objects. Plz correct me if i'm going wronge. Thanx
|
 |
Mike Gershman
Ranch Hand
Joined: Mar 13, 2004
Posts: 1272
|
|
There have been several posts pointing out that the custom on this bulletin board is to use ordinary English. It's not necessary to have perfect grammar or spelling, but I feel that your strange abbreviations show disrespect for other JavaRanch members. You suggest that four objects were created in the code fragment. Please name them so we can understand your reasoning.
|
 |
Sheetal Kaul
Ranch Hand
Joined: Nov 29, 2004
Posts: 47
|
|
Sorry, I have used abbrevations, so sorry for that. that was my first post for the javaranch, i promise i wont repeat this again. Thanks.
|
 |
Atiquzzaman Nayeem
Greenhorn
Joined: Nov 23, 2004
Posts: 6
|
|
I'm confused. I think the answare is 2. Mr. Mike, could you explain why it is 3? regards Atiq [ November 30, 2004: Message edited by: Atiquzzaman Nayeem ]
|
Programmer<br />Software Development Center<br />East West University<br />Bangladesh
|
 |
Olivier Lambert
Greenhorn
Joined: Apr 30, 2004
Posts: 9
|
|
String s1,s2,s3,s4; => Nothing is created s1 = "Hello"; => String object "Hello" is created s2 = s1; => Nothing is created s3 = s2 + "Pal"; => String objects "Pal" and "HelloPal" are created s4 = s3; => Nothing is created => 3 objects are created, "Hello", "Pal" and "HelloPal". Have a nice day!
|
Oli,<br />SCJP
|
 |
Atiquzzaman Nayeem
Greenhorn
Joined: Nov 23, 2004
Posts: 6
|
|
Hi Oli, Now I can understand. Thanks. You r a SCJP. Nice. I want to be a SCJP. So please give me some idea. regards Atiq
|
 |
Soumy Kumar
Ranch Hand
Joined: Nov 02, 2004
Posts: 78
|
|
Sorry i am not able to understand how 3 objects are created.. To which object is " Pal " assigned... As per precedence, + operator is executed and then = ... Help me please... bye
|
SCJP 1.4<br />" Something is difficult doesn't mean you shouldn't try, it only means you should try harder "
|
 |
seemapanth Joshi
Ranch Hand
Joined: Nov 28, 2004
Posts: 47
|
|
When java encounters anything between " ", it creates a string object. I would suggest the following reference : http://java.sun.com/docs/books/tutorial/java/data/stringsAndJavac.html [ December 03, 2004: Message edited by: seemapanth Joshi ]
|
 |
Mike Gershman
Ranch Hand
Joined: Mar 13, 2004
Posts: 1272
|
|
Sow asked:
To which object is " Pal " assigned...
There are many cases in Java where an object is created but not assigned to any variable. In the canonical (I love that word!) System.out.println( "Hello World!" ), a String object containing Hello World! is created and a reference to it is passed as an argument to method println(), but no referenvce variable is used. Wait until you get to anonymous inner classes and you'll see how much can be done without a reference variable or a proper class name!
|
 |
Atiquzzaman Nayeem
Greenhorn
Joined: Nov 23, 2004
Posts: 6
|
|
We all know about garbage collection. There are undefined number of garbage are collected during an application run. So, some objects are created but have no any physical reference in the application. Atiq
|
 |
 |
|
|
subject: Mock Exam Q
|
|
|