Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

assert prob.

 
Vishy Karl
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • 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,
 
Gian Franco
blacksmith
Ranch Hand
Posts: 979
  • Mark post as helpful
  • send pies
  • 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
 
Pierre Henry
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic