• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

assert prob.

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following Q is in javacertificate.com
Given the fragment of code is compiled to permit the use of the assert statement but disabled at runtime. What is the output if showFact is called with the following parameter value: "Scott"?


5. public static void showFact(String explorer) {
6. if (explorer.equals("DaGama")) {
7. System.out.println("DaGama discovered a route to the East");
8. } else if (explorer.equals("Cook")) {
9. System.out.println("Cook reached Batavia, Java in October 1770");
10. } else if (explorer.equals("Armstrong")) {
11. System.out.println("Armstrong was the first person on the moon");
12. } else if (explorer.equals("Drake")) {
13. System.out.println("Drake was involved in the slave trade");
14. } else {
15. assert false : explorer;
16. }
17. }

Options given are
1 An assertion error is thrown with the value Scott.
2 Cook reached Batavia, Java in October 1770 is displayed in the console.
3 Nothing is displayed in the console.
4 Scott is displayed in the console.
5 A null pointer exception is thrown

Answer given is 3. Can u tell me why is it so ?? why not 1 ?
Thanks in Advance,
 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vishy,
As you stated: "...the assert statement is disabled at runtime"
(i.e. java -da ...)
So the assert won't be evaluated and thus, since the String
"Scott" does not pass any of the equals() expressions nothing
is printed.
Greetings,
Gian Franco
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Assertions are disabled means that the code with assert is discarded
by JVM, so it means:

and noting will be printed.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic