| Author |
assertions : Assessment test -2 : Q12
|
Divya Madugula
Greenhorn
Joined: Aug 18, 2012
Posts: 5
|
|
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
Joined: Apr 04, 2012
Posts: 869
|
|
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 ne 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.
|
OCPJP 6(100 %) OCEWCD 6(91 %)
|
 |
Divya Madugula
Greenhorn
Joined: Aug 18, 2012
Posts: 5
|
|
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  ne 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
Joined: Apr 04, 2012
Posts: 869
|
|
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
Joined: Jan 28, 2008
Posts: 5575
|
|
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
Joined: Apr 04, 2012
Posts: 869
|
|
|
oh yeah you are right. thanks for correcting me.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
gurpeet singh wrote:thanks for correcting me.
Of course, you are welcome
|
 |
Divya Madugula
Greenhorn
Joined: Aug 18, 2012
Posts: 5
|
|
Thanks a lot !! Its clear now !!!
|
 |
 |
|
|
subject: assertions : Assessment test -2 : Q12
|
|
|