This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes assertion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "assertion" Watch "assertion" New topic
Author

assertion

rohit kumar
Greenhorn

Joined: May 27, 2007
Posts: 9
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

Joined: Feb 28, 2007
Posts: 1707


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,


cmbhatt
Paul Napauleon
Greenhorn

Joined: Jul 20, 2007
Posts: 15

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

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
 
 
subject: assertion
 
Similar Threads
Enthuware question
Advice me on JQ+ and some else...
Command Line
args.length
just a problem....H E L P ! ! ! !