Discussion
The program is run with java -ea Fragment42 - what will be the output?
public class Fragment42
{
private static int half(int n)
{
assert n % 2 == 0 : n;
return n / 2;
}
public static void main(String[] args)
{
System.out.println(half(8));
System.out.println(half(7));
}
}
Answer: B
half(8) satisfies the precondition and prints 4. half(7) violates it, so the assertion throws before the division and 3 is never printed. Checking a precondition of a private method is precisely the use the Oracle guide endorses, in contrast to argument checking in public methods.
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