repeated scjp questions on operators
Friday, January 29, 2010
6:04 AM
,
0 Comments
Labels: repeated questions in scjp exam , scjp interview quesions , SCJP MATERIAL , SCJP Mock Exam , scjp objects , scjp sun certification material , Sun Certified Java Programmer (SCJP)
Labels: repeated questions in scjp exam , scjp interview quesions , SCJP MATERIAL , SCJP Mock Exam , scjp objects , scjp sun certification material , Sun Certified Java Programmer (SCJP)
Question: 04
Given:
42. public class ClassA {
43. public int getValue() {
44.int value=0;
45. boolean setting = true;
46. String title=”Hello”;
47. if (value || (setting && title == “Hello”)) { return 1; }
48. if (value == 1 & title.equals(”Hello”)) { return 2; }
49. }
50. }
And:
70. ClassA a = new ClassA();
71. a.getValue();
What is the result?
A. 1
B. 2
C. Compilation fails.
D. The code runs with no output.
E. An exception is thrown at runtime.
Answer: C
5. Given:
class Hexy {
public static void main(String[] args) {
Integer i = 42;
String s = (i<40)?"life":(i>50)?"universe":"everything";
System.out.println(s);
} }
What is the result?
A. null
B. life
C. universe
D. everything
E. Compilation fails.
F. An exception is thrown at runtime.
Answer:
-> D is correct. This is a ternary nested in a ternary with a little unboxing thrown in.
Both of the ternary expressions are false.
-> A, B, C, E, and F are incorrect based on the above.
6. Given:
1. class Example {
2. public static void main(String[] args) {
3. Short s = 15;
4. Boolean b;
5. // insert code here
6. }
7. }
Which, inserted independently at line 5, will compile? (Choose all that apply.)
A. b = (Number instanceof s);
B. b = (s instanceof Short);
C. b = s.instanceof(Short);
D. b = (s instanceof Number);
E. b = s.instanceof(Object);
F. b = (s instanceof String);
Answer:
-> B and D correctly use boxing and instanceof together.
-> A is incorrect because the operands are reversed. C and E use incorrect instance of syntax. F is wrong because Short isn't in the same inheritance tree as String.
7. Given:
1. class Comp2 {
2. public static void main(String[] args) {
3. float f1 = 2.3f;
4. float[][] f2 = {{42.0f}, {1.7f, 2.3f}, {2.6f, 2.7f}};
5. float[] f3 = {2.7f};
6. Long x = 42L;
7. // insert code here
8. System.out.println("true");
9. }
10. }
And the following five code fragments:
F1. if(f1 == f2)
F2. if(f1 == f2[2][1])
F3. if(x == f2[0][0])
F4. if(f1 == f2[1,1])
F5. if(f3 == f2[2])
What is true?
A. One of them will compile, only one will be true.
B. Two of them will compile, only one will be true.
C. Two of them will compile, two will be true.
D. Three of them will compile, only one will be true.
E. Three of them will compile, exactly two will be true.
F. Three of them will compile, exactly three will be true.
Answer:
-> D is correct. Fragments F2, F3, and F5 will compile, and only F3 is true.
-> A, B, C, E, and F are incorrect. F1 is incorrect because you can’t compare a primitive to an array. F4 is incorrect syntax to access an element of a two-dimensional array.
8. Given:
class Fork {
public static void main(String[] args) {
if(args.length == 1 | args[1].equals("test")) {
System.out.println("test case");
} else {
System.out.println("production " + args[0]);
} } }
And the command-line invocation:
java Fork live2
What is the result?
A. test case
B. production
C. test case live2
D. Compilation fails.
E. An exception is thrown at runtime.
Answer:
-> E is correct. Because the short circuit (||) is not used, both operands are evaluated. Since args[1] is past the args array bounds, an ArrayIndexOutOfBoundsException is thrown.
-> A, B, C, and D are incorrect based on the above.
Given:
42. public class ClassA {
43. public int getValue() {
44.int value=0;
45. boolean setting = true;
46. String title=”Hello”;
47. if (value || (setting && title == “Hello”)) { return 1; }
48. if (value == 1 & title.equals(”Hello”)) { return 2; }
49. }
50. }
And:
70. ClassA a = new ClassA();
71. a.getValue();
What is the result?
A. 1
B. 2
C. Compilation fails.
D. The code runs with no output.
E. An exception is thrown at runtime.
Answer: C
5. Given:
class Hexy {
public static void main(String[] args) {
Integer i = 42;
String s = (i<40)?"life":(i>50)?"universe":"everything";
System.out.println(s);
} }
What is the result?
A. null
B. life
C. universe
D. everything
E. Compilation fails.
F. An exception is thrown at runtime.
Answer:
-> D is correct. This is a ternary nested in a ternary with a little unboxing thrown in.
Both of the ternary expressions are false.
-> A, B, C, E, and F are incorrect based on the above.
6. Given:
1. class Example {
2. public static void main(String[] args) {
3. Short s = 15;
4. Boolean b;
5. // insert code here
6. }
7. }
Which, inserted independently at line 5, will compile? (Choose all that apply.)
A. b = (Number instanceof s);
B. b = (s instanceof Short);
C. b = s.instanceof(Short);
D. b = (s instanceof Number);
E. b = s.instanceof(Object);
F. b = (s instanceof String);
Answer:
-> B and D correctly use boxing and instanceof together.
-> A is incorrect because the operands are reversed. C and E use incorrect instance of syntax. F is wrong because Short isn't in the same inheritance tree as String.
7. Given:
1. class Comp2 {
2. public static void main(String[] args) {
3. float f1 = 2.3f;
4. float[][] f2 = {{42.0f}, {1.7f, 2.3f}, {2.6f, 2.7f}};
5. float[] f3 = {2.7f};
6. Long x = 42L;
7. // insert code here
8. System.out.println("true");
9. }
10. }
And the following five code fragments:
F1. if(f1 == f2)
F2. if(f1 == f2[2][1])
F3. if(x == f2[0][0])
F4. if(f1 == f2[1,1])
F5. if(f3 == f2[2])
What is true?
A. One of them will compile, only one will be true.
B. Two of them will compile, only one will be true.
C. Two of them will compile, two will be true.
D. Three of them will compile, only one will be true.
E. Three of them will compile, exactly two will be true.
F. Three of them will compile, exactly three will be true.
Answer:
-> D is correct. Fragments F2, F3, and F5 will compile, and only F3 is true.
-> A, B, C, E, and F are incorrect. F1 is incorrect because you can’t compare a primitive to an array. F4 is incorrect syntax to access an element of a two-dimensional array.
8. Given:
class Fork {
public static void main(String[] args) {
if(args.length == 1 | args[1].equals("test")) {
System.out.println("test case");
} else {
System.out.println("production " + args[0]);
} } }
And the command-line invocation:
java Fork live2
What is the result?
A. test case
B. production
C. test case live2
D. Compilation fails.
E. An exception is thrown at runtime.
Answer:
-> E is correct. Because the short circuit (||) is not used, both operands are evaluated. Since args[1] is past the args array bounds, an ArrayIndexOutOfBoundsException is thrown.
-> A, B, C, and D are incorrect based on the above.



0 Response to "repeated scjp questions on operators"
Post a Comment