Java OCA OCP Practice Question 2440

Question

Given the following code, select the correct options:

public class Main {
    static int foo = 10;
    static boolean calc() {
        ++foo;/* ww w .j  av a  2 s. co m*/
        return false;
    }
    public static void main(String args[]) {
        assert (calc());
        System.out.println(foo);
    }
}
a  If Main is executed using the following command, it will print 11:

   java -enable Main/*w w  w  .j  av a2s.  c  om*/

b  If Main is executed using the following command, it will print 10:

   java -ea Main

c  If Main is executed using the following command, it will print 10:

   java -da Main

d  If  Main  is  executed  using  the  following  command,  it  will  throw  an
   AssertionError and print  11:

   java -enableAssertions Main

e  None of the above


c

Note

Options (a) and (d) are incorrect.

-enable and -enableAssertions are invalid switch options.

The correct switch options to enable assertions are -ea and -enableassertions.

If you use an invalid argument (like -enable) the program will not run, but will exit immediately with an "Unrecognized option" error.

Option (b) is incorrect.

With assertions enabled, assert(calc()) will evaluate to assert(false) and throw an AssertionError, exiting the application, before printing any values.

Option (c) is correct.

With assertions disabled, the assert (calc()) statement is equivalent to an empty statement or a nonexistent line of code.

So method calc() isn't executed, and the value of the static variable foo (10) is printed.




PreviousNext

Related