aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Jxam sample question 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Jxam sample question 2" Watch "Jxam sample question 2" New topic
Author

Jxam sample question 2

Metal Zhang
Ranch Hand

Joined: Aug 05, 2001
Posts: 52
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!


Java lover from hell!
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
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

Joined: Aug 05, 2001
Posts: 52
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

Joined: May 08, 2001
Posts: 356
I think answer 3 and 5 are correct, try compiling with the modification asked in option 5.
--Farooq


Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
Metal Zhang
Ranch Hand

Joined: Aug 05, 2001
Posts: 52
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

Joined: Jan 28, 2001
Posts: 153
What's big fuss about line 5. It is straight forward.

tvs sundaram
 
wood burning stoves
 
subject: Jxam sample question 2