Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Mock question - JQuest

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will be the output of the following code?

(The boolean variable is not initialized,but compiler doesnt give an error, is it a special case with boolean??)
The answer is 3 and 1. But i dont get how j is 1.
[This message has been edited by lakshmi nair (edited October 18, 2000).]
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi lakshmi,
not only booleans u can declared any primitives like that..
For example..


public class My {
public static void main(String args[]) {
int i = 0;
int j = 0;
boolean t = true;
boolean r;
//boolean w;
int y;
r = (t & 0<(i += 1));
System.out.println(i + " " + j);
r = (t && 0<(i += 2));
System.out.println(i + " " + j);
r = (t | 0<(j += 1));
System.out.println(i + " " + j);
r = (t | | 0<(j += 2));
System.out.println(i + " " + j);
//r = (w | | 0<(j += 2));
//System.out.println(i + " " + j);
y = j;
System.out.println(y);
}
}


Here note that y is not initialized.
Also if we remove the commented lines , we will get compiler error... because boolean w is not initialized but used in RHS.
Hope this clarifies ur doubt..
Jeban.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeban is right. Note that r is an automatic variable. It is not necessary to initialize an automatic variable at the time of declaration, but it should be initialized before usage, else a compiler error occurs. In contrast member variables can be used without initialization, in this case the default value of the member variable is used.
Regarding your second question, observe that | | is a shortcircuit Conditional OR operator. The second operand will not be evaluated if the first operand of this operator returns true. So the second expression j+=2 is not evaluated since the first operand t is true. Change t to false and notice the difference.
On the other hand | is a Boolean Logical Operator and both the operands are always evaluated.
Hope this helps
Thanks,
Dilip
[This message has been edited by Dilip Nedungadi (edited October 18, 2000).]
[This message has been edited by Dilip Nedungadi (edited October 18, 2000).]
 
lakshmi nair
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks both of you.Now i got it.
Lakshmi
 
reply
    Bookmark Topic Watch Topic
  • New Topic