This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock 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 question" Watch "Mock question" New topic
Author

Mock question

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi!
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 code compiles but throws exception when run but I don�t understand why. Can someone explain me.
Thank you in advance.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
The answer in this thread
http://www.javaranch.com/ubb/Forum24/HTML/011944.html

SCJP2. Please Indent your code using UBB Code
 
jQuery in Action, 2nd edition
 
subject: Mock question
 
Similar Threads
Jiris Mock 1, Q1
strange behaviour
Does the toString() method cannot return null?
a highly recommended mock site: http://www.jiris.com/mock
Mock exam question