Java OCA OCP Practice Question 2520

Question

How many compilation issues are in the following code?

1:    public class Main { 
2:       class RainException extends Exception {} 
3: //  w  w w  . j  a  v a2s .  c  o m
4:       public static void main(String[] args) { 
5:          try(Scanner s = new Scanner("rain"); String line = "";) { 
6:             if (s.nextLine().equals("rain")) 
7:                throw new RainException(); 
8:          } finally { 
9:             s.close(); 
10:         } 
11:     } 
12:   } 
  • A. 0
  • B. 1
  • C. 2
  • D. 3
  • E. 4
  • F. 5


D.

Note

Line 5 is incorrect because String does not implement AutoCloseable.

Not all objects can be declared in a try-with-resources try clause.

Line 7 is incorrect because RainException is a checked exception and is not declared or handled.

Line 9 is incorrect because s is declared in the try clause and is therefore out of scope for the finally block.




PreviousNext

Related