Java MCQ
Java MCQ
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.
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.
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
D. 13
ANSWER: D
4. What would be the output of the following code segment?
int i = 0;
int j = 0;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (j == 1) break;
}
}
System.out.println(i + " " + j);
A.
B.
C.
D.
0
0
3
3
1
2
1
2
ANSWER: C
5. Consider the following code statement. Which one of the following would be th
e result?
int lenOfStr = "Is it possible?".length();
A.
B.
C.
D.
ANSWER: D
6. Given the String objects s1 and s2 below:
String s1 = "zzz";
String s2 = "ZZZ";
What would be the returned value of the following comparisons?
i)
ii)
A.
B.
C.
D.
int v1 = s1.compareTo("AAA");
int v2 = s2.compareTo("aaa");
i)
v1 < 0
ii)
i)
v1 > 0
ii)
i)
v1 > 0
ii)
It generates a compilation
v2 > 0
v2 < 0
v2 > 0
error.
ANSWER: B
7. Given the method f1() below, what does f1(4) return ?
public static int f1(int n) {
if (n == 1)
return 1;
return n + f1(n
1);
}
A.
B.
C.
D.
13
1
10
7
ANSWER: C
8. Given the method f2() below, what does f2(3, 2) return?
public static int f2(int a, int b) {
if (b >= 1)
return f2(a + 1, b - 1);
else
return a;
}
A.
B.
C.
D.
3
5
Error - infinite loop
2
ANSWER: B
9. Given the method f3() below, what is the return value of f3(10200)?
public static int f3(int n) {
if (n == 0)
return 1;
else if (n < 10 && n > -10)
return 0;
else
return f3(n / 10) + f3(n % 10);
}
A.
B.
C.
D.
10200
3
12
0
ANSWER: B
10. String src = "abc";
src.concat("123");
String result = src.substring(0, 4);
A.
B.
C.
D.
"abc1"
"abc12"
"abc123"
Error when executing the code
ANSWER: D
11. Given the String variables s1, s2 and s3 below:
String s1 = "ABC";
String s2 = "ZZZ";
String s3 = "zzz";
Which of the following expressions is/are evaluated to true?
1) s1.length() == s2.length()
2) s2 >= s1
3) s2.charAt(0) != s1.charAt(2)
4) s3.toUpperCase() == s2
A.
B.
C.
D.
1) and
1) and
1) and
All of
3) only
2) and 4) only
3) and 4) only
them
ANSWER: A
12. What is the content of the int array, intArray, after executing the followin
g piece of code?
int[] intArray = {0, 1, 2, 3, 4, 5};
for (int i = 0; i < 4; i++) {
intArray[i + 2] = intArray[i];
}
A.
B.
C.
D.
{0,
{0,
{0,
{2,
1,
1,
1,
3,
2,
0,
0,
4,
3,
1,
1,
5,
4,
2,
0,
4,
5}
3}
1}
5}
ANSWER: C
13. What is the value of the int variable, total, after executing the following
piece of code?
int total = 0;
int temp = 0;
while (temp < 10) {
temp++;
total += temp;
if (total > 6) continue;
}
A.
B.
C.
D.
1
6
10
55
ANSWER: D