Java-MCQ

Embed Size (px)

DESCRIPTION

java questions

Citation preview

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. 0 is outputted on the console window B. 10 is outputted on the console window C. a compilation error occurs D. None of theseANSWER: C2. 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. 100 is printed on the console window B. 101 is printed on the console window C. 1001 is printed on the console window D. There will be an infinite loop and Java will report an error.ANSWER: B3. 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. 7B. 2C. 8D. 13ANSWER: D4. 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. 0 1 B. 0 2 C. 3 1 D. 3 2 ANSWER: C5. Consider the following code statement. Which one of the following would be the result? int lenOfStr = "Is it possible?".length(); A. A compilation error is generated. B. A runtime error is generated. C. The variable, lenOfStr, is set to 0. D. The variable, lenOfStr, is set to 15. ANSWER: D6. 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) int v1 = s1.compareTo("AAA"); ii) int v2 = s2.compareTo("aaa"); A. i) v1 < 0 ii) v2 > 0 B. i) v1 > 0 ii) v2 < 0 C. i) v1 > 0 ii) v2 > 0 D. It generates a compilation error. ANSWER: B7. 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. 13 B. 1 C. 10 D. 7 ANSWER: C8. 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. 3 B. 5 C. Error - infinite loop D. 2 ANSWER: B9. 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. 10200 B. 3 C. 12 D. 0 ANSWER: B10. String src = "abc"; src.concat("123"); String result = src.substring(0, 4); A. "abc1" B. "abc12" C. "abc123" D. 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 >= s13) s2.charAt(0) != s1.charAt(2)4) s3.toUpperCase() == s2 A. 1) and 3) only B. 1) and 2) and 4) only C. 1) and 3) and 4) only D. All of them ANSWER: A12. What is the content of the int array, intArray, after executing the following piece of code?int[] intArray = {0, 1, 2, 3, 4, 5};for (int i = 0; i < 4; i++) { intArray[i + 2] = intArray[i];} A. {0, 1, 2, 3, 4, 5} B. {0, 1, 0, 1, 2, 3} C. {0, 1, 0, 1, 0, 1} D. {2, 3, 4, 5, 4, 5} ANSWER: C13. 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. 1 B. 6 C. 10 D. 55 ANSWER: D