What will be the result of attempting to compile and run the following class?
public class Main{ public static void main (String args []){ if (true) if (false) System .out.println ("True False"); else System .out.println ("True True"); } }
Select 1 option
Correct Option is : D
This code can be rewritten as follows:
public class Main{ public static void main (String args []) { if (true) { if (false) { System .out.println ("True False"); } else { System .out.println ("True True"); } // ww w .ja v a 2s . c o m } } }
Notice how the last "else" is associated with the last "if" and not the first "if".
Now, the first if condition returns true so the next 'if' will be executed.
In the second 'if' the condition returns false so the else part will be evaluated which prints 'True True'.