*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes AssertionError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "AssertionError" Watch "AssertionError" New topic
Author

AssertionError

geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
I got it from
http://www.danchisholm.net/oct1/mybook/chapter23/assertions1.html


class C {
String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default: throw new AssertionError();
}}
public static void main(String[] args) {
C c = new C();
for (int i = 0; i < 4; i++) {
System.out.print(c.m1(i));
}}}

Which statements are true?

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

Thanks,
Geeta Vemula
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
Hi geetha..

As per my knowledge....

here you are thowing an exception which is not depends upon enable and disable of assetion at runtime.

if you are using assertion syntx like assert a<b:"message"

then you need to enable assertions at runtime because bydefault it is in disable mode...


SCJP5 and SCWCD1.5
Think Twice Act Wise...
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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.


SCJP 6
garima jain
Ranch Hand

Joined: Sep 22, 2008
Posts: 60
can someone please explain why option d is true.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
can someone please explain why option d is true.


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.
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
Originally posted by Garima
can someone please explain why option d is true.


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
d. In this code example an assert statement could not be used in place of the "throw" statement.


See Harvinder what this statement is saying.

And you said:
default:{
assert (true): new AssertionError();
return "";
}


You cannot do it here Harvinder.
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default:{
assert (true): new AssertionError();
return "";
}


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 ]
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Originally posted 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
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
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.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Originally posted by Harvinder Thakur:


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.


This is the reason for d to be correct
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ok ok I got the point...
geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
quote:
--------------------------------------------------------------------------------
Originally posted by Harvinder Thakur:


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.

--------------------------------------------------------------------------------

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.

Thanks,
Geeta Vemula
geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
Forgot to add ..
So it must either throw or return something.

Thanks,
Geeta Vemula
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Expression must return boolean.

As assert statement is written like:

assert boolean;
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

@ preetha Arun
After reading your post,i had these questions,

The reason is because the method is returning int value

Which method and where int is returned???

it must return something instead of returning void.


Where is void returned ???

So it must either throw or return something.


I think there are some other options,(i guess)
[ December 22, 2008: Message edited by: James Tharakan ]
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
James where is Preetha Arun' post ?
geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
method m1 is returning String. So it must return something.

class C {
String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default: throw new AssertionError();
}}
public static void main(String[] args) {
C c = new C();
for (int i = 0; i < 4; i++) {
System.out.print(c.m1(i));
}}}



Geeta Vemula.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Originally posted by punit singh:
James where is Preetha Arun' post ?



My mistake
I meant geeta vemula




[ December 23, 2008: Message edited by: James Tharakan ]
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Originally posted by geeta vemula:
method m1 is returning String. So it must return something.



The first three cases would return string,
but default ,its throwing a error at runtime.
I am confused with your question ,hope someone else would answer.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default: assert false;//error must return String here or
}

//or here
}


If you want the code look like above then you are right.
As m1() method is returning String then default case must also return String, if you write assert statement.

then we have to write like:

String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default: assert false; return "D";
}

}


String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default: assert false;
}
return "D";
}

[ December 23, 2008: Message edited by: punit singh ]
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
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
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
Ya this is true, either return something or throw any exception/error.

String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default: throw new AssertionError();
}}


This is completely valid.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Thanks. Punit.
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
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"
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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
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.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

can someone please explain why option d is true.


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



or



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

@ Ankit,
Actually
OPtion d.In this code example an assert statement could not be used in place of the "throw" statement.

What does the word "In place of" mean ? ?

does that mean ,
1-> default: assert new AssertionError();

or
2->default: assert false;


If it is the second one (default: assert false ,

how can we assume that there is false. Why true can't be there??
:roll:
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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
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
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
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 ]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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.

[ December 24, 2008: Message edited by: Ruben Soto ]
[ December 24, 2008: Message edited by: Ruben Soto ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: AssertionError
 
Similar Threads
Dan's Q of Assertion?
Assertion
For Dan's attention:
dan exam doubt 11
Assertions