Discussion
Which line is an inappropriate use of an assertion?
public class Sample55
{
private int[] data = new int[4];
public int at(int i)
{
assert i >= 0 && i < data.length : i;
return data[i];
}
}
Answer: C
at() is public, so the valid index range is part of what callers are promised and must be enforced whether or not -ea is set. Here the check disappears in production and the array access throws ArrayIndexOutOfBoundsException instead. That happens to be a reasonable error, but it is luck rather than design; the bounds belong in an explicit test.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development