• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

assertion

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic