Q1. Given:
What is the result?
A. Area is 6.0
B. Area is 3.0
C. Compilation fails at line n1
D. Compilation fails at line n2.
Answer: D
Q2. Given the code fragment:
What is the result?
A. Match 1
B. Match 2
C. No Match
D. A NullPointerException is thrown at runtime.
Answer: B
Explanation:
it will compare the string contents of the StringBuilder with string object.
Q3. Given the code fragment:
Which three lines fail to compile?
A. Line 7
B. Line 8
C. Line 9
D. Line 10
E. Line 11
F. Line 12
Answer: A,D,F
Q4. Given the code fragment?
public class Test {
public static void main(String[] args) {
Test t = new Test();
int[] arr = new int[10];
arr = t.subArray(arr,0,2);
}
// insert code here
}
Which method can be inserted at line // insert code here to enable the code to compile?
A. public int[] subArray(int[] src, int start, int end) {
return src;
}
B. public int subArray(int src, int start, int end) {
return src;
}
C. public int[] subArray(int src, int start, int end) {
return src;
}
D. public int subArray(int[] src, int start, int end) {
return src;
}
Answer: A
Q5. Given:
What is the result?
A. true:true
B. true:false
C. false:true
D. false:false
Answer: C
Q6. Given the code fragment:
What is the result?
A. Element 0 Element 1
B. Null element 0 Null element 1
C. Null Null
D. A NullPointerException is thrown at runtime.
Answer: D
Q7. Given:
public class TestLoop {
public static void main(String[] args) {
int array[] = {0, 1, 2, 3, 4};
int key = 3;
for (int pos = 0; pos < array.length; ++pos) {
if (array[pos] == key) {
break;
}
}
System.out.print("Found " + key + "at " + pos);
}
}
What is the result?
A. Found 3 at 2
B. Found 3 at 3
C. Compilation fails
D. An exception is thrown at runtime
Answer: C
Explanation: The following line does not compile: System.out.print("Found " + key + "at " + pos);
The variable pos is undefined at this line, as its scope is only valid in the for loop. Any variables created inside of a loop are LOCAL TO THE LOOP.
Q8. Given the code fragment:
What is the result?
A. 100
B. 101
C. 102
D. 103
E. Compilation fails
Answer: E
Q9. Given the code fragment:
What is the result?
A. 10 : 10
B. 5 : 5
C. 5 : 10
D. Compilation fails
Answer: A
Q10. Given the code fragment:
for (int ii = 0; ii < 3;ii++) {
int count = 0;
for (int jj = 3; jj > 0; jj--) {
if (ii == jj) {
++count;
break;
}
}
System.out.print(count); continue; }
What is the result?
A. 011
B. 012
C. 123
D. 000
Answer: A