Sunday, December 9, 2012

SCJP 6 Questions - Question 63

 class C {  
   public static void main ( String ka [ ] ) {  
     while ( false ) {  
       System.out.println ( "Value" ) ;  
     }  
   }  
 }  

What is the output?
1. compile time error
2. prints Value infinitely
3. Runtime Exception
4. None of the above



Explanation:
while(false) always generates a compile time error "java.lang.Error: Unresolved compilation problem: Unreachable code", since the compiler has an algorithm for checking for non-reachable code. If(false) does not generate an error, because this was an exception made for the compiler, to accept conditional code.
Check CodeRanch for more explanations.

Ans: 1

No comments:

Post a Comment