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

Assertions - Pointing out the errors Questions and Answers

31.

Which line rests on a mistaken assumption?

public class Sample31
{
    public static void main(String[] args)
    {
        int big = Integer.MAX_VALUE;
        assert big + 1 > big : "overflow";
        System.out.println(big + 1);
    }
}

Answer: C

Adding one to Integer.MAX_VALUE wraps silently to Integer.MIN_VALUE, so the sum is negative and the assertion that it exceeds the original is false. The assumption that addition always increases an int is the mistake; Java does not raise an error on overflow.

Please log in or register to report this question.

32.

Which line rests on a mistaken assumption?

public class Sample32
{
    public static void main(String[] args)
    {
        assert Math.abs(Integer.MIN_VALUE) >= 0 : "negative magnitude";
        System.out.println(Math.abs(Integer.MIN_VALUE));
    }
}

Answer: D

Integer.MIN_VALUE has no positive counterpart in int, so Math.abs returns it unchanged and the result is still negative. This is documented behaviour, not a defect, and it means the intuitive claim that an absolute value is never negative does not hold for int.

Please log in or register to report this question.

33.

Which line contains the error?

public class Sample33
{
    static void render(String s)
    {
        assert s != null;
        System.out.println(s.toUpperCase());
    }

    public static void main(String[] args)
    {
        render(args.length > 0 ? args[0] : null);
    }
}

Answer: A

The assertion inside the package-private render() is legitimate. The error is the caller deliberately passing null when no argument is supplied, which makes a broken precondition part of normal operation. Run without -ea this is a NullPointerException on the next line.

Please log in or register to report this question.

34.

Which line contains the error?

public class Sample34
{
    public static void main(String[] args)
    {
        char c = 'a';
        assert c + 1 == 'b' : "not next";
        System.out.println((char)(c + 1));
    }
}

Answer: B

This one is fine. Arithmetic promotes the char to int, and the literal 'b' promotes too, so 98 == 98 holds and the assertion passes. The cast on the print is what turns the int back into a char. Nothing here depends on assertions being enabled.

Please log in or register to report this question.

35.

Which line rests on a mistaken assumption?

public class Sample35
{
    public static void main(String[] args)
    {
        int total = 7;
        int count = 2;
        assert total / count == 3.5 : "wrong average";
        System.out.println(total / count);
    }
}

Answer: C

Both operands are ints, so the division is integer division and yields 3, which is then widened to 3.0 for the comparison and does not equal 3.5. The assertion fails on arithmetic that is behaving exactly as specified. Casting one operand to double is what the author intended.

Please log in or register to report this question.

36.

Which line rests on a mistaken assumption?

public class Sample36
{
    static double ratio(int num, int den)
    {
        return (double) num / den;
    }

    public static void main(String[] args)
    {
        double r = ratio(0, 0);
        assert r == r : "not itself";
        System.out.printf("%s%n", r);
    }
}

Answer: D

Dividing zero by zero in floating point gives NaN, and NaN compares unequal to everything including itself, so this assertion fails on an expression that looks tautological. Testing for NaN needs Double.isNaN(d). Note the division does not throw, unlike integer division by zero.

Please log in or register to report this question.

37.

Which line rests on a mistaken assumption?

public class Sample37
{
    enum Status { NEW, OPEN, CLOSED }

    public static void main(String[] args)
    {
        assert Status.CLOSED.ordinal() == 2 : "reordered";
        System.out.println(Status.CLOSED);
    }
}

Answer: A

An ordinal is just the declaration position, so this assertion pins the code to the current order of the constants and breaks the moment one is inserted before CLOSED. The enum declaration itself is fine. Depend on the constant, never on its ordinal.

Please log in or register to report this question.

38.

Which line contains the error?

public class Sample38
{
    public static void main(String[] args)
    {
        Object[] items = new String[2];
        assert items instanceof Object[];
        items[0] = Integer.valueOf(1);
        System.out.println(items[0]);
    }
}

Answer: B

The assertion is true and harmless: a String array is an Object array. The real fault is the store, which throws ArrayStoreException at runtime because the array is really a String[]. Array covariance is checked when you write, not when you declare, and no assertion here would have caught it.

Please log in or register to report this question.

39.

Which line rests on a mistaken assumption?

public class Sample39
{
    static class Base { String who() { return "base"; } }
    static class Sub extends Base { String who() { return "sub"; } }

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

Answer: C

Instance methods dispatch on the runtime type, so b.who() returns sub even though the variable is declared Base. The assertion assumes the declared type decides, which is only true for static methods and fields. It therefore fails on entirely correct polymorphism.

Please log in or register to report this question.

40.

Which line contains the error?

public class Sample40
{
    public static void main(String[] args)
    {
        Boolean flag = null;
        assert flag : "flag not set";
        System.out.println("ready");
    }
}

Answer: D

A Boolean is accepted where a boolean is needed because it unboxes automatically, so this compiles. At runtime unboxing null throws NullPointerException before the assertion can decide anything, so the helpful message never appears. Compare against null explicitly instead.

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