It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes assert prob. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "assert prob." Watch "assert prob." New topic

assert prob.

Vishy Karl
Ranch Hand

Joined: Sep 08, 2003
Posts: 116
Following Q is in
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,

"The man who can drive himself further once the effort gets painful is the man who will win." <br />Roger Bannister
Gian Franco
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
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.
Gian Franco

"Eppur si muove!"
Pierre Henry
Ranch Hand

Joined: Oct 01, 2003
Posts: 31
Assertions are disabled means that the code with assert is discarded
by JVM, so it means:

and noting will be printed.

"A good method shouldn't bypass one screen long"<br />"genius is the knowledge left when one has forgotten everything"
I agree. Here's the link:
subject: assert prob.
It's not a secret anymore!