Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question from anilbachi site

 
madhu kumar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi this question is from http://www.anilbachi.8m.com .
Please help me with solution.
Q}State the output of the following code
public class short{
public static void main(String args()){
int t=0;
boolean t=true;
boolean f=false,b;
b=(t | | ((i++) ==0));
b=(f | | ((i+=2)>0));
System.out.println(i);
}}
a).0
b).1
c).2
d).3
I think the answer is 2. Am I right. Please help me.
 
Kalpesh Pandya
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
First of all instead of int t = 0, it should be int i = 0.
If it is so, you are right, what's the problem ???
in first statement: b=(t | | ((i++) ==0)), as t is true, i++ never get executed, so i remains 0. rest of it is simple.
 
madhu kumar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks kalpesh
 
Noel Hinders
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I too guess that it should be int i=0 then the answer would be 0.
Noel
 
Jonathan Li
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually the question is wrong. the code does not compile at all because you're using a keyword (short) as the class name.
 
Golam Newaz
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I agree with Jonathan. Question is wrong as it has been
used keyword ( short ). Output is nothing. Yes if you
correct variable then you would get 2.
- Golam Newaz
 
Noel Hinders
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
vow I did not even notice it,
I guess that makes the difference
Noel
 
venu gopal
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Madhu,
The answere is 2. There should not be a space between short circuit operator. Please keep in mind while executing the code.
public class short1{
public static void main(String args[]){
int i=0;
boolean t=true;
boolean f=false,b;
b=(t | | ((i++) ==0));
System.out.println(b);
b=(f | | ((i+=2)>0));
System.out.println(b);
System.out.println(i);
}
}


[This message has been edited by venu gopal (edited February 06, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic