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

Assertions - Pointing out the errors Questions and Answers

11.

Which line is an inappropriate use of an assertion?

import java.util.*;

public class Sample11
{
    public static void main(String[] args)
    {
        List<String> names = new ArrayList<String>();
        names.add("ada");
        assert names.remove("ada");
        System.out.println(names.size());
    }
}

Answer: C

The removal is the assertion's condition, so it only happens when assertions are enabled. Run normally the element is never removed and the size is 1 rather than 0. This exact pattern is the one the Oracle guide calls out; the fix is to remove first, keep the boolean result, and assert on that.

Please log in or register to report this question.

12.

Which line is an inappropriate use of an assertion?

public class Sample12
{
    static int served = 0;

    static boolean serve()
    {
        served++;
        return true;
    }

    public static void main(String[] args)
    {
        assert serve();
        System.out.println(served);
    }
}

Answer: D

serve() changes state that outlives the assertion, so the program prints 1 with -ea and 0 without it. An expression inside an assert must be free of side effects precisely so that enabling or disabling assertions cannot change what the program does.

Please log in or register to report this question.

13.

Which line is an inappropriate use of an assertion?

public class Sample13
{
    public static void main(String[] args)
    {
        assert args.length == 2 : "usage: Sample13 <in> <out>";
        String in = args[0];
        String out = args[1];
        System.out.println(in + " -> " + out);
    }
}

Answer: A

Command-line arguments come from outside the program, so validating them is part of its contract with the user, not an internal invariant. Without -ea the check vanishes and the next line throws ArrayIndexOutOfBoundsException instead of reporting the usage. Input validation needs an explicit test.

Please log in or register to report this question.

14.

Which line stops working when the program is run without -ea?

public class Sample14
{
    public static void main(String[] args)
    {
        StringBuilder sb = new StringBuilder("ba");
        assert sb.reverse().length() == 2;
        System.out.println(sb);
    }
}

Answer: B

reverse() mutates the builder in place rather than returning a copy, so the assertion quietly changes the object it is inspecting. With -ea the program prints ab and without it ba. A condition that must be safe to skip cannot call a mutating method.

Please log in or register to report this question.

15.

Which line stops working when the program is run without -ea?

public class Sample15
{
    static int cursor = 0;

    public static void main(String[] args)
    {
        int[] data = {10, 20, 30};
        assert data[cursor++] == 10;
        System.out.println(cursor);
    }
}

Answer: C

The increment is inside the assertion, so cursor advances only when assertions are enabled: the program prints 1 with -ea and 0 without. Any counter, iterator or index the program later depends on must be moved outside the assert.

Please log in or register to report this question.

16.

Which line rests on a mistaken assumption?

public class Sample16
{
    public static void main(String[] args)
    {
        try
        {
            assert false : "broken";
        }
        catch (Exception e)
        {
            System.out.println("recovered");
        }
    }
}

Answer: D

AssertionError extends Error, not Exception, so this catch clause never matches and the error leaves main unhandled. The class was placed under Error deliberately, to discourage recovering from a failed assertion. Catching Throwable or AssertionError by name would match, though neither is advisable.

Please log in or register to report this question.

17.

Which line rests on a mistaken assumption?

public class Sample17
{
    public static void main(String[] args)
    {
        String a = "java";
        String b = new String("java");
        assert a == b : "not equal";
        System.out.println("same");
    }
}

Answer: A

== compares references, and new String is required to produce a distinct object, so the assertion fails even though the text matches. The intent was clearly value equality, which is a.equals(b). The line that creates the second object is legal; only the comparison is wrong.

Please log in or register to report this question.

18.

Which line rests on a mistaken assumption?

public class Sample18
{
    public static void main(String[] args)
    {
        double total = 0.1 + 0.2;
        assert total == 0.3 : total;
        System.out.println(total);
    }
}

Answer: B

Neither operand is exactly representable in binary floating point and their sum lands slightly above 0.3, so the equality is false and the assertion fires on correct arithmetic. Floating-point checks have to allow a tolerance, for example Math.abs(total - 0.3) < 1e-9.

Please log in or register to report this question.

19.

Which line rests on a mistaken assumption?

public class Sample19
{
    public static void main(String[] args)
    {
        Integer x = 200;
        Integer y = 200;
        assert x == y : "boxes differ";
        System.out.println("cached");
    }
}

Answer: C

Only values from -128 to 127 are guaranteed to come from the boxing cache, so two Integers holding 200 may be distinct objects and the reference comparison is unreliable. The assertion can pass or fail depending on the value alone. Wrapper values are compared with equals, or by unboxing to int.

Please log in or register to report this question.

20.

Which line rests on a mistaken assumption?

public class Sample20
{
    public static void main(String[] args)
    {
        int[] a = {1, 2, 3};
        int[] b = {1, 2, 3};
        assert a == b : "contents differ";
        System.out.println("equal");
    }
}

Answer: D

Arrays do not override equality, so == asks whether the two variables reference the same array, which they never do here. The detail message says contents differ, showing the author meant a content comparison: that is java.util.Arrays.equals(a, b).

Please log in or register to report this question.

Loading…
Tags: Assertions - Pointing out the errors Questions and Answers || Assertions - Pointing out the errors MCQ Questions and Answers || Assertions - Pointing out the errors GK Questions and Answers || Assertions - Pointing out the errors GK MCQ Questions || Assertions - Pointing out the errors Multiple Choice Questions and Answers || Assertions - Pointing out the errors GK || GK on Assertions - Pointing out the errors || Java Programming Questions and Answers || Java Programming MCQ Questions and Answers || Java Programming GK Questions and Answers || GK on Java Programming