Exam 1z0-809 All QuestionsBrowse all questions from this exam
Question 6

Given:

public class Counter {

public static void main (String[ ] args) {

int a = 10;

int b = -1;

assert (b >=1) : "Invalid Denominator";

int Ñ = a / b;

System.out.println (c);

}

}

What is the result of running the code with the ""ea option?

    Correct Answer: C

    The given code utilizes the assert statement to validate the condition that the denominator b is greater than or equal to 1. In the code, b is assigned the value -1, which violates this condition. When assertions are enabled using the -ea (enable assertions) option, the JVM evaluates assert statements, and if the condition in the assert statement is false, it throws an AssertionError. Therefore, the program will throw an AssertionError with the message 'Invalid Denominator' when run with the -ea option.

Discussion
Temilaw25Option: C

Correct answer is C. The moment assertion is enabled, this happens: In the code, the condition in the assert statement is b >= 1. This condition is false because b is assigned the value -1. Therefore, when the JVM encounters this assert statement (with assertions enabled via the "ea" option), it throws an AssertionError with the message "Invalid Denominator". Assertions are disabled by default in Java. The "ea" option is a command-line switch that can be used to enable assertions during program execution.

WilsonKKerllOption: C

Use -ea option: java -ea className Answer is C.

akitravvOption: D

D. Compilation error - since c is not declared. System.out.println(Ñ); gives -10 as output

steefaandOption: C

Assuming there are two typos: 1. int Ñ = a / b; is actually int c= a/b 2. java ""ea is actually java -ea Answer is C.

push05Option: C

C:\cjava\809>javac Counter.java C:\cjava\809>java -ea Counter Exception in thread "main" java.lang.AssertionError: Invalid Denominator at Counter.main(Counter.java:5) C:\cjava\809>java -da Counter -10

Kim514Option: A

Answer is A -ea to enable assertions With this option, assertions can be enabled (by default, they are turned off). In a language that supports assertions, the programmer can add runtime conditional checks. In Java, this is done by adding an assert statement, followed by a condition. When assertions are disabled, these statements are ignored completely, but when enabled, the JVM throws an error if the condition turns out to be false. This can be used to check whether the program works as expected. An example of an assert statement in Java: int i = 25; assert i < 24; When assertions are enabled with the -ea option, the preceding code will result in a java.lang.Error instance thrown by the JVM once the assert statement runs. When not explicitly specified ...

Kim514

Correct answer is C! (sorry)

dexdinh91Option: A

A, tested in console

dexdinh91

""ea is not -ea

9641Option: A

int c = a / b;

asdfjhfgjuaDCVOption: C

C is the answer

shivkumarxOption: C

Correct answer is C assuming question and code have typos

r1muka5Option: D

Answer is D. Compilation error - since c is not declared. There is no way this code block would get compiled if copied/pasted to console.

Svetleto13Option: A

A,tested

Svetleto13

Correct answer is C,because assertions are enabled.If they are disabled answer is A.There is one more thing - int = a/b needs to be changed to int c = a/b ,if not,leads to compilation error,

lxlOption: A

A, tested