|
1. þ A
The string reference str1 will point to “xxy” after line 3. It remains unchanged until line 7. The reference str2 initially point at string “yyz”. Therefore, str3 also points to “yyz” after line 5. But at line 6, str2 points to “zzx”. Hence, str3 will point at “yyz” but str2 will point at “zzx”. At line 7, these three strings are appended as “xxy” + “zzx” + “yyz” and “xxyzzxyyz” is printed.
E and F are incorrect; you can assign a string reference to another string reference.
2. þ D
The string reference str will point to “Once upon a ” after line 3. It remains unchanged at line 4. The concat() and toLowerCase() method calls at line 4 will create a new string object. Since the reference of this newly created string is not reassigned to str, the str still points to a string “Once upon a ”. At line 5, anotherStr points to a new string created by replacing ‘i’ with ‘I’ in string pointed by str. But the string “Once upon a” does not contain any ‘i’. Therefore replace() will not actually replace any character. Hence, anotherStr will point to a new string object with the same content as str as “Once upon a ”.
E is incorrect. There will not be any runtime exception at line 5. The replace() method does not throw any exception even if it does not find a character to be replaced in a string.
3. þ E
Total five string objects are created in this three-line code. On line 4, two string objects are created. First, a string literal “Hello” is created and then a string object is created using the new keyword. Line 5 also creates two string objects. First one is the literal “World” and another is the new string created by the + operator by concatenating “World” with the string a. On line 6, one more string object is created by + operator. Thus, total five string objects are created.
4. þ D
After line 5 is executed, the string reference a points to the string “Hello”. A call to concat() creates another new string. But it is not reassigned to a. Therefore, the variable a still points to “Hello” and not “Hello ”. On line 6 a StringBuffer object is created. On line 7 is it first appended with a character ‘!’ making “World!” as its contents. The call replace(‘l’, ‘L’) will replace only the single ‘l’ in it. At line 8, the concatenation operator + will concatenate the strings. For + if any one of the objects is string, the other is converted to a string and appended.
E is incorrect as you can concatenate a string object with StringBuffer using +. In fact you can concatenate any object with string object with +.
5. þ F
This code will not compile as there is no append() method for string object. The StringBuffer class has this method.
E is incorrect as you can concatenate a string object with char using +.
6. þ B E
B is correct, the Math.ceil(-11.2) method rounds to nearest bigger integer -11.0. Note that -11.0 is bigger than -11.2. D is also correct, the floor(-10.7) method rounds to nearest smaller integer -11.0. Note that -11.0 is smaller than -10.7.
A and C are incorrect; note that the result of Math.round() is either int or long, but never a double value. D is incorrect as Math.floor(-11.9); will be -12.0 .
7. þ C D and E
The Math.ceil(48.5) returns a double value 49.0 whereas Math.round(48.5) returns a long value 49L. However, when they are compared at line 8, the long value is promoted to double value 49.0. Since they both are same, SAN FRANCISCO is printed. Similarly, Math.ceil(75.7) returns 76.0 and round(75.7) returns 76L. They are also equal, hence PARIS is printed. Finally the Math.ceil(67) returns 67.0 and Math.round(67) returns 67L. They are also equal and hence KYOTO is printed.
8. þ B C D
B is correct. Since 101.2 is floating-point literal, it is of type double. Therefore , the method min(double,double) will be invoked which returns a result of type double. The first argument is promoted to double. Since 100.0 is smaller than 101.2, it is returned. C is also correct. The abs() method return type is same as its argument. Therefore, it returns a positive 100.0 which is a double value. D is also correct. Note Math class has only one sqrt() method and it always returns a double value. Since squareroot of 10000 is 100, the sqrt(10000) will return 100.0
A is incorrect as max(100,75) will invoke the max(int,int) version of method max(). It will return a int value 100. E is incorrect as abs() will return 100.1 as absolute value (not the 100.0 )
9. þ B and D
Note that the question asks “Which of the following can be a … ”. In other words, the question asks you about a possibility. The random() function can generate a double value which is greater than or equal to 0.0. Therefore, the variable no can be assigned a value 0. Therefore, B is correct. D is also correct as it is possible that random() will result is such as double number (for example, 0.98) which when multiplied by 100 (and cast to int) gives 98.
However, E is incorrect. The random() never generates a double value 1.0. Therefore it is impossible to print 100 is the given setup. Options A and C are incorrect as well as the number being printed, no, is an int number. F is incorrect too. We can definitely say that no will always be value such that 0 <= no < 100 in the given program.
10. þ C
Option C will correctly give you sine of 90 degrees. The Math.sin() method expects the angle in radians. Since we want to find out sine of angle in degrees, we must first convert the angle in degrees into the angle in radians. Then we can pass it to the Math.sin(). The Math.toRadians(90) will do just that.
A and B will compile as sin() can take any double value. But it will not give you sine of angle 90 degrees. D and E will not even compile.
11. þ A C D E
A is valid way of creating a Long object as you can pass a value of appropriate type (in case of Long it can be either long or any narrower type) to the constructor of the wrapper. Options D and E are valid as you can also pass a string representation of appropriate value to all wrapper constructor (except Character). C is also correct. Note that ‘c’ is a char value. Since char is narrower than int, a char value can be passed to a method expecting an int value.
B is incorrect, as the Float constructor cannot accept a value of wider data type double. Note 12.34 is floating-point literal, which is of type double by default.
12. þ C
The correct answer is C. The value of n1 is equal to n4. n2 is 10 in binary which will be the value 2 in decimal number system. Therefore, n2 is not equal to n1. The variable n3 is not equal to n1 either It represents the “10” in hexadecimal (which is 160 in decimal) which is not equal to n1 that holds 10 is decimal number system. On the other hand, the long value in n4 also represents 10 in decimal number system. Hence n1==n4 will be true and 3 will be printed.
13. þ C E F
The fifth line will cause a compilation error. The parseXxx() methods always accept only a string as an argument. But we are passing a numeric value in Integer.parseInt(15). The seventh line will also cause a compilation error. Note that n4 is of type int. The primitives are not objects and hence do not support any methods to invoked on them. Hence n4.intValue(); is invalid. The eighth line will cause compilation error as valueOf() methods return wrapper object. Therefore Integer.valueOf("15"); will return a Integer object which cannot be assigned to a variable n6 of primitive type int.
Option A will not cause any compilation problem. Though “1010” appears to be a binary number, it can be treated as decimal number also. Option B will not cause compilation error as Integer.parseInt("10", 2) treats “10” as string representation of binary number and parses it to int value.
14. þ D
Note that b1 is a reference to a wrapper object of type Boolean whereas b2 is variable of primitive type boolean. The & operators works only on boolean primitive values. Therefore, b1 & b2 is an invalid expression. If we remove the eighth line, the code will compile successfully.
A is incorrect; you can pass string “FALSE” to Boolean constructor. The constructor is case-insensitive and accepts any string that represents “true” or “false” with the case ignored. B is also incorrect. Math.floor(7.8) results in a double value 8.0 whereas d.intValue() will result in int value 8. Since they both are equal, b2 will be true.
15. þ B C
B is correct as i3 is 0 and f is 0.0f , hence they are equal. C is also correct. The equals() method in Integer checks whether the argument object is of type Integer and whether it warps the same value as the current Integer object. Since i2 is also Integer object and it wraps the same value as i1, i1.equals(i2) returns true.
A is incorrect; the equals() method in Boolean first checks whether the argument object is Boolean. In b.equals(“true”) , the argument passed is a string object, hence it can not be compared with Boolean object. Therefore b.equals(“true”) returns false. Options D and E both print false. Note that obj1 and obj2 are two distinct objects of type WrapperTest. Since WrapperTest does not override the equals() method, it inherits it from the Object class. This inherited equals() method does the object reference comparison. Since obj1 and obj2 points to two different objects, obj1 == obj2 will be false and hence the equals() will return false.