Q1. Given:
What is the output?
A. [21, 13, 11]
B. [30]
C. []
D. Compilation fails due to error at line 7
E. Compilation tails due to error at line 10
Answer: D
Explanation:
Option D is the correct answer.
Code fails to compile as we can't use primitive for collections type, so in this code trying to
use int at line 7, causes a compile error. We should have use wrapper. Integer there. So
option D is correct.
https://docs.oracle.eom/javase/8/docs/api/java/util/ArrayList.html
Q2. 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
Q3. Given the code fragment from three files:
Which code fragment, when inserted at line 2, enables the code to compile?
A. Option A
B. Option B
C. Option C
D. Option D
E. Option E
Answer: E
Q4. Given:
What is the result?
A. Good Day! Good Luck!
B. Good Day! Good Day!
C. Good Luck! Good Day!
D. Good Luck! Good Luck!
E. Compilation fails
Answer: E
Q5. Given:
public class Test {
public static void main(String[] args) {
try {
String[] arr =new String[4];
arr[1] = "Unix";
arr[2] = "Linux";
arr[3] = "Solarios";
for (String var : arr) {
System.out.print(var + " ");
}
} catch(Exception e) {
System.out.print (e.getClass());
}
}
}
What is the result?
A. Unix Linux Solaris
B. Null Unix Linux Solaris
C. Class java.lang.Exception
D. Class java.lang.NullPointerException
Answer: B
Explanation: null Unix Linux Solarios
The first element, arr[0], has not been defined.
Q6. Which two are Java Exception classes?
A. SercurityException
B. DuplicatePathException
C. IllegalArgumentException
D. TooManyArgumentsException
Answer: A,C
Q7. Which statement will empty the contents of a StringBuilder variable named sb?
A. sb.deleteAll();
B. sb.delete(0, sb.size());
C. sb.delete(0, sb.length());
D. sb.removeAll();
Answer: C
Q8. Given:
public class ComputeSum {
public int x;
public int y;
public int sum;
public ComputeSum (int nx, int ny) {
x = nx; y =ny;
updateSum();
}
public void setX(int nx) { x = nx; updateSum();}
public void setY(int ny) { x = ny; updateSum();}
void updateSum() { sum = x + y;}
}
This class needs to protect an invariant on the sum field.
Which three members must have the private access modifier to ensure that this invariant is maintained?
A. The x field
B. The y field
C. The sum field
D. The ComputerSum ( ) constructor
E. The setX ( ) method
F. The setY ( ) method
Answer: C,E,F
Explanation: The sum field and the two methods (setX and SetY) that updates the sum field.
Q9. Given:
How many objects have been created when the line / / do complex stuff is reached?
A. Two
B. Three
C. Four
D. Six
Answer: C
Q10. Given the code fragment:
Which code fragment prints blue, cyan, ?
A. Option A
B. Option B
C. Option C
D. Option D
Answer: A
Q11. public class StringReplace {
public static void main(String[] args) {
String message = "Hi everyone!";
System.out.println("message = " + message.replace("e", "X")); }
}
What is the result?
A. message = Hi everyone!
B. message = Hi XvXryonX!
C. A compile time error is produced.
D. A runtime error is produced.
E. message =
F. message = Hi Xveryone!
Answer: B
Q12. Given:
Which of the following is equivalent to the above code fragment?
A. System.out.printLn(x>10?">,': "<":,'=");
B. System.out.println(x>10? ">"?"<":"=");
C. System.out.println(x>10?">":x<10?"<":"=");
D. System.out.printLn(x>10?">"?,'<"?"=");
E. None of the above
Answer: B
Explanation:
Option A is incorrect as we can't use abstract with non abstract method, (here method has method body.) Option C is incorrect as when overriding method we can't use more restrictive access modifier, so trying to use private to override default access Level method causes a compile time error. Option D is incorrect as default methods (not methods with default access level) are allowed only in interfaces. Option E is incorrect as method all ready has void as return type, so we can't add int there. Option B is correct as we can use final there, since the method is non abstract
https://docs.oracle.com/javase/tutorial/java/landl/polymorphism.html
Q13. Given the code fragment:
Assume that the system date is June 20, 2014. What is the result?
A. Option A
B. Option B
C. Option C
D. Option D
Answer: A
Q14. 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
Q15. Given the code fragment:
Which code fragment prints red: blue: small: medium?
A. Option A
B. Option B
C. Option C
D. Option D
Answer: D