Java OCA OCP Practice Question 2573

Question

Consider the following program:

class Base {/*  w  ww .  j  av a 2s.c  o m*/
        public void foo() {
                assert true;   // ASSERT_BASE
        }
}

class Derived extends Base {
        public void foo() {
                assert false; // ASSERT_DERIVED
        }
}

public class Main {
        public static void main(String []args) {
                try {
                        Base base = new Base();
                        base.foo();
                }
                catch(Exception e) {
                        base = new Derived();
                        base.foo();
                }
        }
}

From the command line, this program is invoked as follows:

java -ea -da:Derived Main

Which one of the following options correctly describes the behavior of this program when it is run?

  • a) This program crashes throwing an AssertionError in line marked with comment ASSERT_BASE.
  • b) This program crashes throwing an AssertionError in line marked with comment ASSERT_DERIVED.
  • c) This program first prints "Caught exception" and then crashes throwing an AssertionError in line marked with comment ASSERT_DERIVED.
  • d) This program completes execution normally without producing any output or throwing any exceptions.


d)

Note

The statement assert true; when executed will always succeed.

The statement assert true; when executed will always fail.

Remember that assertions are disabled by default, and -ea enables the assertion for the whole program.

However, -da disables assertions, and -da:Derived instructs the JVM to disable assertions in the Derived class.

Hence, the program completes execution normally without producing any output or throwing any exceptions.




PreviousNext

Related