Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 68 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
68
Dung lượng
620,66 KB
Nội dung
Color profile: Generic CMYK printer profile CertPrs8(SUN) / Sun Certified Composite Default screen Programmer & Developer for Java Study Guide / Sierra / 222684-6 / Chapter Self Test Given the following, 10 11 12 13 14 public class Switch2 { final static short x = 2; public static int y = 0; public static void main(String [] args) { for (int z=0; z < 4; z++) { switch (z) { case x: System.out.print("0 "); default: System.out.print("def "); case x-1: System.out.print("1 "); break; case x-2: System.out.print("2 "); } } } } what is the result? A def B def C def def D def def E 2 def F def def Given the following, 10 11 12 13 14 15 16 public class If2 { static boolean b1, public static void int x = 0; if ( !b1 ) { if ( !b2 ) { b1 = true; x++; if ( > ) x++; } if ( !b1 ) x else if ( b2 else if ( b1 } } P:\010Comp\CertPrs8\684-6\ch04.vp Wednesday, November 13, 2002 5:18:20 PM b2; main(String [] args) { { = x + 10; = true ) x = x + 100; | b2 ) x = x + 1000; 65 Color profile: Generic CMYK printer profile Composite Default CertPrs8(SUN) / Sun Certified screen 66 Chapter 4: 17 18 19 Programmer & Developer for Java Study Guide / Sierra / 222684-6 / Chapter Flow Control, Exceptions, and Assertions System.out.println(x); } } what is the result? A B C 101 D 111 E 1001 F 1101 Flow Control (loops) (Sun Objective 2.2) Given the following, public class While { public void loop() { int x= 0; while ( ) { System.out.print("x plus one is " + (x + 1)); } } } Which statement is true? A There is a syntax error on line B There are syntax errors on lines and C There are syntax errors on lines 1, 4, and D There is a syntax error on line E There are syntax errors on lines and F There is a syntax error on line Given the following, class For { public void test() { System.out.println("x = "+ x); P:\010Comp\CertPrs8\684-6\ch04.vp Wednesday, November 13, 2002 5:18:20 PM Color profile: Generic CMYK printer profile CertPrs8(SUN) / Sun Certified Composite Default screen Programmer & Developer for Java Study Guide / Sierra / 222684-6 / Chapter Self Test } } } and the following output, x = x = which two lines of code (inserted independently) will cause this output? (Choose two.) A for (int x = -1; x < 2; ++x) { B for (int x = 1; x < 3; ++x ) { C for (int x = 0; x > 2; ++x ) { D for (int x = 0; x < 2; x++ ) { E for (int x = 0; x < 2; ++x ) { Given the following, public class Test { public static void main(String [] args) { int I = 1; while ( I < ) System.out.print("I is " + I); while ( I > ) ; } } what is the result? A I is B I is I is C No output is produced D Compilation error E I is I is I is in an infinite loop Given the following, 11 12 13 14 15 16 int I = 0; outer: while (true) { I++; inner: for (int j = 0; j < 10; j++) { P:\010Comp\CertPrs8\684-6\ch04.vp Wednesday, November 13, 2002 5:18:20 PM 67 Color profile: Generic CMYK printer profile Composite Default CertPrs8(SUN) / Sun Certified screen 68 Chapter 4: 17 18 19 20 21 22 23 24 25 26 Programmer & Developer for Java Study Guide / Sierra / 222684-6 / Chapter Flow Control, Exceptions, and Assertions I += j; if (j == 3) continue inner; break outer; } continue outer; } System.out.println(I); what is the result? A B C D 10 Given the following, int I = 0; label: if (I < 2) { System.out.print("I is " + I); I++; continue label; } what is the result? A I is B I is I is C Compilation fails D None of the above Exceptions (Sun Objectives 2.3 and 2.4) 11 Given the following, System.out.print("Start "); try { System.out.print("Hello world"); throw new FileNotFoundException(); P:\010Comp\CertPrs8\684-6\ch04.vp Wednesday, November 13, 2002 5:18:20 PM Color profile: Generic CMYK printer profile CertPrs8(SUN) / Sun Certified Composite Default screen Programmer & Developer for Java Study Guide / Sierra / 222684-6 / Chapter Self Test 10 11 12 69 } System.out.print(" Catch Here "); catch(EOFException e) { System.out.print("End of file exception"); } catch(FileNotFoundException e) { System.out.print("File not found"); } and given that EOFException and FileNotFoundException are both subclasses of IOException, and further assuming this block of code is placed into a class, which statement is most true concerning this code? A The code will not compile B Code output: Start Hello world File Not Found C Code output: Start Hello world End of file exception D Code output: Start Hello world Catch Here File not found 12 Given the following, 10 public class MyProgram { public static void main(String args[]){ try { System.out.print("Hello world "); } finally { System.out.println("Finally executing "); } } } what is the result? A Nothing The program will not compile because no exceptions are specified B Nothing The program will not compile because no catch clauses are specified C Hello world D Hello world Finally executing 13 Given the following, import java.io.*; public class MyProgram { public static void main(String args[]){ FileOutputStream out = null; P:\010Comp\CertPrs8\684-6\ch04.vp Wednesday, November 13, 2002 5:18:20 PM Color profile: Generic CMYK printer profile Composite Default CertPrs8(SUN) / Sun Certified screen 70 Chapter 4: 10 11 12 13 14 15 16 Programmer & Developer for Java Study Guide / Sierra / 222684-6 / Chapter Flow Control, Exceptions, and Assertions try { out = new FileOutputStream("test.txt"); out.write(122); } catch(IOException io) { System.out.println("IO Error."); } finally { out.close(); } } } and given that all methods of class FileOutputStream, including close(), throw an IOException, which of these is true? (Choose one.) A This program will compile successfully B This program fails to compile due to an error at line C This program fails to compile due to an error at line D This program fails to compile due to an error at line E This program fails to compile due to an error at line 13 14 Given the following, 10 11 12 13 14 15 public class MyProgram { public static void throwit() { throw new RuntimeException(); } public static void main(String args[]){ try { System.out.println("Hello world "); throwit(); System.out.println("Done with try block "); } finally { System.out.println("Finally executing "); } } } which answer most closely indicates the behavior of the program? A The program will not compile P:\010Comp\CertPrs8\684-6\ch04.vp Wednesday, November 13, 2002 5:18:20 PM Color profile: Generic CMYK printer profile CertPrs8(SUN) / Sun Certified Composite Default screen Programmer & Developer for Java Study Guide / Sierra / 222684-6 / Chapter Self Test 71 B The program will print Hello world, then will print that a RuntimeException has occurred, then will print Done with try block, and then will print Finally executing C The program will print Hello world, then will print that a RuntimeException has occurred, and then will print Finally executing D The program will print Hello world, then will print Finally executing, then will print that a RuntimeException has occurred 15 Given the following, 10 11 12 13 14 15 16 17 18 19 public class RTExcept { public static void throwit () { System.out.print("throwit "); throw new RuntimeException(); } public static void main(String [] args) { try { System.out.print("hello "); throwit(); } catch (Exception re ) { System.out.print("caught "); } finally { System.out.print("finally "); } System.out.println("after "); } } what is the result? A hello throwit caught B Compilation fails C hello throwit RuntimeException caught after D hello throwit RuntimeException E hello throwit caught finally after F hello throwit caught finally after RuntimeException P:\010Comp\CertPrs8\684-6\ch04.vp Wednesday, November 13, 2002 5:18:21 PM Color profile: Generic CMYK printer profile Composite Default CertPrs8(SUN) / Sun Certified screen 72 Chapter 4: Programmer & Developer for Java Study Guide / Sierra / 222684-6 / Chapter Flow Control, Exceptions, and Assertions Assertions (Sun Objectives 2.5 and 2.6) 16 Which of the following statements is true? A In an assert statement, the expression after the colon ( : ) can be any Java expression B If a switch block has no default, adding an assert default is considered appropriate C In an assert statement, if the expression after the colon ( : ) does not have a value, the assert’s error message will be empty D It is appropriate to handle assertion failures using a catch clause 17 Which two of the following statements are true? (Choose two.) A It is sometimes good practice to throw an AssertionError explicitly B It is good practice to place assertions where you think execution should never reach C Private getter() and setter() methods should not use assertions to verify arguments D If an AssertionError is thrown in a try-catch block, the finally block will be bypassed E It is proper to handle assertion statement failures using a catch (AssertionException ae) block 18 Given the following, 10 11 12 13 14 15 16 17 public class Test { public static int y; public static void foo(int x) { System.out.print("foo "); y = x; } public static int bar(int z) { System.out.print("bar "); return y = z; } public static void main(String [] args ) { int t = 0; assert t > : bar(7); assert t > : foo(8); System.out.println("done "); } } what is the result? A bar P:\010Comp\CertPrs8\684-6\ch04.vp Wednesday, November 13, 2002 5:18:21 PM Color profile: Generic CMYK printer profile CertPrs8(SUN) / Sun Certified Composite Default screen Programmer & Developer for Java Study Guide / Sierra / 222684-6 / Chapter Self Test 73 B bar done C foo done D bar foo done E Compilation fails F An error is thrown at runtime 19 Which two of the following statements are true? (Choose two.) A If assertions are compiled into a source file, and if no flags are included at runtime, assertions will execute by default B As of Java version 1.4, assertion statements are compiled by default C With the proper use of runtime arguments, it is possible to instruct the VM to disable assertions for a certain class, and to enable assertions for a certain package, at the same time D The following are all valid runtime assertion flags: -ea, -esa, -dsa, -enableassertions, -disablesystemassertions E When evaluating command-line arguments, the VM gives –ea flags precedence over –da flags 20 Given the following, 10 11 12 13 14 15 16 17 18 19 20 21 public class Test2 { public static int x; public static int foo(int y) { return y * 2; } public static void main(String [] args) { int z = 5; assert z > 0; assert z > 2: foo(z); if ( z < ) assert z > 4; switch (z) { case 4: System.out.println("4 "); case 5: System.out.println("5 "); default: assert z < 10; } if ( z < 10 ) assert z > 4: z++; System.out.println(z); } } P:\010Comp\CertPrs8\684-6\ch04.vp Wednesday, November 13, 2002 5:18:21 PM Color profile: Generic CMYK printer profile Composite Default CertPrs8(SUN) / Sun Certified screen 74 Chapter 4: Programmer & Developer for Java Study Guide / Sierra / 222684-6 / Chapter Flow Control, Exceptions, and Assertions which line is an example of an inappropriate use of assertions? A Line B Line C Line 11 D Line 15 E Line 18 P:\010Comp\CertPrs8\684-6\ch04.vp Wednesday, November 13, 2002 5:18:21 PM Color profile: Generic CMYK printer profile Composite Default CertPrs8(SUN) / Sun Certified screen 38 Chapter 5: Programmer & Developer for Java Study Guide / Sierra / 222684-6 / Chapter Object Orientation, Overloading and Overriding, Constructors, and Return Types An array is a perfectly legal return type public String [] go() { return new String[] {"Fred", "Barney", "Wilma"}; } In a method with a primitive return type, you can return any value or variable that can be implicitly converted to the declared return type public int foo() { char c = 'c'; return c; // char is compatible with int } In a method with a primitive return type, you can return any value or variable that can be explicitly cast to the declared return type public int foo () { float f = 32.5f; return (int) f; } You must not return anything from a method with a void return type public void bar() { return "this is it"; } // Not legal!! In a method with an object reference return type, you can return any object type that can be implicitly cast to the declared return type public Animal getAnimal() { return new Horse(); // Assume Horse extends Animal } public Object getObject() { int[] nums = {1,2,3}; return nums; // Return an int array, which is still an object } public interface Chewable { } public class Gum implements Chewable { } public class TestChewable { // Method with an interface return type public Chewable getChewable { P:\010Comp\CertPrs8\684-6\ch05.vp Wednesday, November 13, 2002 5:17:18 PM Color profile: Generic CMYK printer profile CertPrs8(SUN) / Sun Certified Composite Default screen Programmer & Developer for Java Study Guide / Sierra / 222684-6 / Chapter Legal Return Types (Exam Objective 1.4) return new Gum(); 39 // Return interface implementer } } Watch for methods that declare an abstract class or interface return type, and know that any object that passes the IS-A test (in other words, would test true using the instanceof operator) can be returned from that method— for example: public abstract class Animal { } public class Bear extends Animal { } public class Test { public Animal go() { return new Bear(); // OK, Bear "is-a" Animal } } Be sure you understand the rules for casting primitives Take a look at the following: public short s = (short) (90 + 900000); The preceding code compiles fine But look at this variation: public short s = (short) 90 + 900000; // Illegal! By leaving off the parentheses around the arithmetic expression, the cast (short) applies only to the first number! So the compiler gives us Test.java:4: possible loss of precision found : int required: short short s = (short) 90 + 900000; ^ Casting rules matter when returning values, so the following code would not compile, public short foo() { return (short) 90 + 900000; } but with parentheses around (90 + 900000), it compiles fine P:\010Comp\CertPrs8\684-6\ch05.vp Wednesday, November 13, 2002 5:17:18 PM Color profile: Generic CMYK printer profile Composite Default CertPrs8(SUN) / Sun Certified screen 40 Chapter 5: Programmer & Developer for Java Study Guide / Sierra / 222684-6 / Chapter Object Orientation, Overloading and Overriding, Constructors, and Return Types CERTIFICATION SUMMARY Let’s take a stroll through Chapter and see where we’ve been You looked at how encapsulation can save you from being ripped to shreds by programmers whose code you could break if you change the way client code accesses your data Protecting the instance variables (often by marking them private) and providing more accessible getter and setter methods represent the good OO practice of encapsulation, and support flexibility and maintainability by hiding your implementation details from other code You learned that inheritance relationships are described using IS-A, as in “Car IS-A Vehicle,” and that the keyword extends is used to define IS-A relationships in Java: class Car extends Vehicle You also learned that reference relationships are described using HAS-A, as in “Car HAS-A Engine.” HAS-A relationships in Java often are defined by giving one class a reference to another, usually through instance variable declarations: class Car extends Vehicle { private Engine eng; // Now Car has-a Engine, // and can thus invoke methods on it } We looked at the difference between overridden and overloaded methods, learning that an overridden method occurs when a subclass inherits a method from a superclass, but the subclass redefines it to add more specialized behavior We learned that at runtime, the JVM will invoke the subclass version on an instance of a subclass, and the superclass version on an instance of the superclass Remember that abstract methods must be overridden (technically abstract methods must be implemented, as opposed to overridden, since there really isn’t anything to override in an abstract method, but who’s counting?) We saw that overriding methods must keep the same argument list and return type as the overridden method, and that the access modifier can’t be more restrictive The overriding method also can’t throw any new or broader checked exceptions that weren’t declared in the overridden method You also learned that the overridden method can be invoked using the syntax super.doSomething(); P:\010Comp\CertPrs8\684-6\ch05.vp Wednesday, November 13, 2002 5:17:18 PM Color profile: Generic CMYK printer profile CertPrs8(SUN) / Sun Certified Composite Default screen Programmer & Developer for Java Study Guide / Sierra / 222684-6 / Chapter Legal Return Types (Exam Objective 1.4) 41 Overloaded methods let you reuse the same method name in a class, but with different arguments (and optionally, a different return type) Whereas overriding methods must not change the argument list, overloaded methods must But unlike overriding methods, overloaded methods are free to vary the return type, access modifier, and declared exceptions any way they like We covered constructors in detail, learning that even if you don’t provide a constructor for your class, the compiler will always insert one The compiler-generated constructor is called the default constructor, and it is always a no-arg constructor with a no-arg call to super() The default constructor will never be generated if there is even a single constructor in your class (and regardless of the arguments of that constructor), so if you need more than one constructor in your class and you want a no-arg constructor, you’ll have to write it yourself We also saw that constructors are not inherited, and that you can be confused by a method that has the same name as the class (which is legal) The return type is the giveaway that a method is not a constructor, since constructors not have return types We saw how all of the constructors in an object’s inheritance tree will always be invoked when the object is instantiated using new We also saw that constructors can be overloaded, which means defining constructors with different argument lists A constructor can invoke another constructor of the same class using the keyword this(), as though the constructor were a method named this() We saw that every constructor must have either this() or super() as the first statement We also looked at method return types, and saw that you can declare any return type you like (assuming you have access to a class for an object reference return type), unless you’re overriding a method An overriding method must have the same return type as the overridden method of the superclass We saw that while overriding methods must not change the return type, overloaded methods can (as long as they also change the argument list) Finally, you learned that it is legal to return any value or variable that can be implicitly converted to the declared return type So, for example, a short can be returned when the return type is declared as an int And a Horse reference can be returned when the return type is declared an Animal (assuming Horse extends Animal) And once again, you learned that the exam includes tricky questions designed largely to test your ability to recognize just how tricky the questions can be If you took our advice about the margarita, you might want to review the following Two-Minute Drill again after you’re sober P:\010Comp\CertPrs8\684-6\ch05.vp Wednesday, November 13, 2002 5:17:18 PM Color profile: Generic CMYK printer profile Composite Default CertPrs8(SUN) / Sun Certified screen 42 Chapter 5: ✓ Programmer & Developer for Java Study Guide / Sierra / 222684-6 / Chapter Object Orientation, Overloading and Overriding, Constructors, and Return Types TWO-MINUTE DRILL Here are some of the key points from each certification objective in Chapter Encapsulation, IS-A, HAS-A (Sun Objective 6.1) ❑ The goal of encapsulation is to hide implementation behind an interface (or API) ❑ Encapsulated code has two features: ❑ Instance variables are kept protected (usually with the private modifier) ❑ Getter and setter methods provide access to instance variables ❑ IS-A refers to inheritance ❑ IS-A is expressed with the keyword extends ❑ “IS-A,” “inherits from,” “is derived from,” and “is a subtype of” are all equivalent expressions ❑ HAS-A means an instance of one class “has a” reference to an instance of another class Overriding and Overloading (Sun Objective 6.2) ❑ Methods can be overridden or overloaded; constructors can be overloaded but not overridden ❑ Abstract methods must be overridden by the first concrete (nonabstract) subclass ❑ With respect to the method it overrides, the overriding method ❑ Must have the same argument list ❑ Must have the same return type ❑ Must not have a more restrictive access modifier ❑ May have a less restrictive access modifier ❑ Must not throw new or broader checked exceptions ❑ May throw fewer or narrower checked exceptions, or any unchecked exception P:\010Comp\CertPrs8\684-6\ch05.vp Wednesday, November 13, 2002 5:17:20 PM Color profile: Generic CMYK printer profile CertPrs8(SUN) / Sun Certified Composite Default screen Programmer & Developer for Java Study Guide / Sierra / 222684-6 / Chapter Two-Minute Drill 43 ❑ Final methods cannot be overridden ❑ Only inherited methods may be overridden ❑ A subclass uses super.overriddenMethodName to call the superclass version of an overridden method ❑ Overloading means reusing the same method name, but with different arguments ❑ Overloaded methods ❑ Must have different argument lists ❑ May have different return types, as long as the argument lists are also different ❑ May have different access modifiers ❑ May throw different exceptions ❑ Methods from a superclass can be overloaded in a subclass ❑ Polymorphism applies to overriding, not to overloading ❑ Object type determines which overridden method is used at runtime ❑ Reference type determines which overloaded method will be used at compile time Instantiation and Constructors (Sun Objectives 6.3 and 1.3) ❑ Objects are constructed: ❑ You cannot create a new object without invoking a constructor ❑ Each superclass in an object’s inheritance tree will have a constructor called ❑ Every class, even abstract classes, has at least one constructor ❑ Constructors must have the same name as the class ❑ Constructors not have a return type If there is a return type, then it is simply a method with the same name as the class, and not a constructor ❑ Constructor execution occurs as follows: ❑ The constructor calls its superclass constructor, which calls its superclass constructor, and so on all the way up to the Object constructor P:\010Comp\CertPrs8\684-6\ch05.vp Wednesday, November 13, 2002 5:17:21 PM Color profile: Generic CMYK printer profile Composite Default CertPrs8(SUN) / Sun Certified screen 44 Chapter 5: Programmer & Developer for Java Study Guide / Sierra / 222684-6 / Chapter Object Orientation, Overloading and Overriding, Constructors, and Return Types ❑ The Object constructor executes and then returns to the calling constructor, which runs to completion and then returns to its calling constructor, and so on back down to the completion of the constructor of the actual instance being created ❑ Constructors can use any access modifier (even private!) ❑ The compiler will create a default constructor if you don’t create any constructors in your class ❑ The default constructor is a no-arg constructor with a no-arg call to super() ❑ The first statement of every constructor must be a call to either this() (an overloaded constructor) or super() ❑ The compiler will add a call to super() if you not, unless you have already put in a call to this() ❑ Instance methods and variables are only accessible after the super constructor runs ❑ Abstract classes have constructors that are called when a concrete subclass is instantiated ❑ Interfaces not have constructors ❑ If your superclass does not have a no-arg constructor, you must create a constructor and insert a call to super() with arguments matching those of the superclass constructor ❑ Constructors are never inherited, thus they cannot be overridden ❑ A constructor can be directly invoked only by another constructor (using a call to super() or this()) ❑ Issues with calls to this(): ❑ May appear only as the first statement in a constructor ❑ The argument list determines which overloaded constructor is called ❑ Constructors can call constructors can call constructors, and so on, but sooner or later one of them better call super() or the stack will explode ❑ this() and super() cannot be in the same constructor You can have one or the other, but never both P:\010Comp\CertPrs8\684-6\ch05.vp Wednesday, November 13, 2002 5:17:22 PM Color profile: Generic CMYK printer profile CertPrs8(SUN) / Sun Certified Composite Default screen Programmer & Developer for Java Study Guide / Sierra / 222684-6 / Chapter Two-Minute Drill 45 Return Types (Sun Objectives 1.4) ❑ Overloaded methods can change return types; overridden methods cannot ❑ Object reference return types can accept null as a return value ❑ An array is a legal return type, both to declare and return as a value ❑ For methods with primitive return types, any value that can be implicitly converted to the return type can be returned ❑ Nothing can be returned from a void, but you can return nothing You’re allowed to simply say return, in any method with a void return type, to bust out of a method early But you can’t return nothing from a method with a non-void return type ❑ For methods with an object reference return type, a subclass of that type can be returned ❑ For methods with an interface return type, any implementer of that interface can be returned P:\010Comp\CertPrs8\684-6\ch05.vp Wednesday, November 13, 2002 5:17:23 PM Color profile: Generic CMYK printer profile Composite Default CertPrs8(SUN) / Sun Certified screen 46 Chapter 5: Programmer & Developer for Java Study Guide / Sierra / 222684-6 / Chapter Object Orientation, Overloading and Overriding, Constructors, and Return Types SELF TEST The following questions will help you measure your understanding of the material presented in this chapter Don’t even think about skipping this test You really need to see what the questions on the exam can be like, and check your grasp and memorization of this chapter’s topics Encapsulation, IS-A, HAS-A (Sun Objective 6.1) Given the following, public class Barbell { public int getWeight() { return weight; } public void setWeight(int w) { weight = w; } public int weight; } which is true about the class described above? A Class Barbell is tightly encapsulated B Line is in conflict with encapsulation C Line is in conflict with encapsulation D Line is in conflict with encapsulation E Lines and are in conflict with encapsulation F Lines 2, 5, and are in conflict with encapsulation Given the following, public class B extends A { private int bar; public void setBar(int b) { bar = b; } } class A { public int foo; } which is true about the classes described above? A Class A is tightly encapsulated P:\010Comp\CertPrs8\684-6\ch05.vp Wednesday, November 13, 2002 5:17:24 PM Color profile: Generic CMYK printer profile CertPrs8(SUN) / Sun Certified Composite Default screen Programmer & Developer for Java Study Guide / Sierra / 222684-6 / Chapter Self Test B Class B is tightly encapsulated C Classes A and B are both tightly encapsulated D Neither class A nor class B is tightly encapsulated Which is true? A Tightly encapsulated classes are typically easier to reuse B Tightly encapsulated classes typically use inheritance more than unencapsulated classes C Methods in tightly encapsulated classes cannot be overridden D Methods in tightly encapsulated classes cannot be overloaded E Tightly encapsulated classes typically not use HAS-A relationships Which two are not benefits of encapsulation? (Choose two.) A Clarity of code B Code efficiency C The ability to add functionality later on D Modifications require fewer coding changes E Access modifiers become optional Given the following, 10 11 12 class B extends A { int getID() { return id; } } class C { public int name; } class A { C c = new C(); public int id; } which two are true about instances of the classes listed above? (Choose two.) A A is-a B B C is-a A C A has-a C D B has-a A E B has-a C P:\010Comp\CertPrs8\684-6\ch05.vp Wednesday, November 13, 2002 5:17:24 PM 47 Color profile: Generic CMYK printer profile Composite Default CertPrs8(SUN) / Sun Certified screen 48 Chapter 5: Programmer & Developer for Java Study Guide / Sierra / 222684-6 / Chapter Object Orientation, Overloading and Overriding, Constructors, and Return Types Overriding and Overloading (Sun Objective 6.2) Given the following, class A { public void baz() { System.out.println("A"); } } public class B extends A { public static void main(String [] args) { A a = new B(); a.baz(); } public void baz() { System.out.println("B"); } } what is the result? A A B B C Compilation fails D An exception is thrown at runtime Given the following, class Foo { String doStuff(int x) { return "hello"; } } which method would not be legal in a subclass of Foo? A String doStuff(int x) { return "hello"; } B int doStuff(int x) { return 42; } C public String doStuff(int x) { return "Hello"; } D protected String doStuff(int x) { return "Hello"; } E String doStuff(String s) { return "Hello"; } F int doStuff(String s) { return 42; } Given the following, class ParentClass { public int doStuff(int x) { return x * 2; P:\010Comp\CertPrs8\684-6\ch05.vp Wednesday, November 13, 2002 5:17:24 PM Color profile: Generic CMYK printer profile CertPrs8(SUN) / Sun Certified Composite Default screen Programmer & Developer for Java Study Guide / Sierra / 222684-6 / Chapter Self Test 10 11 12 13 14 15 16 17 } } public class ChildClass extends ParentClass { public static void main(String [] args ) { ChildClass cc = new ChildClass(); long x = cc.doStuff(7); System.out.println("x = " + x); } public long doStuff(int x) { return x * 3; } } What is the result? A x = 14 B x = 21 C Compilation fails at line D Compilation fails at line 11 E Compilation fails at line 14 F An exception is thrown at runtime Given the following, class Over { int doStuff(int a, float b) { return 7; } } class Over2 extends Over { // insert code here } which two methods, if inserted independently at line 8, will not compile? (Choose two.) A public int doStuff(int x, float y) { return 4; } B protected int doStuff(int x, float y) {return 4; } C private int doStuff(int x, float y) {return 4; } D private int doStuff(int x, double y) { return 4; } E long doStuff(int x, float y) { return 4; } F int doStuff(float x, int y) { return 4; } P:\010Comp\CertPrs8\684-6\ch05.vp Wednesday, November 13, 2002 5:17:24 PM 49 Color profile: Generic CMYK printer profile Composite Default CertPrs8(SUN) / Sun Certified screen 50 Chapter 5: Programmer & Developer for Java Study Guide / Sierra / 222684-6 / Chapter Object Orientation, Overloading and Overriding, Constructors, and Return Types Instantiation and Constructors (Sun Objectives 6.3 and 1.3) 10 Given the following, 10 11 12 13 14 15 16 17 18 public class TestPoly { public static void main(String [] args ){ Parent p = new Child(); } } class Parent { public Parent() { super(); System.out.println("instantiate a parent"); } } class Child extends Parent { public Child() { System.out.println("instantiate a child"); } } what is the result? A instantiate a child B instantiate a parent C instantiate a child instantiate a parent D instantiate a parent instantiate a child E Compilation fails F An exception is thrown at runtime 11 Given the following, public class TestPoly { public static void main(String [] args ){ Parent p = new Child(); } } class Parent { public Parent() { super(); P:\010Comp\CertPrs8\684-6\ch05.vp Wednesday, November 13, 2002 5:17:24 PM Color profile: Generic CMYK printer profile CertPrs8(SUN) / Sun Certified Composite Default screen Programmer & Developer for Java Study Guide / Sierra / 222684-6 / Chapter Self Test 10 11 12 13 14 15 16 17 18 19 System.out.println("instantiate a parent"); } } class Child extends Parent { public Child() { System.out.println("instantiate a child"); super(); } } what is the result? A instantiate a child B instantiate a parent C instantiate a child instantiate a parent D instantiate a parent instantiate a child E Compilation fails F An exception is thrown at runtime 12 Given the following, 10 11 12 13 14 15 16 class MySuper { public MySuper(int i) { System.out.println("super " + i); } } public class MySub extends MySuper { public MySub() { super(2); System.out.println("sub"); } public static void main(String [] args) { MySuper sup = new MySub(); } } what is the result? A sub super P:\010Comp\CertPrs8\684-6\ch05.vp Wednesday, November 13, 2002 5:17:24 PM 51 Color profile: Generic CMYK printer profile Composite Default CertPrs8(SUN) / Sun Certified screen 52 Chapter 5: Programmer & Developer for Java Study Guide / Sierra / 222684-6 / Chapter Object Orientation, Overloading and Overriding, Constructors, and Return Types B super sub C Compilation fails at line D Compilation fails at line E Compilation fails at line F Compilation fails at line 14 13 Given the following, 10 11 12 13 14 15 16 17 public class ThreeConst { public static void main(String [] args) { new ThreeConst(4L); } public ThreeConst(int x) { this(); System.out.print(" " + (x * 2)); } public ThreeConst(long x) { this((int) x); System.out.print(" " + x); } public ThreeConst() { System.out.print("no-arg "); } } what is the result? A B C D no-arg E no-arg F Compilation fails 14 Given the following, public class ThreeConst { public static void main(String [] args) { new ThreeConst(); } P:\010Comp\CertPrs8\684-6\ch05.vp Wednesday, November 13, 2002 5:17:24 PM ... profile Composite Default CertPrs8 (SUN) / Sun Certified screen 68 Chapter 4: 17 18 19 20 21 22 23 24 25 26 Programmer & Developer for Java Study Guide / Sierra / 22 2684-6 / Chapter Flow Control,... 13, 20 02 5: 18 :21 PM Color profile: Generic CMYK printer profile Composite Default CertPrs8 (SUN) / Sun Certified screen 72 Chapter 4: Programmer & Developer for Java Study Guide / Sierra / 22 2684-6... 13, 20 02 5: 18 :22 PM Color profile: Generic CMYK printer profile Composite Default CertPrs8 (SUN) / Sun Certified screen 78 Chapter 4: Programmer & Developer for Java Study Guide / Sierra / 22 2684-6