jobs4timesLogo jobs4timesLogo

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)
  1. java test
  2. java -ea test
  3. java test file1
  4. java -ea test file1
  5. java -ea test file1 file2
  6. 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 ?
  1. assert value==null;
  2. assert value!=null, "value is null";
  3. if(value==null){
     throw new AssertionError("value is null");
    }
    
  4. 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 ?
  1. All of the assert statements are used appropriately.
  2. Only the assert statement on line 31 is used appropriately.
  3. The assert statements on lines 29 and 31 are used appropriately.
  4. The assert statements on lines 26 and 29 are used appropriately.
  5. The assert statements on lines 29 and 33 are used appropriately.
  6. The assert statements on lines 29, 31, and 33 are used appropriately.
  7. 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?
  1. Compilation fails.
  2. Compilation succeeds with errors.
  3. Compilation succeeds with warnings.
  4. 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 ?
  1. javac Mule.java
  2. javac -source 1.3 Mule.java
  3. javac -source 1.4 Mule.java
  4. 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?
  1. no output
  2. no output assert is on
  3. assert is on
  4. no output An AssertionError is thrown.
  5. 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 ?
  1. no output
  2. no output assert is on
  3. assert is on
  4. no output An AssertionError is thrown.
  5. 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 ?
  1. All of the assert statements are used appropriately.
  2. Only the assert statement on line 12 is used appropriately.
  3. Only the assert statement on line 15 is used appropriately.
  4. Only the assert statement on line 18 is used appropriately.
  5. Only the assert statements on lines 12 and 15 are used appropriately.
  6. Only the assert statements on lines 12 and 18 are used appropriately.
  7. 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.)
  1. Only one compilation will succeed.
  2. Exactly two compilations will succeed.
  3. Exactly three compilations will succeed.
  4. All four compilations will succeed.
  5. No compiler warnings will be produced.
  6. 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.)
  1. It is appropriate to use assertions to validate arguments to methods marked public.
  2. It is appropriate to catch and handle assertion errors.
  3. It is NOT appropriate to use assertions to validate command-line arguments.
  4. It is appropriate to use assertions to generate alerts when you reach code that should not be reachable.
  5. 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.
BACK