Friday, September 30, 2011

Quiz of the day | OCPJP (25)

Which is true? (Choose all that apply.)

A. "C extends I" is correct if and only if C is a class and I is an interface
B. "C extends I" is correct if and only if C is an interface and I is a class
C. "C extends I" is correct if C and I are either both classes or both interfaces
D. "C extends I" is correct for all combinations of C and I being classes and/or interfaces


DOWNLOAD the answer

Thursday, September 29, 2011

Quiz of the day | OCPJP (24)

3. public class J {
4. public static void main(String[] args) {
5. int j = 7;
6. assert(++j > 7);
7. assert(++j > 8): "Jakarta";
8. assert(j > 10): j=12;
9. assert(j==12): macet();
10. assert(j==12): new J();
11. }
12. static void macet() { }
13. }

Which are true? (Choose all that apply.)

A. Compilation succeeds
B. Compilation fails due to an error on line 6
C. Compilation fails due to an error on line 7
D. Compilation fails due to an error on line 8
E. Compilation fails due to an error on line 9
F. Compilation fails due to an error on line 10


DOWNLOAD the answer

Wednesday, September 28, 2011

Quiz of the day | OCPJP (23)

Given:

3. class A {
4. public void a() { System.out.print("A "); }
5. }
6. class B extends A {
7. public void b() { System.out.print("B "); }
8. public void a() { System.out.print("a "); }
9. }
10. public class C {
11. public static void main(String[] args) { new C().c(); }
12. void c() {
13. new B().a();
14. ((A) new B()).a();
15. ((A) new B()).b();
16. }
17. }

What is the result? (Choose all that apply.)

A. a a B
B. a A B
C. a a followed by an exception
D. a A followed by an exception
E. Compilation fails with an error at line 14
F. Compilation fails with an error at line 15

DOWNLOAD the answer

Tuesday, September 27, 2011

Quiz of the day | OCPJP (22)

Given:

1. enum Menu {HOME, ABOUT, CONTACT};
2. public class Main {
3. public static void main(String[] args) {
4. Main m = new Main();
5. Menu[] v = Menu.values();
6. v = Menu.getValues();
7. for(Menu me: Menu.values()) m.getEnum(me);
8. for(int x = 0; x < Menu.values().length; x++) m.getEnum(v[x]);
9. for(int x = 0; x < Menu.length; x++) m.getEnum(v[x]);
10. for(Menu me: v) m.getEnum(me);
11. }
12. public void getEnum(Menu e) {
13. System.out.print(e + " ");
14. } }


Which line(s) of code will cause a compiler error? (Choose all that apply.)

A. line 1
B. line 5
C. line 6
D. line 7
E. line 8
F. line 9
G. line 10
H. line 12


DOWNLOAD the answer

Monday, September 26, 2011

Quiz of the day | OCPJP (21)

Given:

3. public class Sep26 {
4. public static void main(String[] args) {
5. short f4 = 4;
6. new Sep26().go(f4);
7. new Sep26().go(new Integer(8));
8. }
9. void go(Short s) { System.out.print("S "); }
10. void go(Long l) { System.out.print("L "); }
11. void go(int i) { System.out.print("i "); }
12. void go(Number n) { System.out.print("N "); }
13. }

What is the result?

A. i L
B. i N
C. S L
D. S N
E. Compilation fails.
F. An exception is thrown at runtime.


DOWNLOAD the answer

Sunday, September 25, 2011

Quiz of the day | OCPJP (20)

Given:

2. public class X {
3. private int g = 3;
4. public static void main(String[] args) {
5. new X().xii();
6. }
7. void xii() {
8. int x = 6;
9. Y y = new Y();
10. class Y {
11. void yii() { System.out.println(g + x); }
12. }
13. y.yii();
14. }
15. }

What is the result? (Choose all that apply.)

A. Compilation succeeds.
B. Compilation fails due to an error on line 3.
C. Compilation fails due to an error on line 8.
D. Compilation fails due to an error on line 9.
E. Compilation fails due to an error on line 10.
F. Compilation fails due to accessing x on line 11.
G. Compilation fails due to accessing y on line 11.


DOWNLOAD the answer

Saturday, September 24, 2011

Quiz of the day | OCPJP (19)

Given:

3. public class O {
4. private int a = 2;
5. protected int s = 3;
6. private static int d = 4;
7. protected static int f = 5;
8. public static void main(String[] args) {
9. int a = 6; int s = 7;
10. int d = 8; int f = 9;
11. new O().new P().p();
12. }
13. class P {
14. void p() { System.out.println(a + " " + s + " " + d + " " + f); }
15. } }

What is the result?

A. 2 3 4 5
B. 2 7 4 9
C. 6 3 8 4
D. 6 7 8 9
E. Compilation fails due to multiple errors.
F. Compilation fails due only to an error on line 11.
G. Compilation fails due only to an error on line 14.


DOWNLOAD the answer

Friday, September 23, 2011

Quiz of the day | OCPJP (18)

Given:

2. interface A { void a(); }
3. // insert code here

Which code, inserted independently at line 3, will compile? (Choose

all that apply.)

A. interface B extends A { }
B. interface B extends A { void a(); }
C. interface B extends A { void doA(); }
D. interface B extends A { void a() { ; } }
E. interface B extends A { protected void a(); }
F. interface B extends A { void a(); void doA(); }


DOWNLOAD the answer

Thursday, September 22, 2011

Quiz of the day | OCPJP (17)

Which are valid command-line switches when working with assertions? (Choose all that apply.)

A. -ea
B. -da
C. -dsa
D. -eva
E. -enableassertions


DOWNLOAD the answer

Wednesday, September 21, 2011

Quiz of the day | OCPJP (16)

Given:

2. import java.text.*;
3. import java.util.*;
4. public class DF {
5. public static void main(String[] args) {
6. DateFormat df1 = DateFormat.getInstance();
7. DateFormat df2 = DateFormat.getInstance(DateFormat.SHORT);
8. DateFormat df3 = DateFormat.getDateInstance(DateFormat.FULL);
9. DateFormat df4 = DateFormat.getDateInstance(DateFormat.EXTENDED);
10. } }

Which are true? (Choose all that apply.)

A. Line 2 is not necessary.
B. Line 3 is not necessary.
C. Compilation fails due to an error on line 6.
D. Compilation fails due to an error on line 7.
E. Compilation fails due to an error on line 8.
F. Compilation fails due to an error on line 9.


DOWNLOAD the answer

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

Thursday, September 15, 2011

Quiz of the day | OCPJP (10)

2. interface A { }
3. interface B { }
4. abstract interface C extends A, B {
5. void c();
6. }
7. class D implements C {
8. public void c() { System.out.print("c "); }
9. }
10. class E implements C extends D {
11. public void c() { System.out.print("c x "); }
12. }
13. public class F extends D implements C, B { }

What is the result? (Choose all that apply.)

A. Compilation succeeds.
B. Compilation fails because of error(s) in C.
C. Compilation fails because of error(s) in D.
D. Compilation fails because of error(s) in E.
E. Compilation fails because of error(s) in F.


DOWNLOAD the answer

Wednesday, September 14, 2011

Quiz of the day | OCPJP (9)

Which are true about a method-local inner class? (Choose all that apply.)

A. It must be marked final
B. It can be marked abstract
C. It can be marked public
D. It can be marked static
E. It can access private members of the enclosing class

DOWNLOAD the answer

Tuesday, September 13, 2011

Quiz of the day | OCPJP (8)

Given:

1. public class MyOffice {
2. public static void main(String[] args) {
3. String name = "PT";
4. name.concat("Time");
5. name = go(name);
6. System.out.println(name);
7. }
8. static String go(String name) {
9. name.concat("Excelindo");
10. return name;
11. } }

What is the result?
A. Time
B. PT
C. TimeExcelindo
D. PTTime
E. PTExcelindo
F. PTTimeExcelindo
G. Compilation fails.


DOWNLOAD the answer

Monday, September 12, 2011

Quiz of the day | OCPJP (7)

Given:

public class TimeExcelindo extends Thread {
public static void main(String[] args) {
Thread t = new Thread(new TimeExcelindo());
Thread t2 = new Thread(new TimeExcelindo());
t.start();
t2.start();
}
public static void run() {
for(int i = 0; i < 2; i++)
System.out.print(Thread.currentThread().getName() + " ");
}
}

Which are true? (Choose all that apply.)

A. Compilation fails.
B. No output is produced.
C. The output could be Thread-1 Thread-3 Thread-1 Thread-2
D. The output could be Thread-1 Thread-3 Thread-1 Thread-3
E. The output could be Thread-1 Thread-1 Thread-2 Thread-2
F. The output could be Thread-1 Thread-3 Thread-3 Thread-1
G. The output could be Thread-1 Thread-3 Thread-1 Thread-1


DOWNLOAD the answer

Sunday, September 11, 2011

Quiz of the day | OCPJP (6)

Given:

interface Beautiful { public void makeUp(); }

Which will compile? (Choose all that apply.)

A. public class Girl implements Beautiful { public void makeUp() { } }

B. public class Girl implements Beautiful { public void makeUp(int x) { } }

C. public class Girl implements Beautiful {
public void makeUp() { System.out.println("The girl is beautiful..."); }
}
D. public abstract class Girl implements Beautiful {
public void makeUp(int x) { }
}
E. public abstract class Girl implements Beautiful {
public void makeUp(int x) ;
}


DOWNLOAD the answer

Saturday, September 10, 2011

Quiz of the day | OCPJP (5)

Given:

class Ca { }
class Cb { }
interface Ia { }
interface Ib { }
class Aa extends Cb implements Ia { }
class Ab implements Ia implements Ib { }
class Ba implements Ca { }
class Bb extends Ca, implements Ib { }
class Ca extends Ia, Ib { }
class Cb implements Ia, Ib { }

What is the result? (Choose all that apply.)

A. Class Aa does not compile.
B. Class Ab does not compile.
C. Class Ba does not compile.
D. Class Bb does not compile.
E. Class Ca does not compile.
F. Class Cb does not compile.
G. Compilation succeeds for all of the classes.


DOWNLOAD the answer

Friday, September 9, 2011

Quiz of the day | OCPJP (4)

Given:

2. class A { }
3. public class B extends A {
4. public static void main(String[] args) {
5. A a = new A();
6. B b = new B();
7. // insert code here
8. }
9. }

Which, inserted independently at line 7, compile? (Choose all that apply.)

A. if(b instanceof a) System.out.print("a ");
B. if(b.instanceof(a)) System.out.print("b ");
C. if(b instanceof A) System.out.print("c ");
D. if(b instanceOf A) System.out.print("d ");
E. if(b.instanceof(A)) System.out.print("e ");


DOWNLOAD the answer

Thursday, September 8, 2011

Quiz of the day | OCPJP (3)

2. public class P {
3. private int p = 1;
4. public static void main(String[] args) {
5. protected int p = 3;
6. new P().new Q().q();
7. }
8. class Q {
9. void q() { System.out.println("number " + p); }
10. }
11. }

Which are true? (Choose all that apply.)
A. Compilation succeeds.
B. The output is "number 1".
C. The output is "number 3".
D. Compilation fails due to an error on line 5.
E. Compilation fails due to an error on line 6.
F. Compilation fails due to an error on line 9.


DOWNLOAD the answer

Wednesday, September 7, 2011

Quiz of the day | OCPJP (2)

Given:

2. public class X {
3. public static void main(String[] args) {
4. String result = "";
5. int x = 4, y = 5;
6. if(x == 0) { result += "5"; }
7. else if (x > 6) { result += "6"; }
8. else if (y < 6) { result += "7"; }
9. else if (x == 4) { result += "8"; }
10. else { result += "9"; }
11. System.out.println(result);
12. }
13. }

What is the result? (Choose all that apply.)

A. 789
B. 7
C. 78
D. 79
E. Compilation fails due to an error on line 5.
F. Compilation fails due to errors on lines 8 and 9.
G. Compilation fails due to errors on lines 7, 8, and 9.


DOWNLOAD the answer

Tuesday, September 6, 2011

Quiz of the day | OCPJP (1)

Given:

public class A extends B {
A() {
System.out.println("a ");
}
public static void main(String[] args) {
new A();
}
}
class B extends C {
B() {
System.out.print("b ");
}
private B(String b) {
}
}
abstract class C {
C() {
System.out.print("c ");
}
}

What is the result?

A. a
B. a b
C. b a
D. a b c
E. c b a
F. Compilation fails due to a single error in the code.
G. Compilation fails due to multiple errors in the code.

DOWNLOAD the answer