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

Discussion

Which line rests on a mistaken assumption?

public class Sample30
{
    public static void main(String[] args)
    {
        long start = System.currentTimeMillis();
        work();
        assert System.currentTimeMillis() - start < 100 : "too slow";
        System.out.println("done");
    }

    static void work() { }
}

  • A.No line here is in error.
  • B.assert System.currentTimeMillis() - start < 100 : "too slow";
  • C.long start = System.currentTimeMillis();
  • D.System.out.println("done");

Answer: B

Elapsed time depends on the machine, its load and garbage collection, none of which the code controls, so this assertion can fail on correct behaviour. Assertions state facts that must be true by construction; performance belongs in a benchmark, not in an assert.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.