Q1. Given the code fragment:
class Student {
int rollnumber;
String name;
List cources = new ArrayList();
// insert code here
public String toString() {
return rollnumber + " : " + name + " : " + cources;
}
}
And,
public class Test {
public static void main(String[] args) {
List cs = newArrayList();
cs.add("Java");
cs.add("C");
Student s = new Student(123,"Fred", cs);
System.out.println(s);
}
}
Which code fragment, when inserted at line // insert code here, enables class Test to print 123 : Fred : [Java, C]?
A.
private Student(int i, String name, List cs) {
/* initialization code goes here */
}
B.
public void Student(int i, String name, List cs) {
/* initialization code goes here */
}
C.
Student(int i, String name, List cs) {
/* initialization code goes here */
}
D.
Student(int i, String name, ArrayList cs) {
/* initialization code goes here */
}
Answer: C
Explanation:
Incorrect:
Not A: Student has private access line: Student s = new Student(123,"Fred", cs);
Not D: Cannot be applied to given types. Line: Student s = new Student(123,"Fred", cs);
Q2. Given:
class Sports {
int num_players;
String name, ground_condition;
Sports(int np, String sname, String sground){
num_players = np;
name = sname;
ground_condition = sground;
}
}
class Cricket extends Sports {
int num_umpires;
int num_substitutes;
Which code fragment can be inserted at line //insert code here to enable the code to compile?
A. Cricket() {
super(11, "Cricket", "Condidtion OK");
num_umpires =3;
num_substitutes=2;
}
B. Cricket() {
super.ground_condition = "Condition OK";
super.name="Cricket";
super.num_players = 11;
num_umpires =3;
num_substitutes=2;
}
C. Cricket() {
this(3,2);
super(11, "Cricket", "Condidtion OK");
}
Cricket(int nu, ns) {
this.num_umpires =nu;
this.num_substitutes=ns;
}
D. Cricket() {
this.num_umpires =3;
this.num_substitutes=2;
super(11, "Cricket", "Condidtion OK");
}
Answer: A
Explanation:
Incorrect:
not C, not D: call to super must be the first statement in constructor.
Q3. Given the code fragment:
What is the result?
A. [Robb, Rick, Bran]
B. [Robb, Rick]
C. [Robb, Bran, Rick, Bran]
D. An exception is thrown at runtime.
Answer: A
Q4. Given:
public class Test {
public static void main(String[] args) {
int arr[] = new int[4];
arr[0] = 1;
arr[1] = 2;
arr[2] = 4;
arr[3] = 5;
int sum = 0;
try {
for (int pos = 0; pos <= 4; pos++) {
sum = sum + arr[pos];
}
} catch (Exception e) {
System.out.println("Invalid index");
}
System.out.println(sum);
}
}
What is the result?
A. 12
B. Invalid Index
C. Invalid Index
D. Compilation fails
Answer: B
Explanation: The loop ( for (int pos = 0; pos <= 4; pos++) { ), it should be pos <= 3, causes an exception, which is caught. Then the correct sum is printed.
Q5. Which two are benefits of polymorphism?
A. Faster code at runtime
B. More efficient code at runtime
C. More dynamic code at runtime
D. More flexible and reusable code
E. Code that is protected from extension by other classes
Answer: C,D
Q6. Given: class Base {
public static void main(String[] args) {
System.out.println("Base " + args[2]);
}
}
public class Sub extends Base{
public static void main(String[] args) {
System.out.println("Overriden " + args[1]);
}
}
And the commands:
javac Sub.java
java Sub 10 20 30
What is the result?
A. Base 30
B. Overridden 20
C. Overridden 20 Base 30
D. Base 30 Overridden 20
Answer: B
Q7. Given the code fragment
int var1 = -5;
int var2 = var1--;
int var3 = 0;
if (var2 < 0) {
var3 = var2++;
} else {
var3 = --var2;
}
System.out.println(var3);
What is the result?
A. – 6
B. – 4
C. – 5
D. 5
E. 4
F. Compilation fails
Answer: C
Q8. Given the following array:
Which two code fragments, independently, print each element in this array?
A. Option A
B. Option B
C. Option C
D. Option D
E. Option E
F. Option F
Answer: B,E
Explanation: All the remaining options have syntax errors
Q9. Given the code fragment:
What is the result?
A. 100
B. 101
C. 102
D. 103
E. Compilation fails
Answer: E
Q10. Given:
What is the result?
A. hEllOjAvA!
B. Hello java!
C. Out of limits hEllOjAvA!
D. Out of limits
Answer: C
Q11. Given:
public class FieldInit {
char c;
boolean b;
float f;
void printAll() {
System.out.println("c = " + c);
System.out.println("c = " + b);
System.out.println("c = " + f);
}
public static void main(String[] args) {
FieldInit f = new FieldInit();
f.printAll();
}
}
What is the result?
A. c = null
b = false
f = 0.0F
B. c = 0
b = false
f = 0.0f
C. c = null
b = true
f = 0.0
D. c =
b = false
f = 0.0
Answer: D
Q12. Given:
A. X XX
B. X Y X
C. Y Y X
D. Y YY
Answer: D
Q13. Given the code fragment:
public class ForTest {
public static void main(String[] args) {
int[] array = {1, 2, 3};
for ( foo ) {
}
}
Which three code fragments, when replaced individually for foo, enables the program to compile?
A. int i : array
B. int i = 0; i < 1;
C. ; ;
D. ; i < 1; i++
E. i = 0; i<1;
Answer: A,B,C
Q14. Given the fragment:
String[][] arra = new String[3][];
arra[0] = new String[]{"rose", "lily"};
arra[1] = new String[]{"apple", "berry","cherry","grapes"};
arra[0] = new String[]{"beans", "carrot","potato"};
// insert code fragment here
Which code fragment when inserted at line '// insert code fragment here', enables the code to successfully change arra elements to uppercase?
A. String[][] arra = new String[3][];
arra[0] = new String[]{"rose", "lily"};
arra[1] = new String[]{"apple", "berry","cherry","grapes"};
arra[0] = new String[]{"beans", "carrot","potato"};
for (int i = 0; i < arra.length; i++) {
for (int j=0; j < arra[i].length; j++) {
arra[i][j] = arra[i][j].toUpperCase();
}
}
B. for (int i = 0; i < 3; i++) {
for (int j=0; j < 4; j++) {
arra[i][j] = arra[i][j].toUpperCase();
}
}
C. for (String a[]:arra[][]) {
for (String x:a[]) {
D. toUpperCase();
}
}
E. for (int i:arra.length) {
for (String x:arra) {
arra[i].toUpperCase();
}
}
Answer: C
Explanation:
Incorrect:
not A: arra.length is 3, but the subarrays have 2, 3 and 4 elements. Index will be out of
bound.
not B: The subarrys are of different lengths. Index will be out of bound.
not D: Compile error.
Q15. Given:
What is the result?
A. Marrown
String out of limits
JesOran
B. Marrown
String out of limits
Array out of limits
C. Marrown
String out of limits
D. Marrown
NanRed
JesOran
Answer: A