Discussion
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.
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