Review Questions

graphics/rq_icon.gif

5.24

Assuming assertions are enabled, which of these assertion statements will throw an error?

Select the two correct answers.

  1. assert true : true;

  2. assert true : false;

  3. assert false : true;

  4. assert false : false;

5.25

Which of the following are valid runtime options?

Select the two correct answers.

  1. -ae

  2. -enableassertions

  3. -source 1.4

  4. -disablesystemassertions

  5. -dea

5.26

What is the class name of the exception thrown by an assertion statement?

Select the one correct answer.

  1. Depends on the assertion statement.

  2. FailedAssertion

  3. AssertionException

  4. RuntimeException

  5. AssertionError

  6. Error

5.27

What can cause an assertion statement to be ignored?

Select the one correct answer.

  1. Nothing.

  2. Using appropriate compiler options.

  3. Using appropriate runtime options.

  4. Using both appropriate compiler and runtime options.

5.28

Given the following method, which statements will throw an exception, assuming assertions are enabled?

static int inv(int value) {
    assert value > -50 : value < 100;
    return 100/value;
}

Select the two correct answers.

  1. inv(-50);

  2. inv(0);

  3. inv(50);

  4. inv(100);

  5. inv(150);

5.29

Which runtime options would cause assertions to be enabled for the class org.example.ttp.Bottle?

Select the two correct answers.

  1. -ea

  2. -ea:Bottle

  3. -ea:org.example

  4. -ea:org...

  5. -enableexceptions:org.example.ttp.Bottle

  6. -ea:org.example.ttp

5.30

What will be the result of compiling and running the following code with assertions enabled?

public class TernaryAssertion {
    public static void assertBounds(int low, int high, int value) {
        assert ( value > low ? value < high : false )
            : (value < high ? "too low" : "too high" );
    }
    public static void main(String[] args) {
        assertBounds(100, 200, 150);
    }
}

Select the one correct answer.

  1. The compilation fails because the method name assertBounds cannot begin with the keyword assert.

  2. The compilation fails because the assert statement is invalid.

  3. The compilation succeeds and the program runs without errors.

  4. The compilation succeeds and an AssertionError with the error message "too low" is thrown.

  5. The compilation succeeds and an AssertionError with the error message "too high" is thrown.

5.31

Which statements are true about the AssertionError class?

Select the two correct answers.

  1. It is a checked exception.

  2. It has a method named toString().

  3. It has a method named getErrorMessage().

  4. It can be caught by a try-catch construct.

5.32

Which of these classes is the direct superclass of AssertionError?

Select the one correct answer.

  1. Object

  2. Throwable

  3. Exception

  4. Error

  5. RuntimeError

5.33

Given the following command, which classes would have assertions enabled?

java -ea -da:com... net.example.LaunchTranslator

Select the two correct answers.

  1. com.example.Translator

  2. java.lang.String

  3. dot.com.Boom

  4. net.example.LaunchTranslator

  5. java.lang.AssertionError