• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock Exam Q

 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The three objects are "Hello", "Pal", and "HelloPal"
 
Sheetal Kaul
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Olivier Lambert
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Atiquzzaman Nayeem
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
seemapanth Joshi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic