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

Assertions - Pointing out the errors Questions and Answers

1.

Which line will cause a compilation error?

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

Answer: A

Expression1 of an assert must be of type boolean, and Java performs no implicit conversion from int. The declaration and the print are both fine; only the assertion is rejected, with "incompatible types: int cannot be converted to boolean". Languages where a non-zero integer counts as true make this look plausible.

Please log in or register to report this question.

2.

Which line will cause a compilation error?

public class Sample02
{
    static void audit() { }

    public static void main(String[] args)
    {
        int total = 0;
        assert total >= 0 : audit();
        System.out.println(total);
    }
}

Answer: B

The value of Expression2 is handed to an AssertionError constructor, so it must produce a value. A call to a void method produces none and the compiler rejects it. Declaring audit() as void is legal on its own; using its result as a detail message is not. Any non-void expression would be accepted.

Please log in or register to report this question.

3.

Which line does the compiler reject?

public class Sample03
{
    public static void main(String[] args)
    {
        int assert = 10;
        System.out.println(assert);
    }
}

Answer: C

assert became a reserved keyword when the assert statement was added in Java 1.4, so it cannot name a variable, method or class. The error is reported at the declaration; the later use is a consequence, not a separate fault. Code written before 1.4 that used assert as an identifier has to be renamed.

Please log in or register to report this question.

4.

Which line will cause a compilation error?

public class Sample04
{
    static String label(String s)
    {
        return s.trim();
    }

    public static void main(String[] args)
    {
        String name = label("  ada  ");
        int len = name.length();
        assert name;
        System.out.println(name + " " + len);
    }
}

Answer: D

A String is not a boolean and does not convert to one, so the assertion does not compile. Truthiness of a non-empty string exists in other languages but not in Java. The intended test is almost certainly assert name != null;, which does yield a boolean.

Please log in or register to report this question.

5.

Which line will cause a compilation error?

public class Sample05
{
    public static void main(String[] args)
    {
        int[] evens = {2, 4, 6, 8};
        for (int a : evens)
        {
            assert a % 2 == 0
            System.out.print(a + " ");
        }
    }
}

Answer: A

An assert is a statement and needs its terminating semicolon. Without one the compiler runs the next line into the same statement and reports the error at the assertion. The condition itself is a perfectly good boolean expression; only the punctuation is wrong.

Please log in or register to report this question.

6.

Which line will cause a compilation error?

public class Sample06
{
    private int limit;
    private boolean capped;

    void configure()
    {
        assert capped = true;
        assert limit = 10;
    }

    public static void main(String[] args)
    {
        new Sample06().configure();
    }
}

Answer: B

An assignment is an expression whose value is the value assigned, so this one has type int and fails the boolean requirement. The same shape with a boolean variable, assert flag = true;, does compile, which is what makes this worth knowing: the single = is not itself the error.

Please log in or register to report this question.

7.

Which line will cause a compilation error?

public class Sample07
{
    static int sqrtFloor(int v)
    {
        assert v > 0 : "v was " : v;
        return (int) Math.sqrt(v);
    }

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

Answer: C

The assert statement takes at most two expressions, separated by a single colon. A second colon is a syntax error. To include the value in the message, build one expression instead: assert n > 0 : "n was " + n;.

Please log in or register to report this question.

8.

Which line will cause a compilation error?

public class Sample08
{
    static boolean ready = true;

    static void check()
    {
        assert ready;
    }

    public static void main(String[] args)
    {
        assert check();
    }
}

Answer: D

check() is declared void, so it yields no value and cannot serve as the boolean condition of an assert. The assertion inside check() is fine. Either give check() a boolean return type or call it as an ordinary statement and assert on something else.

Please log in or register to report this question.

9.

Which line is an inappropriate use of an assertion?

public class Sample09
{
    public void setRate(int rate)
    {
        assert rate > 0 : "rate must be positive";
        this.rate = rate;
    }

    private int rate;
}

Answer: A

setRate is public, so the restriction on rate is part of its published contract and must hold whether or not assertions are enabled. Run without -ea this check disappears and an invalid rate is stored silently. A public method enforces its preconditions with an explicit test that throws IllegalArgumentException; assertions belong in non-public methods.

Please log in or register to report this question.

10.

Which line is an inappropriate use of an assertion?

public class Sample10
{
    private int half(int n)
    {
        assert n % 2 == 0 : n;
        return n / 2;
    }

    public int halveEven(int n)
    {
        assert n % 2 == 0 : n;
        return half(n);
    }
}

Answer: B

The check inside the private half() is exactly the endorsed use: confirming an invariant the class controls. The identical check inside the public halveEven() is not, because callers outside the class rely on that restriction and it vanishes when assertions are disabled. Only the public one needs an explicit throw.

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