Java OCA OCP Practice Question 2824

Question

Which of the following are true of the code? (Choose all that apply.)

4:    private int addPlusOne(int a, int b) { 
5:       boolean assert = false; 
6:       assert a++ > 0; 
7:       assert b > 0; 
8:       return a + b; 
9:    } 
  • A. Line 5 does not compile.
  • B. Lines 6 and 7 do not compile because they are missing the String message.
  • C. Lines 6 and 7 do not compile because they are missing parentheses.
  • D. Line 6 is an appropriate use of an assertion.
  • E. Line 7 is an appropriate use of an assertion.


A, E.

Note

Line 5 does not compile because assert is a reserved word making Choice A correct.

B and C are incorrect because the parenthesis and message are both optional.

D is incorrect because assertions should not have side effects.

E is correct because checking an argument passed from elsewhere in the program is an appropriate use of an assertion.




PreviousNext

Related