Home > Java Programming > Assertions > Questions and Answers
01. |
public class Test2{ public static int x; public static int foo(int y) { return y * 2; } public static void main(String [] args) { int z = 5; assert z > 0; /* Line 9 */ assert z > 2: foo(z); /* Line 10 */ if ( z < 7 ) assert z > 4; /* Line 12 */ switch (z) { case 4: System.out.println("4"); case 5: System.out.println("5"); default: assert z < 10; } if ( z < 10 ) assert z > 4: z++; /* Line 20 */ System.out.println(z); } } Which line is an example of inappropriate use of assertions? | |||||||||||
|
02. |
What will be the output of the program (when you run with the -ea option) ? public class Test{ public static void main(String[] args){ int x = 0; assert (x > 0) : "assertion failed"; /* Line 6 */ System.out.println("finished"); } } | |||||||||||
|
03. | Which of the following statements is true? | |||||||||||
|
04. | Which of the following statements is true? | |||||||||||
|
05. |
What happens when the following code is compiled and run. Select the one correct answer. for(int i = 1; i < 3; i++) for(int j = 3; j > i; j--) assert i!=j {System.out.println(i); } A. The class compiles and runs, but does not print anything. B. The number 1 gets printed with AssertionError C. The number 2 gets printed with AssertionError D. The number 3 gets printed with AssertionError E. The program generates a compilation error. | |||||||||||||||
|
06. |
What happens when the following code is compiled and run. Select the one correct answer. for(int i = 1; i < 3; i++) for(int j = 3; j >= 1; j--) assert i!=j : i; A. The class compiles and runs, but does not print anything. B. The number 1 gets printed with AssertionError C. The number 2 gets printed with AssertionError D. The number 3 gets printed with AssertionError E. The program generates a compilation error. | |||||||||||||||
|
07. |
What happens when the following code is compiled and run. Select the one correct answer. for(int i = 1; i < 4; i++) for(int j = 1; j < 4; j++) if(i < j) assert i!=j : i; A. The class compiles and runs, but does not print anything.
B. The number 1 gets printed with AssertionError C. The number 2 gets printed with AssertionError D. The number 3 gets printed with AssertionError E. The program generates a compilation error. | |||||||||||||||
|
08. | Which of the following statement is true about the assert statement. Select the one correct answer. | |||||||||||
|
09. |
What is the output for the below code ? public class Test { public static void main(String... args) { for(int i = 2; i < 4; i++) for(int j = 2; j < 4; j++) assert i!=j : i; } } | |||||||||||
|
10. |
What is the output for the below code ? public class Test { public static void main(String... args) { for(int i = 2; i < 4; i++) for(int j = 2; j < 4; j++) if(i < j) assert i!=j : i; } } | |||||||||||
|