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

Assertions - Pointing out the errors Questions and Answers

61.

Which line rests on a mistaken assumption?

public class Sample61
{
    public static void main(String[] args) throws Exception
    {
        Thread t = new Thread(() -> { assert false : "worker"; });
        t.start();
        t.join();
        System.out.println("main finished");
    }
}

Answer: A

An AssertionError thrown inside a worker thread kills only that thread. join returns normally, main prints and exits with a success status, and the failure is easy to miss in the log. An assertion is only as useful as the thread that reports it, so worker threads need their own handler.

Please log in or register to report this question.

62.

Which line is an inappropriate use of an assertion?

public class Sample62
{
    interface Store
    {
        default void put(String key, String value)
        {
            assert key != null : "key required";
            write(key, value);
        }

        void write(String k, String v);
    }
}

Answer: B

A default method on an interface is public, so the rule that the key may not be null is part of the published contract and cannot depend on -ea. Implementations and callers outside the interface rely on it. This one needs an explicit throw, exactly as a public class method would.

Please log in or register to report this question.

63.

Which line contains the error?

public class Sample63
{
    static class Item implements Comparable<Item>
    {
        int size;

        public int compareTo(Item other)
        {
            assert compareTo(other) == -other.compareTo(this);
            return Integer.compare(size, other.size);
        }
    }
}

Answer: C

The assertion calls the very method it sits in, so it recurses until the stack overflows before any comparison happens. Checking the symmetry of compareTo is a sensible idea, but it belongs in a test that calls the method from outside, not inside the method itself.

Please log in or register to report this question.

64.

Which line rests on a mistaken assumption?

public class Sample64
{
    public static void main(String[] args)
    {
        assert sum() == 3;
        System.out.println(sum(1, 2));
    }

    static int sum(int... parts)
    {
        assert parts != null;
        int t = 0;
        for (int p : parts) t += p;
        return t;
    }
}

Answer: D

Calling a varargs method with no arguments passes an empty array, so sum() returns 0 and the assertion claiming 3 is false. The null check inside the method is redundant rather than wrong, since the array is never null. The error is the expectation in the caller.

Please log in or register to report this question.

65.

Which line contains the error?

public class Sample65
{
    public static void main(String[] args)
    {
        assert check(2) : "even";
        assert !check(2) : "odd";
        System.out.println("consistent");
    }

    static boolean check(int n) { return n % 2 == 0; }
}

Answer: A

The two assertions contradict each other, so one must always fail whatever check() returns. The first passes for 2 and the second then throws. A pair like this usually survives an edit where a condition was negated in one place and not the other; the code can never satisfy both.

Please log in or register to report this question.

66.

Which line will cause a compilation error?

public class Sample66
{
    public static void main(String[] args)
    {
        assert Character.isDigit('7');
        assert Character.isDigit("7");
        System.out.println("both digits");
    }
}

Answer: B

The two literals look alike but are different types: '7' is a char and "7" is a String. Character.isDigit has no String overload, so the second line does not compile. Single and double quotes are not interchangeable in Java the way they are in some other languages.

Please log in or register to report this question.

67.

Which line contains the error?

public class Sample67
{
    public static void main(String[] args)
    {
        assert Integer.parseInt("+7") == 7;
        assert Integer.parseInt("7 ") == 7;
        System.out.println("parsed");
    }
}

Answer: C

A leading plus sign is accepted, so the first assertion passes. Surrounding whitespace is not: parseInt throws NumberFormatException on "7 " rather than trimming it. The second line therefore fails with an exception, not an assertion failure, and the batch stops there.

Please log in or register to report this question.

68.

Which line contains the error?

public class Sample68
{
    static int count()
    {
        try
        {
            assert false : "unreachable";
            return 1;
        }
        finally
        {
            return 2;
        }
    }

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

Answer: D

A return inside finally discards whatever the try was doing, including a thrown AssertionError, so the method quietly returns 2 and the failed assertion vanishes. Returning from finally swallows errors and should never be done; the assertion is only the victim here.

Please log in or register to report this question.

69.

Which line rests on a mistaken assumption?

public class Sample69
{
    static class Base { static String tag = "base"; }
    static class Sub extends Base { static String tag = "sub"; }

    public static void main(String[] args)
    {
        Base b = new Sub();
        assert b.tag.equals("sub") : b.tag;
        System.out.println(b.tag);
    }
}

Answer: A

Fields are not polymorphic. Accessing tag through a variable declared Base resolves to Base.tag at compile time, so the value is base regardless of the object's runtime type, and the assertion fails. Hiding a field is legal but rarely what the author wanted.

Please log in or register to report this question.

70.

Which line contains the error?

public class Sample70
{
    private final int size;

    Sample70(int size)
    {
        assert size > 0;
        this.size = size;
    }

    Sample70()
    {
        this(0);
    }

    public static void main(String[] args)
    {
        new Sample70();
    }
}

Answer: B

The no-argument constructor chains to the other one with a value its own assertion forbids, so the class cannot be constructed that way under -ea. The assertion is a reasonable statement of the invariant; the convenience constructor contradicts it. Either the default should be valid or the invariant is wrong.

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