Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

assertions : Assessment test -2 : Q12

 
Divya Madugula
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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 !
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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 -eane 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
  • 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 -eane 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
Fedora Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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


 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh yeah you are right. thanks for correcting me.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks a lot !! Its clear now !!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic