jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Valentins Mock Exam Question 6 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Valentins Mock Exam Question 6" Watch "Valentins Mock Exam Question 6" New topic

Valentins Mock Exam Question 6

sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
public class Question07 {
public static void main(String[] args) throws Exception {
int i=2;
boolean b = true;
throw new Exception("Values are:"+(b!=b)+","+(i=args.length)+","+(b=i==2));
When I ran this code it gave me

ava.lang.Exception: Values are:false,0,false
at Question07.main(Question07.java:340)
Exception in thread "main"
Process completed with exit code 1
Why is it so?

Sunita<br />SCJP 1.4
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
this asks if b is not equal to b. Obviously b is equal to b so this is false.
Assuming you entered no arguments when you ran this, args.length = 0 and that is what is assigned to i
This asks if i is equal to 2. i is equal to 0 (see above) so that is false and that value (false) is assigned to b.
[ October 07, 2002: Message edited by: Thomas Paul ]

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
Gautam Jha

Joined: Sep 23, 2002
Posts: 10
throw new Exception("Values are:"+(b!=b)+","+(i=args.length)+","+(b=i==2));
b != b is always false
i=args.length: i is assigned 0
b=i==2 translates to (b =(i==2)). i value is 0, so i==2 is false, which is assigned to b.
Hence the answer.
I agree. Here's the link: http://aspose.com/file-tools
subject: Valentins Mock Exam Question 6
It's not a secret anymore!