Sunteți pe pagina 1din 12

Q1. Which declare a compilable abstract class? (Choose all that apply.) A.

public abstract class Canine { public Bark speak(); } B. public abstract class Canine { public Bark speak() { } } C. public class Canine { public abstract Bark speak(); } D. public class Canine abstract { public abstract Bark speak(); } Q2. Given the following, 1. interface Base { 2. boolean m1 (); 3. byte m2(short s); 4. } Which code fragments will compile? (Choose all that apply.) A. interface Base2 implements Base { } B. abstract class Class2 extends Base { public boolean m1() { return true; } } C. abstract class Class2 implements Base { } D. abstract class Class2 implements Base { public boolean m1() { return (true); } } E. class Class2 implements Base { boolean m1() { return false; } byte m2(short s) { return 42; } } Q3. Which are valid declarations? (Choose all that apply.) A. int $x; B. int 123; C. int _123; D. int #dim; E. int %percent; F. int *divide; G. int central_sales_region_Summer_2005_gross_sales;

Q4. Given: 1. class Voop { 2. public static void main(String [] args) { 3. doStuff(1); 4. doStuff(1,2); 5. } 6. // insert code here 7. } Which, inserted independently at line 6, will compile? (Choose all that apply.) A. static void doStuff(int... doArgs) { } B. static void doStuff(int[] doArgs) { } C. static void doStuff(int doArgs...) { } D. static void doStuff(int... doArgs, int y) { } E. static void doStuff(int x, int... doArgs) { } Q5. Which statement(s) are true? (Choose all that apply.) A. Has-a relationships always rely on inheritance. B. Has-a relationships always rely on instance variables. C. Has-a relationships always require at least two class types. D. Has-a relationships always rely on polymorphis Q6. Given: class Clidders { public final void flipper() { System.out.println("Clidder"); } } public class Clidlets extends Clidders { public void flipper() { System.out.println("Flip a Clidlet"); super.flipper(); } public static void main(String [] args) { new Clidlets().flipper(); } } What is the result? A. Flip a Clidlet B. Flip a Clidder C. Flip a Clidder Flip a Clidlet D. Flip a Clidlet Flip a Clidder E. Compilation fails.

Q7: Given: public abstract interface Frobnicate { public void twiddle(String s); } Which is a correct class? (Choose all that apply.) A. public abstract class Frob implements Frobnicate { public abstract void twiddle(String s) { } } B. public abstract class Frob implements Frobnicate { } C. public class Frob extends Frobnicate { public void twiddle(Integer i) { } } D. public class Frob implements Frobnicate { public void twiddle(Integer i) { } } E. public class Frob implements Frobnicate { public void twiddle(String i) { } public void twiddle(Integer s) { } } Q8. Given: class Top { public Top(String s) { System.out.print("B"); } } public class Bottom2 extends Top { public Bottom2(String s) { System.out.print("D"); } public static void main(String [] args) { Self Test Answers 165 new Bottom2("C"); System.out.println(" "); }} What is the result? A. BD B. DB C. BDC D. DBC E. Compilation fails

Q9. Using the fragments below, complete the following code so it compiles. Note, you may not have to fill all of the slots. Code: class AgedP { __________ __________ __________ __________ __________ public AgedP(int x) { __________ __________ __________ __________ __________ } } public class Kinder extends AgedP { __________ __________ __________ _________ ________ __________ public Kinder(int x) { __________ __________ __________ __________ __________ (); } } Fragments: Use the following fragments zero or more times: AgedP, super, this,(, ), {, },; Q10. Given: 1. class Plant { 2. String getName() { return "plant"; } 3. Plant getType() { return this; } 4. } 5. class Flower extends Plant { 6. // insert code here 7. } 8. class Tulip extends Flower { } Which statement(s), inserted at line 6, will compile? (Choose all that apply.) A. Flower getType() { return this; } B. String getType() { return "this"; } C. Plant getType() { return this; } D. Tulip getType() { return new Tulip(); }

Q11. Given: 1. class Plant { 2. String getName() { return "plant"; } 3. Plant getType() { return this; } 4. } 5. class Flower extends Plant { 6. // insert code here 7. } 8. class Tulip extends Flower { } Which statement(s), inserted at line 6, will compile? (Choose all that apply.) A. Flower getType() { return this; } B. String getType() { return "this"; } C. Plant getType() { return this; } D. Tulip getType() { return new Tulip(); } Q12. Given: 1. class Programmer { 2. Programmer debug() { return this; } 3. } 4. class SCJP extends Programmer { 5. // insert code here 6. } Which, inserted at line 5, will compile? (Choose all that apply.) A. Programmer debug() { return this; } B. SCJP debug() { return this; } C. Object debug() { return this; } D. int debug() { return 1; } E. int debug(int x) { return 1; } F. Object debug(int x) { return this; } Q13. Given: class Uber { static int y = 2; Uber(int x) { this(); y = y * 2; } Uber() { y++; } } class Minor extends Uber { Minor() { super(y); y = y + 3; } public static void main(String [] args) { new Minor(); System.out.println(y); }} What is the result? A. 6

B. 7 C. 8 D. 9 E. Compilation fails. Q14. Given the following, 1. class X { void do1() { } } 2. class Y extends X { void do2() { } } 3. 4. class Chrome { 5. public static void main(String [] args) { 6. X x1 = new X(); 7. X x2 = new Y(); 8. Y y1 = new Y(); 9. // insert code here 10. } 11. } Which, inserted at line 9, will compile? (Choose all that apply.) A. x2.do2(); B. (Y)x2.do2(); C. ((Y)x2).do2(); D. None of the above statements will compile. Q15. Given: class Bird { { System.out.print("b1 "); } public Bird() { System.out.print("b2 "); } } class Raptor extends Bird { static { System.out.print("r1 "); } public Raptor() { System.out.print("r2 "); } { System.out.print("r3 "); } static { System.out.print("r4 "); } } class Hawk extends Raptor { public static void main(String[] args) { System.out.print("pre "); new Hawk(); System.out.println("hawk "); }} What is the result? A. pre b1 b2 r3 r2 hawk B. pre b2 b1 r2 r3 hawk C. pre b2 b1 r2 r3 hawk r1 r4 D. r1 r4 pre b1 b2 r3 r2 hawk

E. r1 r4 pre b2 b1 r2 r3 hawk F. pre r1 r4 b1 b2 r3 r2 hawk G. pre r1 r4 b2 b1 r2 r3 hawk H. The order of output cannot be predicted. I. Compilation fails. Q16. Given: 1. class Eco { 2. public static void main(String[] args) { 3. Eco e1 = new Eco(); 4. Eco e2 = new Eco(); 5. Eco e3 = new Eco(); 6. e3.e = e2; 7. e1.e = e3; 8. e2.e = e1; 9. e2 = null; 9. e3 = null; 10.e1 = null; 11. } 12. Eco e; } At what point is only a single object eligible for GC? A. After line 8 runs. B. After line 9 runs. C. After line 10 runs. D. After line 11 runs. E. Compilation fails. F. Never in this program. G. An exception is thrown at runtime. Q17. Given: 1. class Convert { 2. public static void main(String[] args) { 3. Long xL = new Long(456L); 4. long x1 = Long.valueOf("123"); 5. Long x2 = Long.valueOf("123"); 6. long x3 = xL.longValue(); 7. Long x4 = xL.longValue(); 8. Long x5 = Long.parseLong("456"); 9. long x6 = Long.parseLong("123"); 10. } 11. } Which will compile using Java 5, but will NOT compile using Java 1.4? (Choose all that apply.) A. Line 4. B. Line 5.

C. Line 6. D. Line 7. E. Line 8. F. Line 9. Q18. Which is true? (Choose all that apply.) A. The invocation of an objects finalize() method is always the last thing that happens before an object is garbage collected (GCed). B. When a stack variable goes out of scope it is eligible for GC. C. Some reference variables live on the stack, and some live on the heap. D. Only objects that have no reference variables referring to them can be eligible for GC. E. Its possible to request the GC via methods in either java.lang.Runtime or java.lang.System classes. Q19. Given: class Mixer { Mixer() { } Mixer(Mixer m) { m1 = m; } Mixer m1; public static void main(String[] args) { Mixer m2 = new Mixer(); Mixer m3 = new Mixer(m2); m3.go(); Mixer m4 = m3.m1; m4.go(); Mixer m5 = m2.m1; m5.go(); } void go() { System.out.print("hi "); }} What is the result? A. hi B. hi hi C. hi hi hi D. Compilation fails E. hi, followed by an exception F. hi hi, followed by an exception

Q20. Given: class Sixties { public static void main(String[] args) { int x = 5; int y = 7; System.out.print(((y * 2) % x)); System.out.print(" " + (y % x));}} What is the result? A. 1 1 B. 1 2 C. 2 1 D. 2 2 E. 4 1 F. 4 2 G. Compilation fails. Q21. Given: class Fork { public static void main(String[] args) { if(args.length == 1 | args[1].equals("test")) { System.out.println("test case"); } else { System.out.println("production " + args[0]); } } } And the command-line invocation: java Fork live2 What is the result? A. test case B. production C. test case live2 D. Compilation fails. E. An exception is thrown at runtime.

Q22. Given: class Hexy { public static void main(String[] args) { Integer i = 42; String s = (i<40)?"life":(i>50)?"universe":"everything"; System.out.println(s); } } What is the result? A. null B. life C. universe D. everything E. Compilation fails. F. An exception is thrown at runtime. Q23. Which are true about an anonymous inner class? (Choose all that apply.) A. It can extend exactly one class and implement exactly one interface. B. It can extend exactly one class and can implement multiple interfaces. C. It can extend exactly one class or implement exactly one interface. D. It can implement multiple interfaces regardless of whether it also extends a class. E. It can implement multiple interfaces if it does not extend a class. Q24. Given: public class Foo { Foo() {System.out.print("foo");} class Bar{ Bar() {System.out.print("bar");} public void go() {System.out.print("hi");} } public static void main(String[] args) { Foo f = new Foo(); f.makeBar(); } void makeBar() { (new Bar() {}).go();}} What is the result? A. Compilation fails. B. An error occurs at runtime. C. foobarhi D. barhi E. hi F. foohi

Q25. 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. Q26. Given: 1. class Foo { 2. class Bar{ } 3. } 4. class Test { 5. public static void main(String[] args) { 6. Foo f = new Foo(); 7. // Insert code here 8. }} Which, inserted at line 7, creates an instance of Bar? (Choose all that apply.) A. Foo.Bar b = new Foo.Bar(); B. Foo.Bar b = f.new Bar(); C. Bar b = new f.Bar(); D. Bar b = f.new Bar(); E. Foo.Bar b = new f.Bar(); Q27. Given: public interface Runnable { void run(); } Which construct an anonymous inner class instance? (Choose all that apply.) A. Runnable r = new Runnable() { }; B. Runnable r = new Runnable(public void run() { }); C. Runnable r = new Runnable { public void run(){}}; D. Runnable r = new Runnable() {public void run{}}; E. System.out.println(new Runnable() {public void run() { }}); F. System.out.println(new Runnable(public void run() {})); Q28. Given: 1. class Loopy { 2. public static void main(String[] args) { 3. int[] x = {7,6,5,4,3,2,1}; 4. // insert code here 5. System.out.print(y + " "); 6. } 7. } } Which, inserted independently at line 4, compiles? (Choose all that apply.)

A. for(int y : x) { B. for(x : int y) { C. int y = 0; for(y : x) { D. for(int y=0, z=0; z<x.length; z++) { y = x[z]; E. for(int y=0, int z=0; z<x.length; z++) { y = x[z]; F. int y = 0; for(int z=0; z<x.length; z++) { y = x[z]; Q29.Which of the following statements are true in context of wrapper ? A. Void is a wrapper class. B. All wrapper classes have the valueOf() methods. C. String is a wrapper class D. The objects of the wrapper classes are immutable. Q30. Which of the following statements are representing the valid method declaration? A. void myMethod{....} B. void myMethod(){..} C. void myMethod(void){...} D. myMethod{....}

S-ar putea să vă placă și