• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

question from anilbachi site

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks kalpesh
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I too guess that it should be int i=0 then the answer would be 0.
Noel
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
vow I did not even notice it,
I guess that makes the difference
Noel
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic