• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

assertion

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following,

which line is an example of an inappropriate use of assertions?

A]Line 8

B]Line 9

C]Line 11

D]Line 15

E]Line 18
answer is E. Assert statements should not cause side effects. Line 18 changes the value of z if the assert statement is false.
but i think line 15 is also wrong bcause value of z is 5 & there is no break keyword in the switch stmt,so it will go to default stmt of switch after case 5.& then it will give error.i think author forgot to use break over there.And one more confusion is that z is not the final variable then how can it used in switch stmt of above programme.

pls correct my confusion ,
thanks in advance..
 
bronco
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.

While I agree that the lack of break statement is odd, it doesn't really make the assertion in line 15 incorrect. That assertion doesn't fail in the case you described because z IS less than 10 (which is what's being asserted).

As for the variable not being final: that's not a requirement for use in a switch statement. What you're thinking of is that the values in the CASE statements must be constant values. In this example, those values are 4 and 5, which are constants. If you defined a variable:

int FOO = 5;

...and used it in place of the value 5 in the case statement, it would not compile because FOO is not constant.

Hope that helps.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please consider that assert statements are used to detect program bugs. If the lack of break statements causes an assert to fail, the assert is just doing its job.
 
prajkta patil
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks dave & mike.
now i understood well.
 
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So , What would be the answer of question .
I think the code will run without error but there is no option like none of this .
 
ankur rathi
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didnt get the reason for answer E .
please any body help .
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rathi,

Take care that the question is asking for inappropriate use of assertions, not illegal use.

I think it's mandatory to distinguishe between Illegal and Inappropriate when looking at assertions questions.
 
ankur rathi
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How it is inappropriate ???

I am very much confuse now ?

please help .
 
ankur rathi
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help .

It is confusing me !!
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There should not be any side effects when you are using assertions. Bcoz assertions can be enabled and disabled, z++ in line no 18 gives one output when assertions are enabled and different output when these are disabled. Hence the answer E is correct. The following url also helps when to use assertions and not to use.

http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html

Hope this clarifies your query.

Sreenivas
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic