Assertions
Question : 1
Given :
public class test{
public static void main(String[] a){
assert a.length==1;
}
}
Which two will produce an AssertionError ? (choose two)
- java test
- java -ea test
- java test file1
- java -ea test file1
- java -ea test file1 file2
- java -ea:test test file1
Answer : B, E
Question : 2
Given a method that must ensure that its parameter is not null :
11 public void someMethod(Object value){
12 //check for null value
//.........
20 System.out.println(value.getClass());
21 }
What, inserted at line 12, is the appropriate way to handle a null value ?
- assert value==null;
- assert value!=null, "value is null";
-
if(value==null){
throw new AssertionError("value is null");
}
-
if(value==null){
throw new IllegalArgumentException("value is null");
}
Answer : D
Question : 3
Given :
23. int z = 5;
24.
25. public void stuff1(int x) {
26. assert (x > 0);
27. switch(x) {
28. case 2: x = 3;
29. default: assert false; } }
30.
31. private void stuff2(int y) { assert (y < 0); }
32.
33. private void stuff3() { assert (stuff4()); }
34.
35. private boolean stuff4() { z = 6; return false; }
Which statement is true ?
- All of the assert statements are used appropriately.
- Only the assert statement on line 31 is used appropriately.
- The assert statements on lines 29 and 31 are used appropriately.
- The assert statements on lines 26 and 29 are used appropriately.
- The assert statements on lines 29 and 33 are used appropriately.
- The assert statements on lines 29, 31, and 33 are used appropriately.
- The assert statements on lines 26, 29, and 31 are used appropriately.
Answer : C
Question : 4
Click the Exhibit button.
1. public class Test {
2. 3. public static void main(String [] args) {
4. boolean assert = true;
5. if(assert) {
6. System.out.println("assert is true");
7. }
8. }
9.
10. }
Given: javac -source 1.3 Test.java What is the result?
- Compilation fails.
- Compilation succeeds with errors.
- Compilation succeeds with warnings.
- Compilation succeeds without warnings or errors.
Answer : C
Question : 5
Given :
1. public class Mule {
2. public static void main(String[] args) {
3. boolean assert = true;
4. if(assert) {
5. System.out.println("assert is true");
6. }
7. }
8. }
Which command-line invocations will compile ?
- javac Mule.java
- javac -source 1.3 Mule.java
- javac -source 1.4 Mule.java
- javac -source 1.5 Mule.java
Answer : B
Question : 6
Given :
1. public class Donkey2 {
2. public static void main(String[] args) {
3. boolean assertsOn = true;
4. assert (assertsOn) : assertsOn = true;
5. if(assertsOn) {
6. System.out.println("assert is on");
7. }
8. }
9. }
If class Donkey is invoked twice, the first time without assertions enabled,
and the second time with assertions enabled, what are the results?
- no output
- no output assert is on
- assert is on
- no output An AssertionError is thrown.
- assert is on An AssertionError is thrown.
Answer : C
Question : 7
Given :
1. public class Donkey {
2. public static void main(String[] args) {
3. boolean assertsOn = false;
4. assert (assertsOn) : assertsOn = true;
5. if(assertsOn) {
6. System.out.println("assert is on");
7. }
8. }
9. }
If class Donkey is invoked twice, the first time without assertions enabled,
and the second time with assertions enabled, what are the results ?
- no output
- no output assert is on
- assert is on
- no output An AssertionError is thrown.
- assert is on An AssertionError is thrown.
Answer : D
Question : 8
Given :
11. public void go(int x) {
12. assert (x > 0);
13. switch(x) {
14. case 2: ;
15. default: assert false;
16. }
17. }
18. private void go2(int x) { assert (x < 0); }
Which statement is true ?
- All of the assert statements are used appropriately.
- Only the assert statement on line 12 is used appropriately.
- Only the assert statement on line 15 is used appropriately.
- Only the assert statement on line 18 is used appropriately.
- Only the assert statements on lines 12 and 15 are used appropriately.
- Only the assert statements on lines 12 and 18 are used appropriately.
- Only the assert statements on lines 15 and 18 are used appropriately.
Answer : G
Question : 9
Given two files :
1. class One {
2. public static void main(String[] args) {
3. int assert = 0;
4. }
5. }
1. class Two {
2. public static void main(String[] args) {
3. assert(false);
4. }
5. }
And the four command-line invocations :
javac -source 1.3 One.java
javac -source 1.4 One.java
javac -source 1.3 Two.java
javac -source 1.4 Two.java
What is the result? (Choose all that apply.)
- Only one compilation will succeed.
- Exactly two compilations will succeed.
- Exactly three compilations will succeed.
- All four compilations will succeed.
- No compiler warnings will be produced.
- At least one compiler warning will be produced.
Answer : B and F are correct. Class One will compile (and issue a warning) using the 1.3 flag, and class Two will compile using the 1.4 flag.
Question : 10
Which are true? (Choose all that apply.)
- It is appropriate to use assertions to validate arguments to methods marked public.
- It is appropriate to catch and handle assertion errors.
- It is NOT appropriate to use assertions to validate command-line arguments.
- It is appropriate to use assertions to generate alerts when you reach code that should not be reachable.
- It is NOT appropriate for assertions to change a program's state.
Answer :
- C , D, and E are correct statements.
- A is incorrect. It is acceptable to use assertions to test the arguments of private methods.
B is incorrect.
While assertion errors can be caught, Sun discourages you from doing so.
|