Given the following class
public class Ombersley{
public static void main(String argv[]){
boolean b1 = true;
if((b1 ==true) || place(true)){
System.out.println("Hello Crowle");
}
}
public static boolean place(boolean location){
if(location==true){
System.out.println("Borcetshire");
}
System.out.println("Powick");
return true;
}
}
What will happen when you attempt to compile and run it?
1) Compile time error
2) Output of "Hello Crowle"
3) Output of Hello Crowle followed by Borcetshire and Powick
4) No output
Answer :
2) Output of "Hello Crowle"
This code is an example of a short circuited operator. Because the first operand of the || (or) operator returns true Java sees no reason to evaluate the second. Whatever the value of the second the overall result will always be true. Thus the method called place is never called.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment