Witscale Test Center

Answers with explanations


Answers with explanations

 

9.       þ C

C is correct option, The value of r will be printed on the console. The first if condition (r < q) is false. Therefore, control goes to the else and then to the first if statement (if(p > r) ) in it. The condition  (p > r) is checked. Since its true, the value of r is printed on console.

 

10.    þ B

The while loop will be executed 2 times for i=4 and i=5. The case for(b + 1) will match the first time. The Second time, for i=5, none of the cases will match. Therefore, the default statement will be executed and “none” is printed. Since the default does not have a break statement, execution will continue and “1 ” will be printed even though at that point i != b + 1. Then, the execution control comes out of the switch and the loop.

 

11.    þ E

At the very first iteration (i=0,j=0 and k=0), the condition k==4 is checked. Since the condition is false, nothing happens. Then the next immediate statement is break; Note that the if statement here does not have a curly bracket so the break is not part of if. Therefore, it is executed no matter what the condition is in if. So, the for loop (with k as loop index) will always break whenever it is entered for k=0. Thus the “Message” is never printed.

 

12.    þ A C D

A is valid; the third part of for can have any valid Java statement. It is not necessary to have an increment or decrement step there.

The while loop in B is invalid; you cannot declare variables in while’s round brackets.

 

13.    þ E

The error UnknownError is not caught anywhere. Therefore only the finally clause will be executed and after that the JVM halts. Therfore only E is correct.

A is incorrect because the exception occurs before “Success” could be printed. B, C and D are incorrect as the no catch block is matched when UnknownError is thrown. E is correct as finally is executed even when exception occurs in try block. F is incorrect as control is immediately transferred to the calling method of the method in which this code is present.

 

14.    þ D E F

D E and F are correct. The InterruptedIOException is not caught anywhere. However, its superclass IOException has a catch block. Therefore it is executed followed by the finally block. F is correct; Since the exception is properly caught, the execution continues after try-catch-finally and “Continue ...” is printed.

A is incorrect because the exception occurs before “Success” could be printed. B, C are incorrect as those catch blocks do not match for the InterruptedIOException.

 

15.    þ C D E

C and D represent true statements about assertions. C is correct as expression after the : can be any Java expression with value so that it can be printed in the stacktrace of AssertionError at runtime. D represents appropriate use of assertions to assert the arguments to the switch statement. E is true; as sometimes it is better to throw AssertionError to detect some erroneous conditions when assertions may be disabled.

B is incorrect as expression before the : must be a boolean expression. A is not correct as you should never handle AssertionError. It is desired that your application should terminate whenever assertion fails. B is not completely true as the statement before : in assert statement must be a boolean expression and not just any expression

 

16.    þ B C

B and C are the correct statements. B represents the correct use of assertions for asserting the flow control. C is correct. The public methods expose the public interface of a class. The user of these methods may call them with invalid arguments.  You need to enforce the constraints on the argument within the method itself. Since assertions are not guaranteed to be executed all the time , the arguments should not be validated with assertions.

A is incorrect; sometimes it is advisable to throw an assertion error even when assertions have been disabled. However, it is not always appropriate. D is incorrect; such method will not be called if assertions are disabled. E is incorrect as finally is executed even when an error is thrown in try-catch block.

 

17.    þ D

This code will fail to compile at line 18. The second expression in the assert is a call to the method doSomething() and this method does not return any value. Therefore, compiler flags an error. The compiler will not complain at line 19 because the method getProblemValue() returns some value.