• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Doubt in Master Exam question

 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of the question in the master exam says this

If a switch block has no default, adding an assert default is considered appropriate.

But how can this be true. I know that some of you will say that this example is just to prove the question wrong, but I actually had this code in my program when I was developing a game. It was a quiz based game which used to come on TV in USA as "Who wants to be a Billionaire" and in India as "Kaun Banega Crorepati", on the basis of the current question number, I had to print dots on the money tree on the question which were already answered. So I had this code



If I add assert as default here, my code will always generate an AssertionError. Then I would have to add the dafault as the first case or add break after the last case. But why would I do that??? My code is running fine in this condition. So adding assert is increasing complexity and I would consider it inappropriate....
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, the statement meant that when we are sure that under any case our program would not enter the default case, then placing assert there is a good practice.

If a switch block has no default case, adding an assert is considered appropriate.



means
default:
assert false;

I am not sure but thats how i took it when going to Assertion chapter. Correct me if i am wrong.
[ September 28, 2008: Message edited by: vidhya suvarna ]
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Default case is used with assertion in the following scenario.

In a test suppose max number of questions are 10. Suppose in some exceptional scenario if input questionNumber > 10 then to know why none of question was selected we can use assertions (using simple assertions we get question number passed to switch statement also).

--
Venkata.
[ September 28, 2008: Message edited by: Venkata ]
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you should have added the break statement for every case to run fine.adding assertion in default case is good practice because then only we can assure that no wrong values for question numbers.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assertions are used during testing.These statements are not executed during normal execution of program, as assert are disabled by default.Using assert in switch statement means that as per assumptions your are not suppose to reach the default.assertions are used to check your assumptions or the logic you have applied to your code.They are executed when they are enabled.
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic