What is the result?

Given:

And the commands:
javac Counter.java
java ea Counter
What is the result?

Given:

And the commands:
javac Counter.java
java ea Counter
What is the result?

A.
2

B.
3

C.
NullPointException is thrown at runtime

D.
AssertionError is thrown at runtime

E.
Compilation fails

Explanation:

The command line javac Counter.java
Willcompile the code.

The command line java ea Counter
Willrun the cod with assertions enabled.
Assertion is true because getCount(arr) = 3 and Length of array is 4
The following line:
assert (getCount(arr) < arr.length);
where the Boolean expression getCount(arr) < arr.length will evaluate to false, will ensure that
anAssertionError is thrown at runtime.
Note:The javac command compiles Java source code into Java bytecodes. You then use the Java
interpreter -the java command – to interprete the Java bytecodes.
Note 2:The java tool launches a Java application. It does this by starting a Java runtime
environment, loading aspecified class, and invoking that class’s main method. The method
declaration must look like the following:public static void main(String args[])
Paramater ea:
-enableassertions[:<package name>”…” | :<class name> ] -ea[:<package name>”…” | :<class
name> ]
Enable assertions. Assertions are disabled by default. With no arguments, enableassertions or -ea
enablesassertions.
Note 3:
An assertion is a statement in the JavaTM programming language that enables you to test your
assumptionsabout your program.
Each assertion contains a boolean expression that you believe will be true when the assertion
executes. If it isnot true, the system will throw an error.



Leave a Reply 3

Your email address will not be published. Required fields are marked *


Bahaa Khateib

Bahaa Khateib

correct

Krzysztof Gołębiowski

Krzysztof Gołębiowski

Answer B is correct.