Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java Test

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

1. Which is true? (Choose all that apply.) A.

"X extends Y" is correct if and only if X is a class and Y is an interface B. "X extends Y" is correct if and only if X is an interface and Y is a class C. "X extends Y" is correct if X and Y are either both classes or both interfaces D. "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces 2. Which method names follow the JavaBeans standard? (Choose all that apply.) A. addSize B. getCust C. deleteRep D. isColorado E. putDimensions 3. Given:
1. class Voop { 2. 3. 4. 5. 6. 7. } } // insert code here public static void main(String [] args) { doStuff(1); doStuff(1,2);

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) { } 4. Given:
1. enum Animals { 2. 3. 4. 5. } 6. class TestEnum { 7. 8. 9. 10. } static Animals a; public static void main(String[] args) { System.out.println(a.DOG.sound + " " + a.FISH.sound); DOG("woof"), CAT("meow"), FISH("burble"); String sound; Animals(String s) { sound = s; }

11. }

What is the result? A. woof burble B. Multiple compilation errors C. Compilation fails due to an error on line 2 D. Compilation fails due to an error on line 3 E. Compilation fails due to an error on line 4 F. Compilation fails due to an error on line 9 5. Given two files:
1. package pkgA; 2. public class Foo { 3. 4. 5. 6. } int a = 5; protected int b = 6; public int c = 7;

1. package pkgB; 2. import pkgA.*; 3. public class Baz { 4. 5. 6. 7. 8. 9. 10. } } public static void main(String[] args) { Foo f = new Foo(); System.out.print(" " + f.a); System.out.print(" " + f.b); System.out.println(" " + f.c);

What is the result? (Choose all that apply.) A. 5 6 7 B. 5 followed by an exception C. Compilation fails with an error on line 7 D. Compilation fails with an error on line 8 E. Compilation fails with an error on line 9 F. Compilation fails with an error on line 10 6. Given:
1. public class Electronic implements Device { public void doIt() { } }

2. 3. abstract class Phone1 extends Electronic { } 4. 5. abstract class Phone2 extends Electronic { public void doIt(int x) { } } 6. 7. class Phone3 extends Electronic implements Device { public void doStuff() { } } 8. 9. interface Device { public void doIt(); }

What is the result? (Choose all that apply.) A. Compilation succeeds B. Compilation fails with an error on line 1 C. Compilation fails with an error on line 3 D. Compilation fails with an error on line 5 E. Compilation fails with an error on line 7 F. Compilation fails with an error on line 9 7. Given:
4. class Announce { 5. 6. 7. 8. 9. 10. 11. 12. } } public static void main(String[] args) { for(int __x = 0; __x < 3; __x++) ; int #lb = 7; long [] x [5]; Boolean []ba[]; enum Traffic { RED, YELLOW, GREEN };

What is the result? (Choose all that apply.) A. Compilation succeeds B. Compilation fails with an error on line 6 C. Compilation fails with an error on line 7 D. Compilation fails with an error on line 8 E. Compilation fails with an error on line 9 F. Compilation fails with an error on line 10 8. Given:
3. public class TestDays {

4. 5. 6. 7. 8. 9. 10. 11. }

public enum Days { MON, TUE, WED }; public static void main(String[] args) { for(Days d : Days.values() ) ; Days [] d2 = Days.values(); System.out.println(d2[2]); }

What is the result? (Choose all that apply.) A. TUE B. WED C. The output is unpredictable D. Compilation fails due to an error on line 4 E. Compilation fails due to an error on line 6 F. Compilation fails due to an error on line 8 G. Compilation fails due to an error on line 9 9. Given:
4. public class Frodo extends Hobbit { 5. 6. 7. 8. 9. } 10. class Hobbit { 11. 12. } int countGold(int x, int y) { return x + y; } } public static void main(String[] args) { Short myGold = 7; System.out.println(countGold(myGold, 6));

What is the result? A. 13 B. Compilation fails due to multiple errors 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 11 10. 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) { } }

11. 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) { new Bottom2("C"); System.out.println(" "); } }

What is the result? A. BD B. DB C. BDC D. DBC E. Compilation fails 12. Given:


class Clidder { private final void flipper() { System.out.println("Clidder"); } } public class Clidlet extends Clidder { public final void flipper() { System.out.println("Clidlet"); }

public static void main(String [] args) { new Clidlet().flipper(); } }

What is the result? A. Clidlet B. Clidder C. Clidder


Clidlet

D. Clidlet
Clidder

E. Compilation fails 13. Given the following,


1. class X { void do1() { } } 2. class Y extends X { void do2() { } } 3. 4. class Chrome { 5. 6. 7. 8. 9. 10. } } public static void main(String [] args) { X x1 = new X(); X x2 = new Y(); Y y1 = new Y(); // insert code here

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 14. Given: 1. ClassA has a ClassD 2. Methods in ClassA use public methods in ClassB 3. Methods in ClassC use public methods in ClassA 4. Methods in ClassA use public variables in ClassB Which is most likely true? (Choose the most likely.) A. ClassD has low cohesion B. ClassA has weak encapsulation

C. ClassB has weak encapsulation D. ClassB has strong encapsulation E. ClassC is tightly coupled to ClassA 15. Given:
3. class Dog { 4. 5. } 6. class Hound extends Dog { 7. 8. 9. } 10. public class DogShow { 11. 12. 13. 14. 15. 16. 17. } } public static void main(String[] args) { new DogShow().go(); } void go() { new Hound().bark(); ((Dog) new Hound()).bark(); ((Dog) new Hound()).sniff(); public void sniff() { System.out.print("sniff "); } public void bark() { System.out.print("howl "); } public void bark() { System.out.print("woof "); }

What is the result? (Choose all that apply.) A. howl howl sniff B. howl woof sniff C. howl howl followed by an exception D. howl woof followed by an exception E. Compilation fails with an error at line 14 F. Compilation fails with an error at line 15
16. Given:
3. public class Redwood extends Tree { 4. 5. 6. 7. 8. 9. 10. 11. } void go2(Tree t1, Redwood r1) { } void go() { go2(new Tree(), new Redwood()); go2((Redwood) new Tree(), new Redwood()); public static void main(String[] args) { new Redwood().go();

12. 13. 14. 15. } }

Redwood r2 = (Redwood)t1; Tree t2 = (Tree)r1;

16. class Tree { }

What is the result? (Choose all that apply.) A. An exception is thrown at runtime B. The code compiles and runs with no output C. Compilation fails with an error at line 8 D. Compilation fails with an error at line 9 E. Compilation fails with an error at line 12 F. Compilation fails with an error at line 13 17. Given:
3. public class Tenor extends Singer { 4. 5. 6. 7. 8. 9. 10. } 18. class Singer { public static String sing() { return "la"; } } } public static String sing() { return "fa"; } public static void main(String[] args) { Tenor t = new Tenor(); Singer s = new Tenor(); System.out.println(t.sing() + " " + s.sing());

What is the result? A. fa fa B. fa la C. la la D. Compilation fails E. An exception is thrown at runtime 19. Given:
3. class Alpha { 4. 5. 6. } 7. class SubAlpha extends Alpha { 8. 9. } private SubAlpha() { s += "sub "; } static String s = " "; protected Alpha() { s += "alpha "; }

10. public class SubSubAlpha extends Alpha { 11. 12. 13. 14. 15. 16. } } private SubSubAlpha() { s += "subsub "; } public static void main(String[] args) { new SubSubAlpha(); System.out.println(s);

What is the result? A. subsub B. sub subsub C. alpha subsub D. alpha sub subsub E. Compilation fails F. An exception is thrown at runtime 20. Given:
3. class Building { 4. 5. 6. 7. 8. } 9. public class House extends Building { 10. 11. 12. 13. } 14. public static void main(String[] args) { new House("x "); } 15. } House() { System.out.print("h "); } House(String name) { this(); System.out.print("hn " + name); } Building() { System.out.print("b "); } Building(String name) { this(); System.out.print("bn " + name);

What is the result? A. h hn x B. hn x h C. b h hn x D. b hn x h E. bn x h hn x F. b bn x h hn x G. bn x b h hn x

H. Compilation fails 21. Given:


3. class Mammal { 4. 5. 6. } 7. class Zebra extends Mammal { 8. 9. 10. } 11. public class ZooKeeper { 12. 13. 14. 15. 16. 17. } } public static void main(String[] args) { new ZooKeeper().go(); } void go() { Mammal m = new Zebra(); System.out.println(m.name + m.makeNoise()); String name = "stripes "; String makeNoise() { return "bray"; } String name = "furry "; String makeNoise() { return "generic noise"; }

What is the result? A. furry bray B. stripes bray C. furry generic noise D. stripes generic noise E. Compilation fails F. An exception is thrown at runtime 21. Youre designing a new online board game in which Floozels are a type of Jammers, Jammers can have Quizels, Quizels are a type of Klakker, and Floozels can have several Floozets. Which of the following fragments represent this design? (Choose all that apply.) A. import java.util.*;
interface Klakker { } class Jammer { Set<Quizel> q; } class Quizel implements Klakker { } public class Floozel extends Jammer { List<Floozet> f; } interface Floozet { }

B. import java.util.*;
class Klakker { Set<Quizel> q; } class Quizel extends Klakker { }

class Jammer { List<Floozel> f; } class Floozet extends Floozel { } public class Floozel { Set<Klakker> k; }

C. import java.util.*;
class Floozet { } class Quizel implements Klakker { } class Jammer { List<Quizel> q; } interface Klakker { } class Floozel extends Jammer { List<Floozet> f; }

D. import java.util.*;
interface Jammer extends Quizel { } interface Klakker { } interface Quizel extends Klakker { } interface Floozel extends Jammer, Floozet { } interface Floozet { }

22. Given:
3. class A { } 4. class B extends A { } 5. public class ComingThru { 6. 7. 8. 9. 10. 11. 12. 13. 14. } 15. static void sifter(A[]... a2) { s += "1"; } 16. static void sifter(B[]... b1) { s += "2"; } 17. static void sifter(B[] b1) { s += "3"; } 18. static void sifter(Object o) { s += "4"; } 19. } static String s = "-"; public static void main(String[] args) { A[] aa = new A[2]; B[] ba = new B[2]; sifter(aa); sifter(ba); sifter(7); System.out.println(s);

What is the result? A. -124 B. -134

C. -424 D. -434 E. -444 F. Compilation fails 1. Given:


class CardBoard { Short story = 200; CardBoard go(CardBoard cb) { cb = null; return cb; } public static void main(String[] args) { CardBoard c1 = new CardBoard(); CardBoard c2 = new CardBoard(); CardBoard c3 = c1.go(c2); c1 = null; // do Stuff } }

When // doStuff is reached, how many objects are eligible for GC? A. 0 B. 1 C. 2 D. Compilation fails E. It is not possible to know F. An exception is thrown at runtime 2. Given:
class Alien { String invade(short ships) { return "a few"; } String invade(short... ships) { return "many"; } } class Defender { public static void main(String [] args) { System.out.println(new Alien().invade(7)); } }

What is the result?

A. many B. a few C. Compilation fails D. The output is not predictable E. An exception is thrown at runtime
Self Test 269

3. Given:
1. class Dims { 2. public static void main(String[] args) { 3. int[][] a = {{1,2,}, {3,4}}; 4. int[] b = (int[]) a[1]; 5. Object o1 = a; 6. int[][] a2 = (int[][]) o1; 7. int[] b2 = (int[]) o1; 8. System.out.println(b[1]); 9. } }

What is the result? A. 2 B. 4 C. An exception is thrown at runtime D. Compilation fails due to an error on line 4 E. Compilation fails due to an error on line 5 F. Compilation fails due to an error on line 6 G. Compilation fails due to an error on line 7 4. 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

270 Chapter 3: Assignments


D. Compilation fails E. hi, followed by an exception F. hi hi, followed by an exception 5. Given:
class Fizz { int x = 5; public static void main(String[] args) { final Fizz f1 = new Fizz(); Fizz f2 = new Fizz(); Fizz f3 = FizzSwitch(f1,f2); System.out.println((f1 == f3) + " " + (f1.x == f3.x)); } static Fizz FizzSwitch(Fizz x, Fizz y) { final Fizz z = x; z.x = 6; return z; } }

What is the result? A. true true B. false true C. true false D. false false E. Compilation fails F. An exception is thrown at runtime 6. 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 "); } }

Self Test 271

272 Chapter 3: Assignments


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 7. Given:
3. public class Bridge { 4. public enum Suits { 5. CLUBS(20), DIAMONDS(20), HEARTS(30), SPADES(30), 6. NOTRUMP(40) { public int getValue(int bid) { return ((bid-1)*30)+40; } }; 7. Suits(int points) { this.points = points; } 8. private int points; 9. public int getValue(int bid) { return points * bid; } 10. } 11. public static void main(String[] args) { 12. System.out.println(Suits.NOTRUMP.getValue(3));

13. System.out.println(Suits.SPADES + " " + Suits.SPADES.points); 14. System.out.println(Suits.values()); 15. } 16. }

Which are true? (Choose all that apply.) A. The output could contain 30 B. The output could contain @bf73fa C. The output could contain DIAMONDS D. Compilation fails due to an error on line 6 E. Compilation fails due to an error on line 7 F. Compilation fails due to an error on line 8 G. Compilation fails due to an error on line 9 H. Compilation fails due to an error within lines 12 to 14 8. Given:
3. public class Ouch { 4. static int ouch = 7; 5. public static void main(String[] args) { 6. new Ouch().go(ouch); 7. System.out.print(" " + ouch); 8. } 9. void go(int ouch) { 10. ouch++; 11. for(int ouch = 3; ouch < 6; ouch++) 12. ; 13. System.out.print(" " + ouch); 14. } 15. }

What is the result? A. 5 7 B. 5 8 C. 8 7 D. 8 8 E. Compilation fails F. An exception is thrown at runtime 9. Given:


3. public class Bertha {

4. static String s = ""; 5. public static void main(String[] args) { 6. int x = 4; Boolean y = true; short[] sa = {1,2,3}; 7. doStuff(x, y); 8. doStuff(x); 9. doStuff(sa, sa); 10. System.out.println(s); 11. } 12. static void doStuff(Object o) { s += "1"; } 13. static void doStuff(Object... o) { s += "2"; } 14. static void doStuff(Integer... i) { s += "3"; } 15. static void doStuff(Long L) { s += "4"; } 16. }

Self Test 273

274 Chapter 3: Assignments


What is the result? A. 212 B. 232 C. 234 D. 312 E. 332 F. 334 G. Compilation fails 10. Given:
3. class Dozens { 4. int[] dz = {1,2,3,4,5,6,7,8,9,10,11,12}; 5. } 6. public class Eggs { 7. public static void main(String[] args) { 8. Dozens [] da = new Dozens[3]; 9. da[0] = new Dozens(); 10. Dozens d = new Dozens(); 11. da[1] = d; 12. d = null; 13. da[1] = null; 14. // do stuff

15. } 16. }

Which two are true about the objects created within main(), and eligible for garbage collection when line 14 is reached? A. Three objects were created B. Four objects were created C. Five objects were created D. Zero objects are eligible for GC E. One object is eligible for GC F. Two objects are eligible for GC G. Three objects are eligible for GC 11. Given:
3. class Beta { } 4. class Alpha { 5. static Beta b1; 6. Beta b2; 7. } 8. public class Tester { 9. public static void main(String[] args) { 10. Beta b1 = new Beta(); Beta b2 = new Beta(); 11. Alpha a1 = new Alpha(); Alpha a2 = new Alpha(); 12. a1.b1 = b1; 13. a1.b2 = b1; 14. a2.b2 = b2; 15. a1 = null; b1 = null; b2 = null; 16. // do stuff 17. } 18. }

When line 16 is reached, how many objects will be eligible for garbage collection? A. 0 B. 1 C. 2 D. 3 E. 4 F. 5

12. Given:
3. class Box { 4. int size; 5. Box(int s) { size = s; } 6. } 7. public class Laser { 8. public static void main(String[] args) { 9. Box b1 = new Box(5); 10. Box[] ba = go(b1, new Box(6)); 11. ba[0] = b1; 12. for(Box b : ba) System.out.print(b.size + " "); 13. } 14. static Box[] go(Box b1, Box b2) { 15. b1.size = 4; 16. Box[] ma = {b2, b1}; 17. return ma; 18. } 19. }

What is the result? A. 4 4 B. 5 4 C. 6 4 D. 4 5 E. 5 5 F. Compilation fails 13. Given:


3. public class Dark { 4. int x = 3; 5. public static void main(String[] args) { 6. new Dark().go1(); 7. } 8. void go1() { 9. int x; 10. go2(++x); 11. } 12. void go2(int y) {

13. int x = ++y; 14. System.out.println(x); 15. } 16. }

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

You might also like