| Author |
K&B Page 814 Q7 Regarding Assertions in Java
|
Peter Swiss
Greenhorn
Joined: Mar 03, 2011
Posts: 23
|
|
Here is the question
Which sets of commands will compile and run without exception or error?
A. javac Antique.java
java Antique
B. javac Antique.java
java -ea Antique
C. javac -source 6 Antique.java
java Antique
D. javac -source 1.4 Antique.java
java Antique
E. javac -source 1.6 Antique.java
java -ea Antique
Answer says A and C are correct.
A is obvious.
What is the difference between C and D? Since Assertions were added to Java 1.4, options C and D are the same. So even D should be correct.
Please reply.
Thank you.
|
 |
Vijay keshava
Ranch Hand
Joined: Jul 05, 2011
Posts: 40
|
|
D would have been correct, if instead of List<String> myList = new ArrayList<String>(); we had List myList = new ArrayList(); Generics were introduced only in java 1.5. The question is tricky, since you are focusing on assertion, but the catch is with usage of generics
|
 |
Peter Swiss
Greenhorn
Joined: Mar 03, 2011
Posts: 23
|
|
oh, yes!! LOL that was good!
Thanks Vijay!
|
 |
Glen Iris
Ranch Hand
Joined: Jul 13, 2011
Posts: 157
|
|
Why is B wrong? - I guess assertions are enabled by default but what is wrong with explicitly enabling them too?
Why is E wrong?
|
OCPJP 6, OCMJD (2/3)
|
 |
Helen Ma
Ranch Hand
Joined: Nov 01, 2011
Posts: 451
|
|
Suppose, we are using Java 6 to compile.
B and E will compile, but it will throw the error as there is no argument from the command-Line.
assert (args.length>0) will be false and print an AssertionError message.
|
 |
Glen Iris
Ranch Hand
Joined: Jul 13, 2011
Posts: 157
|
|
thank you helen
|
 |
dennis deems
Ranch Hand
Joined: Mar 12, 2011
Posts: 808
|
|
Glen Iris wrote:Why is B wrong? - I guess assertions are enabled by default but what is wrong with explicitly enabling them too?
Assertions are disabled by default. That's why A works. The line that would cause an AssertionError is ignored unless assertions are explicitly enabled.
|
 |
Glen Iris
Ranch Hand
Joined: Jul 13, 2011
Posts: 157
|
|
Thanks Dennis. I had figured that out in the mean time but its always good to have a solid Java person back up my assumptions.
|
 |
 |
|
|
subject: K&B Page 814 Q7 Regarding Assertions in Java
|
|
|