• 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

assertions : Assessment test -2 : Q12

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class One {
int x = 0;
{ assert x == 1; }
}
public class Two {
public static void main(String[] args) {
int y = 0;
assert y == 0;
if(args.length > 0)
new One();
}
}

Which of the following will run without error? (Choose all that apply.)
A. java Two
B. java Two x
C. java -ea Two
D. java -ea Two x
E. java -ea:One Two
F. java -ea:One Two x
G. java -ea:Two Two x


Can someone please explain this !!!
A and B will work for sure as assertions are not enabled.
The rest of the options are a bit confusing because its involving 2 classes here !! please help !
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Except D and F all are right

when you do java -ea Two, this means that you are enabling assertions for all classes. in your case it will enable assertion on Two as well as one

when you do java -ea:One Two, here you are enabling assertion on only class named One and disabled for class Two. so -ea:one means enable assertions on one, while running Two.

also when we do java -ea:com. Two , this means enable assertions on the package com and all its subpackages.

also suppose class Two is in a package com.guru and you do java -da:com -ea:Two Two, then assertions will be enabled for class Two, because the jvm sees the most specific setting for a class.
 
Divya Madugula
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gurpeet singh wrote:Except D and F all are right

when you do java -ea Two, this means that you are enabling assertions for all classes. in your case it will enable assertion on Two as well as one

when you do java -ea:One Two, here you are enabling assertion on only class named One and disabled for class Two. so -ea:one means enable assertions on one, while running Two.

also when we do java -ea:com. Two , this means enable assertions on the package com and all its subpackages.

also suppose class Two is in a package com.guru and you do java -da:com -ea:Two Two, then assertions will be enabled for class Two, because the jvm sees the most specific setting for a class.




Firstly , Thank you for giving your time to explain. I still have a problem understanding the problems code ., why are options C,E,F,G incorrect ? I am confused !!!

In class One,
int x = 0;
{ assert x == 1; } must always return an error whenever assertions are enabled .., isn't it ?
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Did you read my previous post ? C, E, F, G are not incorrect as you say . i clearly wrote that EXCEPT D and F all are right. so that means options D, F are will produce AssertionError.

In class One,
int x = 0;
{ assert x == 1; } must always return an error whenever assertions are enabled .., isn't it ?



No it won't, not always. the assertion expression in enclosed in instance initializer(also called init blocks). these init block always run when an instance of the class is created. hence , only when instance of One is created does the assertion expression runs. and now a question for you, when will a instance of One be created ? ? and there you are now crystal clear

if you still have any problem , feel free to post


 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One small changes in your good reply , may be nitpick though ;)

gurpeet singh wrote:
also when we do java -ea:com. Two , this means enable assertions on the package com and all its subpackages.


the syntax should be java -ea:com... Two . the three dot operator is important and
when you pass class name instead of package then you should not use ... i.e, java -ea:com.Clazz Two
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh yeah you are right. thanks for correcting me.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gurpeet singh wrote:thanks for correcting me.


Of course, you are welcome
 
Divya Madugula
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot !! Its clear now !!!
 
When you have exhausted all possibilities, remember this: you haven't - Edison. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic