Discussion
The program is run with java Demo39 - what will be the output?
public class Demo39
{
public static void main(String[] args)
{
int[] scores = {90, 80, -1, 70};
for (int s : scores)
{
assert s >= 0 : s;
System.out.print(s + " ");
}
}
}
Answer: C
With assertions disabled the check is skipped on every pass, so the negative score is never objected to and the loop prints all four values. Under -ea the same program would stop at the -1 and throw with that value as its message. A loop is where a cheap assertion earns its keep, because it runs on every element.
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