|
1. þ A B C
A is correct 0x1f is integral literal in hexadecimal system and you can assign it to an int. ^ is binary operator, hence 4 ^ a is valid. B is legal too as ~ is unary operator that works on integrals. C looks strange but it is valid += is compound assignment the -1 acts as the second operand to it. The – here is used as unary operator.
D is incorrect as ! operates only on boolean operand. E is incorrect as the condition (first operand) of ternary operator must be of boolean type. Since a is of int type, the expression will not compile.
2. þ E
E is correct, as signed right shift by 3 bits is same as dividing by 23. Hence 64 >> 3 will result in 8.
A is incorrect; 32 % 4 gives the remainder of 32 divided by 4 which is 0. B is invalid as there is no such operator as <<<. C is incorrect; the unsigned right shift in positive numbers also causes division by power of 2, hence 32 >> 4 will be 32 divided by 24 that is 2. D is incorrect as 64 >>> 8 will results in 0 ( 64 /28 ). F is incorrect as left shifting causes multiplication by 2, hence 2 << 2 will result is 2 * 22 which is 8 and this values is again shifted, 8 << 2 which will be 32.
3.þ C D E F
C D E and F are all valid expressions as they correctly use the operators. C is valid as + operator can be used when any one operand is string. D and E are valid as += operator can be used for string concatenation as well. a += f is equivalent a = a + f; The result of a + f is a string “Hello World2.76” , therefore it can be assigned back to a. Similarly a+=a is equivalent to a = a+a. The result of a+a is a string “Hello WorldHello World”, therefore it can be assigned back to a. F represents f - b which is a subtraction between float value and int value.
A and B are invalid. A is invalid as b += a will be equivalent to b = b+a; The expression b+a will result in string “9Hello World”. But it is a String hence it cannot stored back to b. B is invalid as a *= b will be equivalent to a = a * b; The expression a * b is invalid as multiplication cannot be performed on string operand.
4. þ D
Only the a.equals(b) will be evaluated to true. Both a and b are string references and referring to two different string objects. But their contents are same therefore equals() returns true.
Condition specified in option A will not evaluate to true as int c and d have different numeric value. Veriable c contains decimal value 1010 whereas variable d contains hexadecimal value 0x1010. Option B represents a valid comparison a==b, but the variables a and b point to two different string object therefore these references cannot be same. Option C also represents a valid comparison e==f that evaluates as false because they point to two different Integer objects. Note that new always creates a new object. The options E and F will not even compile. The condition in E tries to compare primitive int d and Integer object reference e which is an illegal comparison. Option F also has illegal comparison. It compares a string reference with a primitive int which is not allowed.
5.þ D
The correct answer is D. The condition in if statement evaluates to false. a++ will increment a to 7 but the expression itself will return 6. Since 6 is not equal to value of b, expression a++ == b evaluates false. Therefore the else part is executed and b = 7 is printed.
Option A and B are wrong as the ternary operator on line 5 is never executed. Option E is incorrect. There is nothing wrong in putting an expression. Had it executed the ternary operator expression line 5 would have returned an int value.
6. þ B
The correct answer is B. The reference v is an instance of Vector. But since Object is a superclass of Vector, the expression v instanceof Object is valid and return true. The second condition s==null is true as well. You may recall that the member variables of class are automatically initialized. Since variable s is a String variable that will be automatically initialized to null. Hence s==null will be true. Therefore, s will be set to “checked” and will be printed at line 11.
7. þ C
The correct answer is C. Every bit in bit representations of a and b will be ANDed. Ther result bit representation will be 00000001 which is represents the value 1.
A and B are invalid. They both represent the bit representation. Note that whenever the question is asked about the value, your answer cannot be the bit representation of that value. Instead, it should be the numeric value for that bit representation.
8. þ A
The correct answer is A. The first if condition s1==s2 return false as s1 and s2 are pointing to two different string objects. The second if condition s1.equals(s3) returns true as the contents of strings referred by s1 and s2 are same. Thus the first “Equal ” is printed. The third if condition s2.equals(s3) also returns true as the contents of strings referred by s2 and s3 are same. Thus the second “Equal ” is printed. The forth if condition s4 == s5 returns false. The variable s4 is pointing to string literal whereas s5 points to a string object. Therefore, the two cannot be same. Hence “Identical ” is not printed. The final if condition s4.equals("The obstacle is the path") compares s4 directly with a string literal. Since equals() method checks only contents, it returns true and the third “Equal” is printed.
9. þ AEF
The correct answer is A. The condition specified at line 7 has two expressions. The first names[i + 1].equals("Pluto") evaluates to true when i =0 and “Hello Pluto” is stored in names array at index 0. The names array will be {“Hello Pluto”, “Pluto” , “Donald”} after that.
Since the loop breaks after the condition at line 7 cannot throw ArrayIndexOutOfBoundsException. Therefore H is incorrect.
10. þ A C D E F
All expressions return true except the option B. The condition3 evaluates to true as it is XOR operation between false and true. Option A is also true. First condition1 & condition2 is evaluated (since order of execution is from left to right and & has higher precedence over |) which evaluates to false. Then condition2 & condition3 is evaluated which returns true. At last the | operator is applied to the results false (condition1 & condition2) and true (condition2 & condition3). This OR operation returns true. Option C similarly evaluates to true. Since & has a precedence over |, first the expression condition2 & condition2 is evaluated which is true and then it is | (ORed) with condition1 and then condition3 and the result is true. Option D and E may be confusing but they are valid. Option D use the complement ! operator in assignment. Therefore condition2 is assigned value !condition1. Since assignment returns value, condition2 = !condition1 returns true. Option E on the other hand uses a != comparison operator. The comparison condition2 != condition1 returns true as condition2 is indeed not-equal-to condition1. Option F performs XOR and returns true.
Option B is the only incorrect option. It does not return true. In fact, it is not even a valid operation as ~ operator cannot be used with boolean operands.