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

Assertions - Pointing out the errors Questions and Answers

71.

Which line contains the error?

public class Sample71
{
    public static void main(String[] args)
    {
        int grade = 75;
        switch (grade / 10)
        {
            case 7:
                assert grade >= 70 && grade < 80;
            case 6:
                System.out.println("pass");
                break;
            default:
                System.out.println("other");
        }
    }
}

Answer: C

The case 7 branch has no break, so control falls through into case 6 and prints pass by accident rather than by design. The assertion above it is true and passes. Fall-through is legal and occasionally intended, but here it makes the 70s share a branch with the 60s silently.

Please log in or register to report this question.

72.

Which line rests on a mistaken assumption?

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

Answer: D

The int constructor sets the initial CAPACITY, not the length. The builder holds no characters yet, so its length is 0 and the assertion fails with 0 as its message. Capacity is an allocation hint and is reported by a different method entirely.

Please log in or register to report this question.

73.

Which line contains the error?

public class Sample73
{
    static class Point
    {
        int x;

        public boolean equals(Object o)
        {
            return o instanceof Point && ((Point) o).x == x;
        }
    }

    public static void main(String[] args)
    {
        Point a = new Point(), b = new Point();
        assert a.equals(b);
        assert a.hashCode() == b.hashCode();
    }
}

Answer: A

Point overrides equals but not hashCode, so two equal points inherit identity-based hash codes that almost certainly differ. The first assertion passes and the second fails, which is the contract violation showing itself. Overriding equals obliges you to override hashCode.

Please log in or register to report this question.

74.

Which line rests on a mistaken assumption?

public class Sample74
{
    public static void main(String[] args)
    {
        assert "a".compareTo("b") == -1 : "a".compareTo("b");
        System.out.println("a".compareTo("b"));
    }
}

Answer: B

compareTo promises only the SIGN of the result, not a particular magnitude, and String returns the difference of the first differing characters. Here that happens to be -1, so this passes, but comparing "a" with "c" returns -2 and the same assertion would fail. Test the sign, never the value.

Please log in or register to report this question.

75.

Which line rests on a mistaken assumption?

public class Sample75
{
    public static void main(String[] args)
    {
        Object o = new Object();
        assert o.hashCode() == o.hashCode();
        assert new Object().hashCode() == new Object().hashCode();
        System.out.println("stable");
    }
}

Answer: C

Asking the same object twice must give the same hash code, so the first assertion is sound. Two distinct objects have no such guarantee, and the default hash code is derived per instance, so the second is almost always false. Equal hash codes are required only for equal objects.

Please log in or register to report this question.

76.

Which line will cause a compilation error?

public class Sample76
{
    static int clamp(int v)
    {
        return Math.max(0, v);
        assert v >= 0;
    }

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

Answer: D

The assertion sits after an unconditional return, so it can never execute and the compiler rejects it as an unreachable statement. A postcondition has to be checked before the value leaves the method, which means assigning the result to a local, asserting on that, and returning it.

Please log in or register to report this question.

77.

Which line will cause a compilation error?

public class Sample77
{
    private int low, mid, high;

    void sort3(int x, int y, int z)
    {
        low = Math.min(x, Math.min(y, z));
        high = Math.max(x, Math.max(y, z));
        mid = x + y + z - low - high;
        assert low < mid < high;
    }

    public static void main(String[] args)
    {
        new Sample77().sort3(9, 2, 5);
    }
}

Answer: A

Java has no chained comparison. a < b produces a boolean, and comparing a boolean with the int c is a type error, so the line does not compile. The intended test is written with an explicit conjunction: assert a < b && b < c;.

Please log in or register to report this question.

78.

Which line contains the error?

public class Sample78
{
    public static void main(String[] args)
    {
        String cmd = "stop";
        switch (cmd)
        {
            case "stop":
                assert cmd.length() == 4;
                break;
            case "stop ":
                assert false;
                break;
        }
        System.out.println(cmd);
    }
}

Answer: B

Switching on a String is legal, but the two labels differ only by a trailing space, which is almost certainly a typo rather than a real case. It compiles and is simply dead. The assertion in the first branch is true and passes.

Please log in or register to report this question.

79.

Which line contains the error?

public class Sample79
{
    public static void main(String[] args)
    {
        Integer a = null;
        int b = true ? 1 : a;
        assert b == 1;
        System.out.println(b);
    }
}

Answer: C

Mixing an int and an Integer in a conditional forces binary numeric promotion, so BOTH branches are unboxed even though only the first is chosen. Unboxing the null throws NullPointerException before the assertion is reached. Keeping both branches the same type avoids it.

Please log in or register to report this question.

80.

Which line contains the error?

public class Sample80
{
    public static void main(String[] args)
    {
        int[] src = {1, 2, 3};
        int[] dst = new int[2];
        assert src.length == dst.length;
        System.arraycopy(src, 0, dst, 0, src.length);
    }
}

Answer: D

The assertion states something that is plainly false for these arrays, so it fires immediately under -ea and hides the real problem. Without assertions the copy throws ArrayIndexOutOfBoundsException, which is the actual bug: the destination is too small. The check should compare the length being copied against the destination.

Please log in or register to report this question.

Loading…

Other Subjects

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