Java OCA OCP Practice Question 419

Question

Consider the following code:

1. public class MyClass { 
2.   public static void main(String[] args) { 
3.     assert args.length == 0; 
4    } 
5. } 

Which of the following conditions must be true in order for the code to throw an AssertionError?

Assume you are using release 5.0. (Choose all that apply.)

  • A. The source code must be compiled with the -source 1.5 flag.
  • B. The application must be run with the -enableassertions flag or another assertion enabling flag.
  • C. The args array must have exactly zero elements.
  • D. The args array must have one or more elements.


A, B, D.

Note

The 1.4 compiler only treated assert as a keyword and not an ordinary identifier if the -source 1.4 flag appeared in the command line.

JDK 5.0 does not require a -source flag.

So A is not a requirement.

If the application is not run with assertions explicitly enabled, all assert statements will be ignored.

If the args array does not have exactly zero arguments, no AssertionError will be thrown.




PreviousNext

Related