a. With assertions enabled it prints ABC followed by an AssertionError message. b. With assertions disabled it prints ABC followed by an AssertionError message. c. Assertions should not be used within the default case of a switch statement. d. In this code example an assert statement could not be used in place of the "throw" statement.

Answer is a,b,d

I am unable to understand statement b. If Assertions are disabled then how it is able to throw AssertionError? Please clear my doubt

Well!!! It is really simple, if you would have run this program, you would have got the reason.

When c.m1(3) will be called, then "default" case will run, and that is "throw new AssertionError()". It is just an error, to throw this error you donot require to enable assertion. There is not statement like "assert boolean:expression" that will depend on enabling or disabling of assertion.

if you replace throw new AssertionError(); with assert new AssertionError(); then you would be violating the rule of Assertions which says that the first expression should always result in a boolean value.

Though you could have said something like this:

thanks
Harvinder

Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952

posted Dec 22, 2008 07:34:00

0

d. In this code example an assert statement could not be used in place of the "throw" statement.

see here no case statement has break; means what? default case will be executed every time you call switch() with any value. That is not the use of assertion. [ December 22, 2008: Message edited by: punit singh ]

As no case has break statement, so everytime assert statement will get executed. Assert could be used with default, but in situation where it is not possible or error to control reach default case.

AND

see here no case statement has break; means what? default case will be executed every time you call switch() with any value.

I dont think that is true. If the return statement is executed once, it will return from the method.It does not matter if break is present or not.

if a return statement is present in try or catch block ,and if finally block exists ,THAT is the only place were return is over looked . i.e finally will exectue and then only it returns.

Ex:

only after finally is executed , it will return.

SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !

Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952

posted Dec 22, 2008 08:25:00

0

Oh I overlooked the return statement!

Now I am also not clear why d is true here ?

Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231

posted Dec 22, 2008 08:35:00

0

Originally posted by Punit: see here no case statement has break; means what? default case will be executed every time you call switch() with any value. That is not the use of assertion.

I guess James has clarified your point.

Moreover, that example was only to show how assert could have been put there legally. Also the real exam options would want to test on a legal use of JLS statements and not the appropriate use of JLS statements. So, incase i did not communicate properly, i just wanted to show a correct legal use of assert statement *syntactically*. Nothing more.

But i do stick by my previous explanation as to the use of a boolean expression in an assert statement being mandatory.

if you replace throw new AssertionError(); with assert new AssertionError(); then you would be violating the rule of Assertions which says that the first expression should always result in a boolean value.

if you replace throw new AssertionError(); with assert new AssertionError(); then you would be violating the rule of Assertions which says that the first expression should always result in a boolean value.

But in this case no String object is returned, instead an Assertion error is thrown???

geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208

posted Dec 23, 2008 00:50:00

0

Somewhere i have read if the mothod is having non-void return type then it must return something or else it must throw something. Even i am not sure about the above statement. Please anyone clear this.

Thanks Geeta Vemula

Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952

posted Dec 23, 2008 00:56:00

0

Ya this is true, either return something or throw any exception/error.

Originally posted by Geeta Vemula:The above reason is not the correct explanation for the option d to be correct.

The reason is because the method is returning int value, it must return something instead of returning void. this is what i thought. Tell me whether I am correct or not.

Well you answer is correct in the context of the method returning a String value. I could have used the same logic to say that it is "assert boolean" syntax which is the only reason.

But i guess the reason of assert accepting a boolean is correct alongwith the reason put forth by you. That is why i put a return statement in the default block of my previous post, though i explicitly did not mention it as i thought the primary reason was "assert boolean" syntax. Your perspective is totally valid but for me the primary reason is "assert boolean"

I think you are all missing the point of why d. is correct. The actual reason is this:

throw new AssertionError(); is equivalent to: assert false; only when assertions are enabled when you launch the program. If assertions are disabled, the second statement is not even seen by the JVM. However, the first statement remains regardless of whether assertions are enabled or not.

There was some discussion as to whether a method that returns a String must always return a string. But what would be the point of returning a String when an exception or error that is not caught is thrown? When an exception or error is thrown the method doesn't even return in the proper sense of the term. It just throws an exception or an error.

All code in my posts, unless a source is explicitly mentioned, is my own.

Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231

posted Dec 23, 2008 23:34:00

0

Originally posted by Ruben:

throw new AssertionError(); is equivalent to: assert false; only when assertions are enabled when you launch the program. If assertions are disabled, the second statement is not even seen by the JVM. However, the first statement remains regardless of whether assertions are enabled or not.

Can you please elaborate what you are trying to say? Also let me add, the compiler does not know whether assertions will be enabled or disabled at runtime. Hence, as per the question if we say assert new AssertionError(); you won't even go past the compiler. And why would the compiler not let you go past because it expects a boolean as the first expression of an assert. Simple.

But, if you do want to introduce an assert then the syntactically correct example( not necessarily appropriate) is:

Originally posted by Ruben:

But what would be the point of returning a String when an exception or error that is not caught is thrown?

A String return would be required to compile the program. The assert statement introduced in the default clause won't even compile without the return. Also the use of assert statement, was only to show a legal use and not an appropriate use. Please refer to my previous post.

geeta answered this question correctly. If I replace throw with assert (not as Harvinder is saying), then the code will not compile

Now this code will not compile. Suppose you pass 10 to the method and assertions are disabled on runtime. Then the default case will get executed. Since assertions are disabled, so assert will not get executed. Now the control will get out of switch. Now what?? The method is over and there is no string value to return. What will the poor JVM do now. So the compiler saved JVM from this confusion by generating a compile time error in the default case. The correct code would be

Whatever it is i.e. false or true, the code will not compile. If you read the question, it says this

in place of the "throw" statement.

It is not talking about replacing the throw word...

Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952

posted Dec 24, 2008 00:17:00

0

James it is talking about the replacing with the whole assert statement. Then it's up to you, you can put any boolean.

assert true; assert false; assert any_boolean_expression;

But my question is what is the relationship of this assert argument to this question? Assert statement could be any legal statement.

Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032

posted Dec 24, 2008 00:30:00

0

I agree, "in place of" in this case doesn't mean to substitute "throws" by "assert." If someone asked you to put a for loop in place of a while loop you wouldn't just replace "while" for "for", right?

I am very surprised that this:

won't compile. The compiler is smart enough to distinguish between assertion code that might not exist depending on the execution command, and code that will always throw an exception.

Thanks for explaining this, guys.

Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231

posted Dec 24, 2008 02:57:00

0

A statement is indeed different from a keyword. The correct perspective thrown by Ankit on how to interpret the throws *statement* has changed the answer (for me atleast).

Yes, folks thanks for pitching in and correcting this oversight on my part. I agree with Geeta and Ankit and all those who say that the reason why option d is correct is the return statement missing in default statement. [ December 24, 2008: Message edited by: Harvinder Thakur ]

Originally posted by Harvinder Thakur: I agree with Geeta and Ankit and all those who say that the reason why option d is correct is the return statement missing in default statement.

Good Boy

Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231

posted Dec 24, 2008 04:31:00

0

Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952

posted Dec 24, 2008 04:39:00

0

Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032

posted Dec 24, 2008 09:18:00

0

And even if you add the return statement, you can't actually substitute the throws statement by the assert statement (plus the return statement.)

If what you understand by "substituting for" means that it will still compile, then the answer is yes. But I don't think you should understand the question that way. In that case, you could substitute the throws statement by a "return ""; " statement, although they would clearly be not equivalent.

What I think they are asking is if you could make the program logically equivalent by replacing the throws with an assert. In this case the question is moot because the assert in its own won't compile, but I think the intent of the question is to see if you can realize that assert statements are only in effect when assertions are enabled, unlike throws statements.

That's what I think they are asking, anyway. As if Java wasn't complicated enough to master, now I have to decipher the semantics of the English language.