File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock question - JQuest Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Mock question - JQuest" Watch "Mock question - JQuest" New topic
Author

Mock question - JQuest

lakshmi nair
Ranch Hand

Joined: Oct 11, 2000
Posts: 63
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).]
Jonathan Jeban
Ranch Hand

Joined: Oct 08, 2000
Posts: 52
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.
Dilip Nedungadi
Greenhorn

Joined: Jun 23, 2000
Posts: 3
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

Joined: Oct 11, 2000
Posts: 63
Thanks both of you.Now i got it.
Lakshmi
 
IntelliJ Java IDE
 
subject: Mock question - JQuest
 
Threads others viewed
Confused??
Loops
Question from Khalid's Book
BItwise & logical operators question
Operator Precedence & Associativity
developer file tools