• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string with null value

 
Vicky Makhija
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What is the result?
A. null
B. zero
C. some
D. Compilation fails.

The answer to the above is "D"...
Can anyone explain me why the answer is D ??

[edit]Add code tags. CR[/edit]
[ October 01, 2008: Message edited by: Campbell Ritchie ]
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vicky M:
public static void main(String[] args) {
String str = "null";
if (str == null) {
System.out.println("null");
} else (str.length() == 0) {
System.out.println("zero");
} else {
System.out.println("some");
}
}
What is the result?
A. null
B. zero
C. some
D. Compilation fails.

The answer to the above is "D"...
Can anyone explain me why the answer is D ??


What syntax did you get wrong? Hint: it is in your conditionals.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Vicky M",

Please read your private messages regarding an important announcement.

Thank you,

Rob


As for the answer, type this in and compile it. It will give you three errors, the first one is the line where the problem is.
 
ruban raj
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if we want to check more than one condition . . . [deleted. CR]
}
i hope this code will help you.


Thanks and regards,
ruban

[ October 01, 2008: Message edited by: ruban raj ]

[edit]Deleted solution. CR[/edit]
[ October 01, 2008: Message edited by: Campbell Ritchie ]
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ruban Raj, thank you for the solution, but I would remind you what it says when you enter the beginners' forum
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
It would have helped Vicky much more to have found the error herself; that way she would remember it far better.

I have felt it right to edit your post and delete the solution. Sorry if you think I have messed you around.
 
NehaJain Jain
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vicky Makhija wrote:
What is the result?
A. null
B. zero
C. some
D. Compilation fails.

The answer to the above is "D"...
Can anyone explain me why the answer is D ??

[edit]Add code tags. CR[/edit]
[ October 01, 2008: Message edited by: Campbell Ritchie ]
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NehaJain Jain,

Please read the ENTIRE thread before you reply. Had you done so, you'd have seen Campbell's post at 9:13 a.m., stating that we prefer to HELP people find the solutions, not just hand them out.

I removed the answer from your post.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It wasn’t 9.13. It was 3.13pm. On 1st October.




2008, that was.

I hope it didn't take OP 4½ years to find the missing if?
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damn time-zones...
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Vicky,
As steve rightly pointed out "look carefully at your conditional expression", there is something amiss there
If you are unable to find it, then try to decipher the error thrown by JVM, Understanding error thrown by JVM can go a long way in understanding the syntax and other innate functions of the language
Cheers :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic