• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

assertion

 
rohit kumar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q. Consider the following code fragment:
public class AssertionExample2 {
public static void main(String[] args) {
System.out.println(args.length);
assert args.length != 0;
}
}
Which of the following must be done in order for the code to throw an AssertionError?
(Choose all that apply.)
A. The code must be compiled with the -source 1.4 option if you are using JDK 5.0.
B. The program must be executed with the -ea option.
C. At least one argument must be given in the execution command.
D. No argument should be given in the execution command.

i tried compiling without source option its giving me results like 0 which is expected but compiling with source option also happens to provide me with the same answer which makes things confusing.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


1- Assertion is disabled at runtime, use -ea with java to enable assertion.
2- There should be at least one argument passed to the main
method so that assertion could fail and you get AssertionError as desired.

By combining these two, you can achieve the desired case.

Thanks,
 
Paul Napauleon
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

2- There should be at least one argument passed to the main
method so that assertion could fail and you get AssertionError as desired.

By combining these two, you can achieve the desired case.


I think that is not correct. when one or more arguments are passed, the condition evaluates true, so there's no error. When NO arguments are passed, the error will occur.

Regards, Paul
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy Ranchers!

considering
A. The code must be compiled with the -source 1.4 option if you are using JDK 5.0.


Should be can be compiled... since assertions work since 1.4.
But can is not must, hence wrong.


Yours,
Bu.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic