Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Jxam sample question 2

 
Metal Zhang
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is:

Consider the following piece of code and select the correct statements.
1. Object o = new String("abcd");
2. String s = o;
3. System.out.println(s);
4. System.out.println(o);

The answers are:

1,The following is displayed:
abcd
abcd
2,The code fails to compile at line 1.
3,The code fails to compile at line 2.
4,The code fails to compile at line 4.
5,The code can be made to compile by changing line 1 to the following:
String o = new String("abcd");

The right answer is 3.But I think they are 3,4 and 5.Any one can explain?
Thanx!
------------------
Java lover from hell!
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3 is not compilable because we can not assign a reference Object to a reference of String. It is going down the heirachy. Why do you feel that 4 and 5 are wrong? Both provide a toString() method and can provide String context for the out stream.
 
Metal Zhang
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O,Can the object of class java.lang.Object convert itself to String object by invoking Object.toString automatically?
Originally posted by Cameron Park:
3 is not compilable because we can not assign a reference Object to a reference of String. It is going down the heirachy. Why do you feel that 4 and 5 are wrong? Both provide a toString() method and can provide String context for the out stream.

------------------
Java lover from hell!
[This message has been edited by Metal Zhang (edited August 23, 2001).]
 
Muhammad Farooq
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think answer 3 and 5 are correct, try compiling with the modification asked in option 5.
--Farooq
 
Metal Zhang
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes,I think you are right.I tried it!
Originally posted by Muhammad Farooq:
I think answer 3 and 5 are correct, try compiling with the modification asked in option 5.
--Farooq


------------------
Java lover from hell!
 
tvs sundaram
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's big fuss about line 5. It is straight forward.

tvs sundaram
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic