Java OCA OCP Practice Question 1797

Question

Given the following class, which command causes the class to throw an AssertionError at runtime?

public class Main extends Exception {
   private int c = 12;
   public Main(String name) {
      super(name);
   }
   public static void main(String[] aluminum) {
      assert new Main(null).c<12;
   }
}
  • A. java Main
  • B. java -ea -da:Main Main
  • C. java -da -ea:Main Main
  • D. The code does not compile.


C.

Note

The code compiles without issue, making Option D incorrect.

Option A is incorrect because assertions are not enabled by default in Java.

Therefore, the code will run without throwing any exceptions.

Option B is also incorrect because the command enables assertions everywhere but disables them for the Main class.

Option C is the correct answer, with the command disabling assertions everywhere except in the Main class, causing an AssertionError to be thrown at runtime.




PreviousNext

Related