Discussion
The program is run with java -ea Listing41 - what will be the output?
public class Listing41
{
Listing41(int v)
{
assert v > 0 : "v must be positive";
System.out.println("built " + v);
}
public static void main(String[] args)
{
new Listing41(1);
new Listing41(0);
}
}
Answer: A
The first construction passes the assertion and prints built 1. The second fails it, so the AssertionError is thrown inside the constructor before its print and propagates out of main. Note the object is never fully constructed on the failing call.
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