当前位置:文档之家› JAVA复习资料

JAVA复习资料

第一章1.Given the following,1. interface Base {2. boolean m1 ();3. byte m2(short s);4. }Which code fragments willcompile?(Choose all that apply.)A.interface Base2 implements Base { }B.abstract class Class2 extends Base { publicboolean ml() { return true; } }C.abstract class Class2 implements Base { }D.abstract class Class2. implements Base{ public boolean m1() { return (true); } }E.class Class2 implements Base { booleanm1( ) { return false; } byte m2(short s){ return 42; } }2. Which declare a compilable abstract class? (Choose all that apply.)A.public abstract class Canine { public Barkspeak(); }B.public abstract class Canine { public Barkspeak() { } }C.public class Canine { public abstract Barkspeak(); }D.public class Canine abstract{ publicabstract Bark speak(); }3. Which is true? (Choose all that apply. )A."X extends Y" is correct if and only if X isa class and Y is an interface.B."X extends Y" is correct if and only if X isan interface and Y is a class.C."X extends Y" is correct if X and Y areeither both classes or both interfaces.D."X extends Y" is correct for allcombinations of X and Y being classesand/or interfaces.4. 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.intcentral_sales_region_Summer_2005_gross_sales第二章1. Which statement(s) are true? (Choose all that apply.)A.Has-a relationships always rely oninheritance.B.Has-a relationships always rely on instancevariables.C.Has-a relationships always require at leasttwo class types.D.Has-a relationships always rely onpolymorphism.E.Has-a relationships are always tightlycoupled.2.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 ClidletB.Flip a ClidderC.Flip a ClidderFlip a ClidletD.Flip a ClidletFlip a ClidderE.Compilation fails.3. Given:public abstract interface Frobnicate { public void twiddle(String s) ; }Which is a correct class? (Choose all that apply.)A.public abstract class Frob implementsFrobnicate {public abstract void twiddle(String s){}}B.public abstract class Frob implementsFrobnicate { }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) { }}4. 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.BDB.DBC.BDCD.DBCpilation fails.5. Select the two statements that best indicate a situation with low coupling. (Choose two.)A.The attributes of the class are all private.B.The class refers to a small number of otherobjects.C.The object contains only a small number ofvariables.D.The object is referred to using ananonymous variable, not directly.E.The reference variable is declared for aninterface type, not a class. The interfaceprovides a small number of methods.F.It is unlikely that changes made to oneclass will require any changes in another.6. 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.ClidletB.ClidderC.ClidderClidletD.ClidletClidderpilation fails.7. 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( ) { }; 8. Given:1. class Plant {2. String getName() { return "plant"; }3. Plant getType() { return this; }4. }5. class Flower extends Plant {6. // insert code here7. }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() ;}9. Given:1. class Zing {2. protected Hmpf h;3. }4. class Woop extends Zing { }5. class Hmpf { }Which is true? (Choose all that apply.)A.Woop is-a Hmpf and has-a zing.B.zing is-a Woop and has-a Hmpf.C.Hmpf has-a Woop and Woop is-a Zing.D.Woop has-a Hmpf and Woop is-a zing.E.Zing has-a Hmpf and Zing is-a Woop.10. Given:1. class Programmer {2. Programmer debug() { return this; }3. }4. class SCJP extends Programmer {5. // insert code here6. }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; }第四章1. 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.nullB.lifeC.universeD.everythingpilation fails.F.An exception is thrown at runtime.2. Given:1. class Example {2. public static void main(String[] args) {3. Short s = 15;4. Boolean b;5. // insert code here6. }7. }Which, inserted independently at line 5, will compile? (Choose all that apply.)A. b = (Number instanceof s);B. b = (s instanceof Short);C. b = s.instanceof(Short);D. b = (s instanceof Number);E. b = s.instanceof(Object);F. b = (s instanceof String);3. Given:1. class Comp2 {2. public static void main(String[] args) {3. float f1 = 2.3f;4. float[][] f2 = {{42.Of}, {l.7f, 2.3f}, {2.6f, 2.7f}};5. float[] f3 = {2.7f};6. Long x = 42L;7. // insert code here8. System.out.println("true");9. }10. }And the following five code fragments:F1. if (f1 == f2)F2.if (f1 == f2[2][1])F3. if (x == f2[0][0])F4. if (f1 == f2 [1,1] )F5. if (f3 == f2 [2] )What is true?A.One of them will compile, only one will betrue.B.Two of them will compile, only one willbe true.C.Two of them will compile, two will betrue.D.Three of them will compile, only one willbe true.E.Three of them will compile, exactly twowill be true.F.Three of them will compile, exactly threewill be true.4. 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 live2What is the result?A.test caseB.productionC.test case live2pilation fails.E.An exception is thrown at runtime.5. Given:class Foozit {public static void main(String[] args) {Integer x = 0;Integer y = 0;for(Short z = 0; z < 5; z++)if((++x > 2) || (++y > 2))X++ ;System.out.println (x + " " + y);}}What is the result?A. 5 1B. 5 2C. 5 3D.8 1E.8 2F.8 3G.10 2H.10 3第六章4. Given:Class TKO {public static void main(String[] args) {String s = "-";Integer x = 343;long L343 = 343L;if(x.equals(L343)) s += ".e1 ";if(x.equals(343)) s += ".e2 ";Short s1 = (short)((new Short((short)343)) / (new Short((short)49)));if (s1 == 7) s += "=s ";if(sl < new Integer(7+1)) s += "fly ";System.out.println(s);}}Which of the following will be included in the output String s? (Choose all that apply.)A..e1B..e2C.=sD.flyE.None of the above.pilation fails.G.An exception is thrown at runtime.5. Given:1. import java.text.*;2. class DateOne {3. public static void main(String[] args) {4. Date d = new Date(1123631685981L);5. DateFormat df = new DateFormat();6. System.out.println(df.format(d));7. }8. }And given that 1123631685981L is the number of milliseconds between Jan. 1, 1970, and sometime on Aug. 9, 2005, what is the result? (Note: the time of day in option A may vary.)A.8/9/05 5:54 PMB.1123631685981L,C.An exception is thrown at runtime.pilation fails due to a single error inthe code.pilation fails due to multiple errors inthe code.6. Given:import java.io.*;class Keyboard { }public class Computer implements Serializable {private Keyboard k = new Keyboard(); public static void main(String[] args)Computer c = new Computer();c.storeIt(c) ;}void storeIt(Computer c) {try {ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("myFile"));os.writeObject(c);os.close() ;System.out.println("done");} catch (Exception x){System.out.println("exc"); } }}}What is the result? (Choose all that apply.)A.excB.donepilation fails.D.Exactly one object is serialized.E.Exactly two objects are serialized.7. Using the fewest fragments possible (and filling the fewest slots possible), complete the code below so that the class builds a directory named "dir3" and creates a file named "file3" inside "dir3". Note you can use each fragment either zero or one times.Code:import java.io.________________class Maker {public static void main(String [] args) {___________ _________________________________ _________________________________ _________________________________ _________________________________ _________________________________ _________________________________ ______________________}}Fragments:File; FileDescriptor; FileWriter; Directory;try { .createNewDir(); File dirFile{ } (Exception x) ("dir3");filefile .createNewFile(); = new File = new Filedir (dir, "file3"); (dir,file); .createFile() ;} catch ("dir3", "file3"); .mkdir(); File file第七章1. Given:import java.util.*;class Test {public static void main(String[] args) {// insert code herex.add("one");x.add("two");x. add ("TWO");System.out.printIn(x.poll());}}Which, inserted at // insert code here, will compile? (Choose all that apply.)A.List<String> x = newLinkedList<String>();B.TreeSet<String> x = newTreeSet<String>();C.HashSet<String> x = newHashSet<String>();D.Queue<String> x = newPriorityQueue<String>();E.ArrayList<String> x = newArrayList<String>();F.LinkedList<String> x = newLinkedList<String>();2. Given:public static void main(String[] args) {// INSERT DECLARATION HEREfor (int i = 0; i <= 10; i++) {List<Integer> row = newArrayList<Integer>();for (int j = 0; j <= 10; j++)row.add(i * j);table.add(row);}for (List<Integer> row : table)System.out.println(row);}Which statements could be inserted at // INSERT DECLARATION HERE to allow this code to compile and run? (Choose all that apply.)A.List<List<Integer>> table = newList<List<Integer>>();B.List<List<Integer>> table = newArrayList<List<Integer>>();C.List<List<Integer>> table = newArrayList<ArrayList<Integer>>();D.List<List, Integer> table = new List<List,Integer>();E.List<List, Integer> table = newArrayList<List, Integer>();F.List<List, Integer> table = newArrayList<ArrayList, Integer>();G.None of the above.3. Which statements are true about comparing two instances of the same class, given that the equals() and hashCode() methods have been properly overridden? (Choose all that apply.)A.If the equals() method returns true, thehashCode() comparison == might returnfalse.B.If the equals() method returns false, thehashCode() comparison == might returntrue.C.If the hashCode() comparison == returnstrue, the equals() method must return true.D.If the hashCode() comparison == returnstrue, the equals() method might return true.E.If the hashCode() comparison ! = returnstrue, the equals() method might return true.15. Given:12. TreeSet map = new TreeSet();13. map.add("one");14. map.add("two");15. map.add("three");16. map.add("four"};17. map.add("one");18. Iterator it = map.iterator();19. while (it.hasNext() ) {20. System.out.print( it.next() + " " );21. }What is the result?pilation fails.B.one two three fourC.four three two oneD.four one three twoE.one two three four oneF.one four three two oneG.An exception is thrown at runtime.H.The print order is not guaranteed.16. Given a method declared as:public static <E extends Number> List<? super E> process(List<E> nums)A programmer wants to use this method like this: // INSERT DECLARATIONS HEREoutput = process(input);Which pairs of declarations could be placed at // INSERT DECLARATIONS HERE to allow the code to compile? (Choose all that apply.)A.ArrayList<Integer> input = null;ArrayList<Integer> output = null;B.ArrayList<Integer> input = null;List<Integer> output = null;C.ArrayList<Integer> input = null;List<Number> output = null;D.List<Number> input = null;ArrayList<Integer> output = null;E.List<Number> input = null;List<Number> output = null;F.List<Integer> input = null;List<Integer> output = null;G.None of the above.第九章1. Given:public class Messager implements Runnable { public static void main(String[] args) {new Thread(newMessager("Wallace")).start() ;new Thread(newMessager("Gromit")).start();}private String name;public Messager(String name) { = name; }public void run() {message(1);message(2);}private synchronized void message(int n) {System.out.print(name + "-" + n + " ");} }Which of the following is a possible result? (Choose all that apply.)A.Wallace-1 Wallace-2 Gromit-1B.Wallace-1 Gromit-2 Wallace-2 Gromit-1C.Wallace-1 Gromit-1 Gromit-2 Wallace-2D.Gromit-1 Gromit-2E.Gromit-2 Wallace-1 Gromit-1 Wallace-2F.The code does not compile.G.An error occurs at run time.2. Given:public class Letters extends Thread {private String name;public Letters(String name) { = name;}public void write () {System.out.print(name);System.out.print(name);}public static void main(String[] args) {new Letters("X").start();new Letters("Y").start();}}We want to guarantee that the output can be either XXYY or YYXX, but never XYXY or any other combination. Which of the following method definitions could be added to the Letters class to make this guarantee? (Choose all that apply.)A.public void run() { write(); }B.public synchronized void run() { write(); }C.public static synchronized void run(){ write(); }D.public void run() { synchronized(this){ write(); } }E.public void run(){ synchronized(Letters.class) { write(); } }F.public void run () { synchronized(System.out) { write (); } }G.public void run(){ synchronized(System.out.class){ write(); } }3. The following block of code creates a Thread using a Runnable target:Runnable target = new MyRunnable();Thread myThread = new Thread(target);Which of the following classes can be used to create the target, so that the preceding code compiles correctly?A.public class MyRunnable extendsRunnable{public void run(){}}B.public class MyRunnable extendsObject{public void run(){}}C.public class MyRunnable implementsRunnable{public void run(){}}D.public class MyRunnable implementsRunnable{void run(){}}E.public class MyRunnable implementsRunnable{public void start(){}}4. Given:2. class MyThread extends Thread {3. public static void main(String [] args) {4. MyThread t = new MyThread();5. t.start() ;6. System.out.print("one. ");7. t.start();8. System.out.print("two. ");9. }10. public void run() {11. System.out.print("Thread "};12. }13. }What is the result of this code?pilation fails.B.An exception occurs at runtime.C.Thread one. Thread two.D.The output cannot be determined.5. Given:3. class MyThread extends Thread {4. public static void main(String [] args) {5. MyThread t = new MyThread () ;6. Thread x = new Thread(t);7. x.start();8. }9. public void run() {10. for(int i=0;i<3;++i) {11. System.out.print(i + "..");12. }13. }14. }What is the result of this code?pilation fails.B.1‥2‥3‥C.0‥1‥2‥3‥D.0‥1‥2‥E.An exception occurs at runtime.。

相关主题