Discussion
Which line rests on a mistaken assumption?
public class Sample64
{
public static void main(String[] args)
{
assert sum() == 3;
System.out.println(sum(1, 2));
}
static int sum(int... parts)
{
assert parts != null;
int t = 0;
for (int p : parts) t += p;
return t;
}
}
Answer: D
Calling a varargs method with no arguments passes an empty array, so sum() returns 0 and the assertion claiming 3 is false. The null check inside the method is redundant rather than wrong, since the array is never null. The error is the expectation in the caller.
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