wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock exam question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mock exam question" Watch "Mock exam question" New topic
Author

Mock exam question

bani kaali
Ranch Hand

Joined: May 05, 2002
Posts: 42
I came across this question in one of the mock exams.
class Test001
{
int i;
public Test001(int i) { this.i = i; }
public String toString()
{
if(i == 0) return null;
else return "" + i;
}
public static void main(String[ ] args)
{
Test001 t1 = new Test001(0);
Test001 t2 = new Test001(2);
System.out.println(t1); //1
System.out.println(t2); //2
}
}
The answer is : throws exception at runtime.
can some please explain this to me?
thanks
bani
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
No, because it's wrong. Where did you find this question? The program prints
null
2
If the idea is that System.out.println(x) is
supposed to throw a NullPointerException
if x.toString() returns null.... well, it doesn't. It prints the string "null".
[ August 16, 2002: Message edited by: Ron Newman ]

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Ian Wayne
Greenhorn

Joined: Aug 03, 2002
Posts: 8
It seems PrintStream has only the following methods:
PrintStream.println(), PrintStream.println(boolean), PrintStream.println(char), PrintStream.println(char[]), PrintStream.println(double), PrintStream.println(float), PrintStream.println(int), PrintStream.println(long), PrintStream.println(java.lang.Object), PrintStream.println(java.lang.String)
There's no PrintStream.println(null).


Ian
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
You're not calling println(null) ... although it's perfectly safe and OK to do so.
You're calling println(Object) , which in turn is calling your object's toString() method, which is returning null. At that point it's smart enough to print the string "null" rather than throwing a NullPointerException.
Rodge Thomas
Ranch Hand

Joined: Jul 25, 2002
Posts: 38
This question was giving me trouble, too. I still am unsure. The question is from JIRIS.com
toString returns a String. I believe when you attempt to print a null reference that the PrintStream System.out calls toString and then toString will send the string "null" back to System.out. But, in this case toString is actually returning the value null, rather than the string "null". toString is supposed to return a String. So, I would expect that both
System.out.println(t1);
System.out.println(t1.toString());
give errors. But, a bug in the JDK allows the first one to work. Well, any insight on if I am right/wrong will be greatly appreciated.
Here is the explanation following that question at http://www.jiris.com/scjp2/mockexam1answer.html:
: It will throw the following exception at runtime at //1.

java.lang.NullPointerException
at java.io.Writer.write(Writer.java:129)
at java.io.PrintStream.write(PrintStream.java:267)
at java.io.PrintStream.print(PrintStream.java:426)
at java.io.PrintStream.println(PrintStream.java:563)
at Test001.main(Test001.java:18)
But it is strange that
System.out.println(t1.toString());
will run properly with output null.
--------------
Words from Adrian Uribe about this question:

Question 1 on this mock exam seems to address a bug in the jdk. The explanation shows what is output from running the code but it does not seem logical that this is what should happen.
-------------
Words from Dallas Hockley about this question:

I agree that that seems to be an inconsistency or a bug on the JDK.
System.out.println(t1.toString()) should be the same as
System.out.println(t1) as I understand the automatic typecast/method
call in that situation. I've verified it on 1.3.1 on Windows, and on
1.4 Beta 2 on Windows. Very odd.
bani kaali
Ranch Hand

Joined: May 05, 2002
Posts: 42
I found this question on jiris.com
I was thinking the pgm will print null and 2
but the explanation given by jiris is entirely different.
Its strange that
System.out.println(t1.toString());
will run properly with output null.
bani kaali
Ranch Hand

Joined: May 05, 2002
Posts: 42
when i change line 1 to
System.out.println("" + t1)
then i getting the result as "null" .
In case of line 1 ie
System.out.println(t1)

Here System.out.println method will try and call toString method on null. Which results in a null pointer exception!
Correct me if i am wrong.
bani
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
I ran this on Java 1.1.8 on MacOS 8.6. Are you saying that later versions of Java act differently?
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
System.out is a PrintStream.
From the J2SE APi documentation at
http://java.sun.com/j2se/1.4/docs/api/java/io/PrintStream.html:

public void println(Object�x)
Print an Object and then terminate the line. This method behaves as though it invokes print(Object) and then println().
public void print(Object�obj)
Print an object. The string produced by the String.valueOf(Object) method is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

OK, let's check out the documentation of
String.valueOf(Object) at
http://java.sun.com/j2se/1.4/docs/api/java/lang/String.html :

public static String valueOf(Object�obj)
Returns the string representation of the Object argument.
Parameters:
obj - an Object.
Returns:
if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.

This seems to leave open to question (and maybe to implementation?) exactly what happens when the null pointer "is translated into bytes according to the platform's default character encoding, and these bytes are written".
[ August 16, 2002: Message edited by: Ron Newman ]
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi All:
I have jdk1.3.1_01. The following line:
System.out.println(t1)
gives run time error. I think the overriding
toString method is returning null reference instead
of a string which is probably a valid return
type for toString method. But when println()
tries to print a null reference, it causes a
run time error. However following statements:
System.out.println(""+t1)
will call toString method second time on t1. The
second time null reference will become "null". I
guess same is the case with :
System.out.println(t1.toString)
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Could someone who has 1.4 installed please run the program and tell us what it does? Also tell us what sub-version of 1.4, and what OS it's running on.
Brian Dang
Greenhorn

Joined: Aug 13, 2002
Posts: 4
hi, my system is running j2sdk1.4.0_01 and the following result I got when executing the commands:

1. System.out.println(t1) // 1
// compiler did not complain, but a NullPointerException was thrown during runtime
2. System.out.println(t1 + "")
OR
System.out.println(String.valueOf(t1))
//the above commands had the same effect and it print:
null
2
Rahul Phadnis
Greenhorn

Joined: Aug 13, 2002
Posts: 7
With JDK 1.4 beta I get the exception
The jdk version info is
java version "1.4.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)
I am running it on Windows 2000 machine
The exception is
java.lang.NullPointerException
at java.io.Writer.write(Writer.java:129)
at java.io.PrintStream.write(PrintStream.java:306)
at java.io.PrintStream.print(PrintStream.java:465)
at java.io.PrintStream.println(PrintStream.java:602)
at com.rahul.certify.java.TestToString.main(TestToString.java:27)
Exception in thread "main"
The code I used to test is
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Mock exam question
 
Similar Threads
Jiris Mock 1, Q1
strange behaviour
Does the toString() method cannot return null?
Mock question
a highly recommended mock site: http://www.jiris.com/mock