Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question from javacross.com

 
hans raj
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
anyone tell me the solution for this question found at www.javacross.com
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);�
�������� }
}�

also please suggest me some good books available.
thankyou.
 
Gaurav Goyal
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there are 4 errors in the code
1) the class name cant be short as it is a keyword.
2)argument in the main method is wrong it should be String args[]
3) variable t is declared twice
4) undefined variable i
thanku
 
Mikael Jonasson
Ranch Hand
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing that you did a typo and it is int i=0; not int t=0;
Anyway, the | | operator is design so that if the firts part is true, the second part is never evaluated.
This means that the line b=(t | | ((i++) ==0)); only sets b to true, sice t is true.
The second line, where f is false, evaluates the i+=2 part, seting i to 2. Since 2 is larger then 0, b is asigned true again.
ok?
/Mike
 
hans raj
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thankyou.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the Bunkhouse for good books. Also, you should compile your code before posting. It will help get more meaningful answers.
 
srinivas bolloju
Ranch Hand
Posts: 112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if it was int i=0; in the declaration,
then the answer is 2 when u execute the class right?
 
hans raj
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
thankyou paul for ur suggestion, iam new to this site , i will certainly do it before i post some code.
Hans
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic