

MCQOPTIONS
Saved Bookmarks
This section includes 7 Mcqs, each offering curated multiple-choice questions to sharpen your Java Programming knowledge and support exam preparation. Choose a topic below to get started.
1. |
Which statement is true about assertions in the Java programming language? |
A. | Assertion expressions should not contain side effects. |
B. | Assertion expression values can be any primitive type. |
C. | Assertions should be used for enforcing preconditions on public methods. |
D. | An AssertionError thrown as a result of a failed assertion should always be handled by the enclosing method. |
Answer» B. Assertion expression values can be any primitive type. | |
2. |
Which three statements are true? Assertion checking is typically enabled when a program is deployed. It is never appropriate to write code to handle failure of an assert statement. Assertion checking is typically enabled during program development and testing. Assertion checking can be selectively enabled or disabled on a per-package basis, but not on a per-class basis. Assertion checking can be selectively enabled or disabled on both a per-package basis and a per-class basis. |
A. | 1, 2 and 4 |
B. | 2, 3 and 5 |
C. | 3, 4 and 5 |
D. | 1, 2 and 5 |
Answer» C. 3, 4 and 5 | |
3. |
public class Test { public void foo() { assert false; /* Line 5 */ assert false; /* Line 6 */ } public void bar() { while(true) { assert false; /* Line 12 */ } assert false; /* Line 14 */ } } What causes compilation to fail? |
A. | Line 5 |
B. | Line 6 |
C. | Line 12 |
D. | Line 14 |
Answer» E. | |
4. |
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 11 */ assert z > 2: foo(z); /* Line 12 */ if ( z < 7 ) assert z > 4; /* Line 14 */ 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 22 */ System.out.println(z); } } which line is an example of an inappropriate use of assertions? |
A. | Line 11 |
B. | Line 12 |
C. | Line 14 |
D. | Line 22 |
Answer» E. | |
5. |
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"); } } |
A. | finished |
B. | Compilation fails. |
C. | An AssertionError is thrown. |
D. | An AssertionError is thrown and finished is output. |
Answer» D. An AssertionError is thrown and finished is output. | |
6. |
What will be the output of the program? public class Test { public static int y; public static void foo(int x) { System.out.print("foo "); y = x; } public static int bar(int z) { System.out.print("bar "); return y = z; } public static void main(String [] args ) { int t = 0; assert t > 0 : bar(7); assert t > 1 : foo(8); /* Line 18 */ System.out.println("done "); } } |
A. | bar |
B. | bar done |
C. | foo done |
D. | Compilation fails |
Answer» E. | |
7. |
What will be the output of the program? public class Test { public static void main(String[] args) { int x = 0; assert (x > 0) ? "assertion failed" : "assertion passed" ; System.out.println("finished"); } } |
A. | finished |
B. | Compiliation fails. |
C. | An AssertionError is thrown and finished is output. |
D. | An AssertionError is thrown with the message "assertion failed." |
Answer» C. An AssertionError is thrown and finished is output. | |