You are on page 1of 4

1.

class A {
private int value;
public A (int v) {value = 10;}
public int getValue() {return value;}
}
class B extends A {
public B() {
super();
System.out.println(getValue());
}
}
What will be the result when an instance of B is created?
A.
B.
C.
D.

0 is outputted on the console window
10 is outputted on the console window
a compilation error occurs
None of these

ANSWER: C
2. Given classes A and B below:
public class A {
public void f(int a) {System.out.println(a);}
}
public class B extends A {
public void f(int b) {super.f(b + 1);}
}
What will be the result when an instance of B is created and the method f(100)
is invoked through the
instance?
A.
B.
C.
D.

100 is printed on the console window
101 is printed on the console window
1001 is printed on the console window
There will be an infinite loop and Java will report an error.

ANSWER: B
3. Given a 4 x 4 integer 2D array, arr. What would be the value stored in arr[1]
[3] after executing the
following code for initialization?
int size = 4;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
arr[i][j] = size * j + i;
}
}
A. 7
B. 2
C. 8

what does f1(4) return ? public static int f1(int n) { . What would be the returned value of the following comparisons? i) ii) A. } } System. int v2 = s2. Consider the following code statement. D. C. 0 0 3 3 1 2 1 2 ANSWER: C 5. i < 3. int v1 = s1.D. lenOfStr. C. A runtime error is generated. ANSWER: B 7. What would be the output of the following code segment? int i = 0. int j = 0. A.out. D. Which one of the following would be th e result? int lenOfStr = "Is it possible?". Given the String objects s1 and s2 below: String s1 = "zzz". C. j < 3. lenOfStr. B.println(i + " " + j). for (i = 0.length(). D. The variable. is set to 15.compareTo("aaa"). j++) { if (j == 1) break. ANSWER: D 6. i++) { for (j = 0. Given the method f1() below. The variable. String s2 = "ZZZ". is set to 0. i) v1 < 0 ii) i) v1 > 0 ii) i) v1 > 0 ii) It generates a compilation v2 > 0 v2 < 0 v2 > 0 error. B. A compilation error is generated. 13 ANSWER: D 4. B.compareTo("AAA"). A.

C. what is the return value of f3(10200)? public static int f3(int n) { if (n == 0) return 1. } A.infinite loop 2 ANSWER: B 9. Given the method f3() below. else return a. D. 4).if (n == 1) return 1. return n + f1(n 1). D. } A. "abc1" "abc12" "abc123" Error when executing the code . C. A. String src = "abc". 13 1 10 7 ANSWER: C 8. b . B. String result = src. int b) { if (b >= 1) return f2(a + 1. 10200 3 12 0 ANSWER: B 10. } A. else return f3(n / 10) + f3(n % 10). src.1). Given the method f2() below. C.substring(0. B. 2) return? public static int f2(int a. 3 5 Error . C. B. D. else if (n < 10 && n > -10) return 0. D.concat("123"). what does f2(3. B.

length() == s2. 5. 5}. } A. What is the value of the int variable. C. {0. int temp = 0. 4. 1. 3. while (temp < 10) { temp++. Given the String variables s1. } A. B. 1. 3. i++) { intArray[i + 2] = intArray[i]. 1. after executing the following piece of code? int total = 0. 2. intArray. 3. String s2 = "ZZZ". 1) and 1) and 1) and All of 3) only 2) and 4) only 3) and 4) only them ANSWER: A 12. 1. 2. D.ANSWER: D 11. 4. total += temp. after executing the followin g piece of code? int[] intArray = {0.toUpperCase() == s2 A. String s3 = "zzz". for (int i = 0. {0. 4. 1. total. 0.charAt(0) != s1. 1. if (total > 6) continue. 0.length() 2) s2 >= s1 3) s2. {0. 0. i < 4. 2. 1 6 10 55 ANSWER: D . {2. 5} 3} 1} 5} ANSWER: C 13. What is the content of the int array. C. B. C. D. Which of the following expressions is/are evaluated to true? 1) s1. s2 and s3 below: String s1 = "ABC". 4. B.charAt(2) 4) s3. D.