• 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 Question???

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello folk's

For the following question the answer is "C", but I couldn't understand why the answer "E" is wrong. Here is there explanation. pls let me know what they are trying to say.

Explanation: -
" Option E apparently looks quite appropriate, but note that in absence of this check, a HashMap with no entries will not cause any exception or error if its get method is called with any key"

Question: -
Which is the most appropriate code snippet that can be inserted at line 18 in the following code?(Assume that the code is complied and run with assertions enabled)

1. import java.util.*;
2.
3. public class Assert Test
4. {
5. private HashMap cctld;
6.
7.public AssertTest()
8.{
9.cctld = new HashMap();
10.cctld.put("in","Indai");
11.cctld.put("uk","United Kingdom");
12.cctld.put("au","Australia");
13// more code.....
14.}
15.// other methods
16.public String getCountry(String countryCode)
17.{
18.//What should be inserted here?
19.String country =(String)cctld.get(countryCode);
20.return country;
21.}
22. }

Answer:-
A)assert countryCode != null;
B)assert countryCode != null: "Country code can not be null";
C)assert cctld != null : "No country code data is available";
D)assert cctld : "No country code data is available";
E) assert cctld.size() != 0 : "No country code data is available";
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Raghu...

How abt this explantion: if cctld is null then invoking cctld.size() gives a NullPointerException so of all the choices this seems to be the most appropriate one....

what do you think???
 
RAGU KANNAN
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great!!!
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was thinking that somewhere in SCJP they say you should only use assertions for conditions that should _never_ happen. During the life of the container, it should be allowed to be empty without getting an assertion error I would think. Maybe this is wrong...what do you think?
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dave Reinhardt ,
I think you are right. Assertions are used when you assuming (certain) that the condition is true. And this is enabled at development and debuging time and not in deployment.

Regards,
yogesh
 
machines help you to do more, but experience less. Experience this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic