Machine-translated by Google. Questions are authored in English.
Top

Assertions - Finding the output Questions and Answers

1.

The program is run with java Sample01 - what will be the output?

public class Sample01
{
    public static void main(String[] args)
    {
        int x = 5;
        assert x > 10;
        System.out.println("done");
    }
}

Answer: A

Assertions are disabled by default, and a disabled assertion is semantically equivalent to an empty statement. The expression x > 10 is never evaluated, so no AssertionError can be thrown and the program falls through to the print. Only java -ea Sample01 would make this fail.

Please log in or register to report this question.

2.

The program is run with java Probe02 - what will be the output?

public class Probe02
{
    public static void main(String[] args)
    {
        int[] a = new int[3];
        assert a.length == 5;
        System.out.println(a.length);
    }
}

Answer: B

Without -ea the assertion is skipped entirely, so the clearly false claim that the array has five elements costs nothing. The print then reports the real length, 3. This is exactly why an assertion must never be the only thing checking a condition you actually rely on.

Please log in or register to report this question.

3.

The program is run with java Listing05 - what will be the output?

public class Listing05
{
    public static void main(String[] args)
    {
        boolean flag = false;
        assert flag : "flag was false";
        System.out.println("end");
    }
}

Answer: A

The detail message only matters if the assertion actually fails, and a disabled assertion cannot fail. Neither flag nor the message expression is evaluated, so the program simply prints end. The message form is assert Expression1 : Expression2; and both halves are skipped together.

Please log in or register to report this question.

4.

The program is run with java Fragment06 - what will be the output?

public class Fragment06
{
    public static void main(String[] args)
    {
        int x = 1;
        assert x == 1;
        assert x == 2;
        System.out.println("both passed");
    }
}

Answer: B

Both assertions are inert without -ea, including the false one, so control reaches the print. Note that the first assertion being true is irrelevant: it was not evaluated either. Under -ea the program would throw an AssertionError at the second statement and print nothing.

Please log in or register to report this question.

5.

The program is run with java -ea Example07 - what will be the output?

public class Example07
{
    public static void main(String[] args)
    {
        int used = 250;
        int budget = 100;
        assert used <= budget;
        System.out.println("within budget");
    }
}

Answer: C

With -ea the expression is evaluated, and 250 is not within a budget of 100, so the assertion is false and an AssertionError is thrown at that statement. The error propagates out of main, so the following print never runs. Nothing catches it, so the JVM reports the error and exits with a non-zero status.

Please log in or register to report this question.

6.

The program is run with java -ea Program08 - what will be the output?

public class Program08
{
    public static void main(String[] args)
    {
        System.out.println("before");
        assert false;
        System.out.println("after");
    }
}

Answer: D

Statements before the assertion have already executed and their output has already been written, so before appears. The assertion then fails and the error propagates out of main, so after is never reached. Output already produced is not undone by a later error.

Please log in or register to report this question.

7.

The program is run with java -ea Sketch11 - what will be the output?

public class Sketch11
{
    public static void main(String[] args)
    {
        StringBuilder sb = new StringBuilder("ab");
        assert sb.length() == 3 : sb;
        System.out.println(sb);
    }
}

Answer: C

The assertion fails because the length is 2, not 3. Expression2 is sb, so the AssertionError constructor receives the StringBuilder and calls its toString, giving the message ab. Note the message is the object's text, not the length that was tested.

Please log in or register to report this question.

8.

The program is run with java -ea Item12 - what will be the output?

public class Item12
{
    public static void main(String[] args)
    {
        int d = 0;
        assert d != 0 : "divisor is zero";
        System.out.println(10 / d);
    }
}

Answer: D

The assertion is checked before the division, fails, and throws immediately, so the division never executes and no ArithmeticException occurs. This is the assertion doing its job: turning a downstream failure into a clear statement of the assumption that was broken. Without -ea it would be an ArithmeticException instead.

Please log in or register to report this question.

9.

The program is run with java -ea Sample13 - what will be the output?

public class Sample13
{
    public static void main(String[] args)
    {
        try
        {
            assert false;
        }
        catch (Exception e)
        {
            System.out.println("caught");
        }
        System.out.println("end");
    }
}

Answer: A

AssertionError extends Error, not Exception, so a catch (Exception e) block does not match it. The error passes straight through the try statement and out of main, and end is never printed. The hierarchy was chosen deliberately to discourage recovering from a failed assertion.

Please log in or register to report this question.

10.

The program is run with java -ea Probe14 - what will be the output?

public class Probe14
{
    public static void main(String[] args)
    {
        try
        {
            assert false;
        }
        catch (Error e)
        {
            System.out.println("caught " + e.getClass().getSimpleName());
        }
    }
}

Answer: B

AssertionError is a subclass of Error, so this catch block does match. getSimpleName() reports the runtime class of the object thrown, which is AssertionError rather than the declared catch type, so the output is caught AssertionError. Catching Error like this is legal but a bad habit.

Please log in or register to report this question.

Loading…
Tags: Assertions - Finding the output Questions and Answers || Assertions - Finding the output MCQ Questions and Answers || Assertions - Finding the output GK Questions and Answers || Assertions - Finding the output GK MCQ Questions || Assertions - Finding the output Multiple Choice Questions and Answers || Assertions - Finding the output GK || GK on Assertions - Finding the output || Java Programming Questions and Answers || Java Programming MCQ Questions and Answers || Java Programming GK Questions and Answers || GK on Java Programming