Java OCA OCP Practice Question 2996

Question

Given:

1. public class Main {  
2.   static boolean b;  
3.   static int x = 0;  
4.   public static void main(String[] args) {  
5.     int guess = (int)(Math.random() * 5);  
6.     if(guess < 0) assert false;  
7.     assert b = true;  
8.     assert x = 0;  
9.     assert x == 0;  
10. } } // w w w.  j ava2  s .co m

Which are true? (Choose all that apply.)

  • A. The code compiles.
  • B. The assert on line 6 is appropriate.
  • C. Compilation fails due to an error on line 6.
  • D. Compilation fails due to an error on line 7.
  • E. Compilation fails due to an error on line 8.
  • F. Compilation fails due to an error on line 9.


B and E are correct.

Note

It's considered appropriate, even in public methods, to validate that a code block will not be reached.

Line 8 fails to compile because the first expression in an assert statement must resolve to a boolean value.




PreviousNext

Related