This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assertion

 
B pandoo
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would you also please state from which mock exam you are getting these questions. Thanks.
 
Rancy Chadha
Ranch Hand
Posts: 135
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Deepthi Kanakam Rajan
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic