• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

assertion questions

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q: 03 Given:
23. int z = 5;
24.
25. public void stuff1(int x) {
26. assert (x > 0);
27. switch(x) {
28. case 2: x = 3;
29. default: assert false; } }
30.
31. private void stuff2(int y) { assert (y < 0); }
32.
33. private void stuff3() { assert (stuff4()); }
34.
35. private boolean stuff4() { z = 6; return false; }
Which statement is true?
A. All of the assert statements are used appropriately.
B. Only the assert statement on line 31 is used appropriately.
C. The assert statements on lines 29 and 31 are used appropriately.
D. The assert statements on lines 26 and 29 are used appropriately.
E. The assert statements on lines 29 and 33 are used appropriately.
F. The assert statements on lines 29, 31, and 33 are used appropriately.
G. The assert statements on lines 26, 29, and 31 are used appropriately.

can someone please tell me the correct with reasons.
thank you in advance..
 
Ranch Hand
Posts: 51
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just let me watch for the answers ... but syntax of assert statement is 1) assert boolean; 2) assert boolean: "message"; so from these two syntax perspective i guess they all are correct.
 
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kaxhif Khan wrote:Just let me watch for the answers ... but syntax of assert statement is 1) assert boolean; 2) assert boolean: "message"; so from these two syntax perspective i guess they all are correct.



The usage of the assertions can be (1) correct, (2) appropriate. Correct usage means that the Java syntax, related to assertions is not violated and the compiler doesn't generate any errors. Appropriate usage of the assertions means that although it may be correct it violates the recommendations on where to use assertions in the code.

Anirudh, I would suggest reading this article from the official Oracle website, "Putting Assertions Into Your Code" section. You may also find this information in the preparation books. And then you may post the answers by yourself so that other participants can check and give some other advice.
 
Ever since I found this suit I've felt strange new needs. And a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic