1. | public class While Which statement is true? |
|||||||
Answer: Option D Explanation: Using the integer 1 in the while statement, or any other looping or conditional construct for that matter, will result in a compiler error. This is old C Program syntax, not valid Java. A, B and C are incorrect because line 1 is valid (Java is case sensitive so While is a valid class name). Line 8 is also valid because an equation may be placed in a String operation as shown. |
2. | public void test(int x) Which statement is true? |
|||||||
Answer: Option A Explanation: The compiler will complain because of incompatible types (line 4), the if expects a boolean but it gets an integer. |
3. | switch(x) Which two are acceptable types for x? 1 byte |
|||||||
Answer: Option A Explanation: Switch statements are based on integer expressions and since both bytes and chars can implicitly be widened to an integer, these can also be used. Also shorts can be used. Short and Long are wrapper classes and reference types can not be used as variables. |
4. | public void foo( boolean a, boolean b) |
|||||||
Answer: Option C Explanation: Option C is correct. The output is "ELSE". Only when a is false do the output lines after 11 get some chance of executing. Option A is wrong. The output is "A". When a is true, irrespective of the value of b, only the line 5 output will be executed. The condition at line 7 will never be evaluated (when a is true it will always be trapped by the line 12 condition) therefore the output will never be "A && B". Option B is wrong. The output is "A". When a is true, irrespective of the value of b, only the line 5 output will be executed. Option D is wrong. The output is "notB". |