Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Valentins Mock Exam Question 6

 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(b!=b)
this asks if b is not equal to b. Obviously b is equal to b so this is false.
(i=args.length)
Assuming you entered no arguments when you ran this, args.length = 0 and that is what is assigned to i
(b=i==2));
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 ]
 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks...
 
Gautam Jha
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic