aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assertion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertion" Watch "Assertion" New topic
Author

Assertion

B pandoo
Ranch Hand

Joined: Nov 14, 2006
Posts: 37
line 1 public class Asserttest3
2 {

3 public static Boolean message()
4 {
5 System.out.println("Error");
6 return new Boolean("true");
7}
8 public static void main(String[] args)
9{
10 boolean b =false;
11 assert b:message();
12 int x = -5;
13 assert(x+1)>0;
14 assert x>0 x= x * -1);
15 assert !b;
16 while(true)
17 {
18 System.out.println(x);
19 assert x-- > 0;
20 }
21 }
22 }


Find out the line which used the assertion inappropriately.

Select multiple options:
1) line 11
2)line 13
3)line 15
4)line 19
5)line 14



i donot find any wrong in the above assertion used except none included any try/catch for assertion error.what could be the output.?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Hello "pandoo pandoo"-

Welcome to JavaRanch.

On your way in you may have missed that we have a JavaRanch Naming Policy for displayed (screen) names. Your displayed name must consist of a first name (or an initial), a space, and a family name (in that order) and not be obviously fictitious. Since yours "pandoo pandoo", does not conform with it, please take a moment to change it, which you can do right here.

Posters with nonconforming displayed names will be locked out of JavaRanch after a few posts using those names.

Thanks
-Barry

(NR) - search tag


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Would you also please state from which mock exam you are getting these questions. Thanks.
Rancy Chadha
Ranch Hand

Joined: Jul 12, 2006
Posts: 135
Hi Pandoo,

line 1 public class Asserttest3
2 {

3 public static Boolean message()
4 {
5 System.out.println("Error");
6 return new Boolean("true");
7}
8 public static void main(String[] args)
9{
10 boolean b =false;
11 assert b:message();
12 int x = -5;
13 assert(x+1)>0;
14 assert x>0: (x= x * -1);
15 assert !b;
16 while(true)
17 {
18 System.out.println(x);
19 assert x-- > 0;
20 }
21 }
22 }

I think for the above mentioned code the correct answer would be option 5 which will be at line 14. The code will compile and run fine but the question is not asking us assertion used illegaly it is asking us assertion used inappropriately. On line number 14 value of variable x is getting changed. Which will in turn affect the code after this using variable x. But imagine if we run this program without assertion enabled we will get a different answer from what we will get with assetion disabled. There will be INCONSISTENCY in the output with the way program runs using assertions enabled or disabled.
Also there is no point using TRY-CATCH block for assertion errors because assertion are generally used for verifying our assumption during the development phase. After application is made they are generally disabled while deploying and use of application.

Assume you have a code snippet like
int i=1;
try
{
assert i==0; //since 1!=0 this will cause an assertion error
}catch(AssertionError ae)
{ //no functionality provided for handling assertion
}


if we run program containing this code with assertion enabled your program will run fine and you will not even come to know if YOUR ASSUMPTION passed or failed. Because catch block has nothing in it to handle it,so the control goes directly to statement after catch block. Thus you will not avail the useful functionality given by assertions.
I hope this is clear to you.

Thanks,
Rancy


Thanks,<br />-Rancy
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

I think the whole question is not appropriate.
ALL uses of assertions are inappropriate in this code, because they are used in public method.

So the code can be used from outside. This is not appropriate.
You may use assertions in public methods only if you are absolutely sure that they will never been thrown. I cannot see, that the assertions in the public method belong to this type.
But Rancy found out that line 14 is double inappropriate, so this might be the correct answer for the makers of that question.

noname baboo wrote:
i donot find any wrong in the above assertion used except none included any try/catch for assertion error.


Catching an assertion error?
That's tricking out what assertions were made for.



Yours,
Bu.


all events occur in real time
Deepthi Kanakam Rajan
Greenhorn

Joined: May 15, 2006
Posts: 13
Originally posted by pandoo pandoo:
line 1 public class Asserttest3
2 {

3 public static Boolean message()
4 {
5 System.out.println("Error");
6 return new Boolean("true");
7}
8 public static void main(String[] args)
9{
10 boolean b =false;
11 assert b:message();
12 int x = -5;
13 assert(x+1)>0;
14 assert x>0 x= x * -1);
15 assert !b;
16 while(true)
17 {
18 System.out.println(x);
19 assert x-- > 0;
20 }
21 }
22 }


Find out the line which used the assertion inappropriately.

Select multiple options:
1) line 11
2)line 13
3)line 15
4)line 19
5)line 14



i donot find any wrong in the above assertion used except none included any try/catch for assertion error.what could be the output.?


Hi Pandoo

I think the answers should be lines 19 and 14 ie 4 and 5.Assertions should not cause any side-effects when used because it can disbaled at runtime.

In both lines 14 and 19 the value of variable x is changed.
 
 
subject: Assertion