Tuesday, September 20, 2011

Quiz of the day | OCPJP (15)

Given:

2. public class X {
3. static int x = 1;
4. public static void main(String[] args) {
5. int x = 2;
6. for(int i=0; i< 3; i++) {
7. if(i==1) System.out.print(x + " ");
8. }
9. go();
10. System.out.print(x + " " + i);
11. }
12. static void go() { int x = 3; }
13. }

What is the result?
A. 1 2 2
B. 2 2 2
C. 2 2 3
D. 2 3 2
E. Compilation fails.
F. An exception is thrown at runtime.


DOWNLOAD the answer

Monday, September 19, 2011

Quiz of the day | OCPJP (14)

1. import java.util.*;
2. public class BCA {
3. public static void main(String[] args) {
4. List desk = new ArrayList();
5. desk.add("b"); desk.add("c"); desk.add("a");
6. Collection.sort(desk);
7. System.out.print(desk.indexOf("b"));
8. Collection.reverse(desk);
9. System.out.print(" " + desk.indexOf("c"));
10. System.out.println(" " + desk.indexOf("a"));
11. } }

What is the result?

A. 1 2 0
B. 2 2 1
C. 2 0 2
D. 1 0 2
E. Compilation fails.
F. An exception is thrown at runtime.


DOWNLOAD the answer

Sunday, September 18, 2011

Quiz of the day | OCPJP (13)

2. class Parent {
3. static String parent = "";
4. void doStuff() { parent += "parent "; }
5. }
6. public class Child extends Parent {
7. public static void main(String[] args) {
8. new Child().go();
9. }
10. void go() {
11. Parent p = new Child();
12. Child c = (Child)p;
13. // insert code here
14. }
15. void doStuff() { parent += "child "; }
16. }


If the rest of the code compiles, which line(s) of code, inserted independently at line 13, compile? (Choose all that apply.)

A. c.doStuff();
B. p.doStuff();
C. this.doStuff();
D. super.doStuff();
E. c.super.doStuff();
F. p.super.doStuff();
G. this.super.doStuff();
H. There are other errors in the code.


DOWNLOAD the answer

Saturday, September 17, 2011

Quiz of the day | OCPJP (12)

3. public class C {
4. public static void main(String[] c) {
5. int i = 23;
6. Integer i2;
7. Integer i3;
8. i2 = i.intValue();
9. i3 = i.valueOf(23);
10. System.out.println((i == i2) + " " + (i == i3));
11. } }

What is the result?
A. true true
B. true false
C. false true
D. false false
E. Compilation fails.
F. An exception is thrown at runtime.


DOWNLOAD the answer

Friday, September 16, 2011

Quiz of the day | OCPJP (11)

3. class P {
4. P play() { System.out.print("p "); return new P(); }
5. P play(int x) { System.out.print("p x "); return new P(); }
6. }
7. class Q extends P {
8. Q play() { System.out.print("q "); return new Q(); }
9. P play(int x) { System.out.print("r "); return new P(); }
10.
11. public static void main(String[] args) {
12. new Q().play();
13. new Q().play(7);
14. super.play(7);
15. new P().play();
16. P s = new Q();
17. s.play();
18. } }

What is the result?

A. q r r p p
B. q r p x p r
C. q r p x p q
D. Compilation fails due to a single error.
E. Compilation fails due to errors on more than one line.


DOWNLOAD the answer