| 1. | What will be the output of the program? public class Test |
|||||||
Answer: Option B Explanation: Java only ever passes arguments to a method by value (i.e. a copy of the variable) and never by reference. Therefore the value of the variable i remains unchanged in the main method. If you are clever you will spot that 16 is 4 multiplied by 2 twice, (4 * 2 * 2) = 16. If you had 16 left shifted by three bits then 16 * 2 * 2 * 2 = 128. If you had 128 right shifted by 2 bits then 128 / 2 / 2 = 32. Keeping these points in mind, you don't have to go converting to binary to do the left and right bit shifts. |
| 2. | What will be the output of the program? class BoolArray void set(boolean [] x, int i) public static void main(String [] args) void test() |
|||||||
Answer: Option C Explanation: The reference variables b and x both refer to the same boolean array. count is incremented for each call to the set() method, and once again when the first if test is true. Because of the && short circuit operator, count is not incremented during the second if test. |
| 3. | What will be the output of the program? class Two class PassO void start() Two fix(Two tt) |
|||||||
Answer: Option C Explanation: In the fix() method, the reference variable tt refers to the same object (class Two) as the t reference variable. Updating tt.x in the fix() method updates t.x (they are one in the same object). Remember also that the instance variable x in the Two class is initialized to 0. |
| 4. | What will be the output of the program? class Test void start() void twice(int x) |
|||||||
Answer: Option B Explanation: The int x in the twice() method is not the same int x as in the start() method. Start()'s x is not affected by the twice() method. The instance variable s is updated by twice()'s x, which is 14. |
| 5. | What will be the output of the program? class SC2 void start() String foo() |
|||||||
Answer: Option D Explanation: Because all of these expressions use the + operator, there is no precedence to worry about and all of the expressions will be evaluated from left to right. If either operand being evaluated is a String, the + operator will concatenate the two operands; if both operands are numeric, the + operator will add the two operands. |
| 6. | What will be the output of the program? class SSBool |
|||||||||
Answer: Option B Explanation: The & operator has a higher precedence than the | operator so that on line 8 b1 and b2 are evaluated together as are b2 & b3. The final b1 in line 10 is what causes that if test to be true. Hence it prints "dokey". |
| 7. | What will be the output of the program? class Bitwise |
|||||||
Answer: Option D Explanation: The & operator produces a 1 bit when both bits are 1. The result of the & operation is 9. The ^ operator produces a 1 bit when exactly one bit is 1; the result of this operation is 10. The | operator produces a 1 bit when at least one bit is 1; the result of this operation is 14. |
| 8. | What will be the output of the program? class Test |
|||||||
Answer: Option B Explanation: The first two iterations of the for loop both x and y are incremented. On the third iteration x is incremented, and for the first time becomes greater than 2. The short circuit or operator || keeps y from ever being incremented again and x is incremented twice on each of the last three iterations. |
