Welcome Guest | Sign in | Register

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? 
A. Line 9 B. Line 10
C. Line 12 D. Line 20

Answer and Explanation

Answer: Line 20

Explanation:
Assert statements should not cause side effects. Line 20 changes the value of z if the assert statement is false.
Option A is fine; a second expression in an assert statement is not required.
Option B is fine because it is perfectly acceptable to call a method with the second expression of an assert statement.
Option C is fine because it is proper to call an assert statement conditionally.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
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");
}
}
A. finished B. Compilation fails.
C. An AssertionError is thrown. D. An AssertionError is thrown and finished is output.

Answer and Explanation

Answer: An AssertionError is thrown.

Explanation:
An assertion Error is thrown as normal giving the output "assertion failed". The word "finished" is not printed (ensure you run with the -ea option)
Assertion failures are generally labelled in the stack trace with the file and line number from which they were thrown, and also in this case with the error's detail message "assertion failed". The detail message is supplied by the assert statement in line 6.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. Which of the following statements is true?
A. Assertions can be enabled or disabled on a class-by-class basis. B. Conditional compilation is used to allow tested classes to run at full speed.
C. Assertions are appropriate for checking the validity of arguments in a method. D. The programmer can choose to execute a return statement or to throw an exception if an assertion fails.

Answer and Explanation

Answer: Assertions can be enabled or disabled on a class-by-class basis.

Explanation:
Option A is correct. The assertion status can be set for a named top-level class and any nested classes contained therein. This setting takes precedence over the class loader's default assertion status, and over any applicable per-package default. If the named class is not a top-level class, the change of status will have no effect on the actual assertion status of any class.
Option B is wrong. Is there such a thing as conditional compilation in Java?
Option C is wrong. For private methods - yes. But do not use assertions to check the parameters of a public method. An assert is inappropriate in public methods because the method guarantees that it will always enforce the argument checks. A public method must check its arguments whether or not assertions are enabled. Further, the assert construct does not throw an exception of the specified type. It can throw only an AssertionError.
Option D is wrong. Because you're never supposed to handle an assertion failure. That means don't catch it with a catch clause and attempt to recover.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04. Which of the following statements is true?
A. It is sometimes good practice to throw an AssertionError explicitly. B. Private getter() and setter() methods should not use assertions to verify arguments.
C. If an AssertionError is thrown in a try-catch block, the finally block will be bypassed. D. It is proper to handle assertion statement failures using a catch (AssertionException ae) block.

Answer and Explanation

Answer: It is sometimes good practice to throw an AssertionError explicitly.

Explanation:
Option A is correct because it is sometimes advisable to thrown an assertion error even if assertions have been disabled.
Option B is incorrect because it is considered appropriate to check argument values in private methods using assertions.
Option C is incorrect; finally is never bypassed.
Option D is incorrect because AssertionErrors should never be handled.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
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. 
A. A B. B
C. C D. D
E. E

Answer and Explanation

Answer: E

Explanation:
The condition in assert statement must be followed by a semi-colon.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
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. 
A. A B. B
C. C D. D
E. E

Answer and Explanation

Answer: B

Explanation:
When i and j are both 1, assert condition is false, and AssertionError gets generated.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
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. 
A. A B. B
C. C D. D
E. E

Answer and Explanation

Answer: A

Explanation:
When the if condition returns true, the assert statement also returns true. Hence AssertionError does not get generated.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
08. Which of the following statement is true about the assert statement. Select the one correct answer. 
A. If a Java class contains assert statements, then it must be compiled with -1.4 option.  B. When a program having assertions is run, -assertion option must be specified, otherwise the assertions get ignored. 
C. A possible syntax of assert statement is assert logical_expression If logical_expression evaluates to true, the program generates an AssertionError.  D. The program terminates on its first AssertionError.

Answer and Explanation

Answer: The program terminates on its first AssertionError.

Explanation:
The option A is incorrect, as the Java compiler option is -source 1.4 . The option B is incorrect, as the runtime option is -ea or -enableassertions. If the logical expression evaluates to false, then the program generates an AssertionError, hence C is incorrect.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
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;
}
}
A. The class compiles and runs, but does not print anything. B. The number 2 gets printed with AssertionError
C. The number 3 gets printed with AssertionError D. compile error

Answer and Explanation

Answer: The number 2 gets printed with AssertionError

Explanation:
When i and j are both 2, assert condition is false, and AssertionError gets generated. 

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
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;
}
}
A. The class compiles and runs, but does not print anything. B. The number 2 gets printed with AssertionError
C. The number 3 gets printed with AssertionError D. compile error

Answer and Explanation

Answer: The class compiles and runs, but does not print anything.

Explanation:
When if condition returns true, the assert statement also returns true.
Hence AssertionError not generated.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.