A 0 b 1 c 4 d compilation fails e an exception is

This preview shows page 98 - 103 out of 144 pages.

A. 0 B. 1 C. 4 D. Compilation fails. E. An exception is thrown at runtime. Answer: D Explanation/Reference: line 12: Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ ) You have to add another "\" to correctly escape it. QUESTION 200 Given: import java.io.*; class Animal { Animal() {
System. out .print("a"); } } class Dog extends Animal implements Serializable { Dog() { System. out .print("d"); } } public class Beagle extends Dog { } If an instance of class Beagle is created, then Serialized, then deSerialized, what is the result? A. ad B. ada C. add D. adad E. Compilation fails. F. An exception is thrown at runtime. Answer: B QUESTION 201 A team of programmers is involved in reviewing a proposed design for a new utility class. After some discussion, they realize that the current design allows other classes to access methods in the utility class that should be accessible only to methods within the utility class itself. What design issue has the team discovered? A. Tight coupling B. Low cohesion C. High cohesion D. Loose coupling E. Weak encapsulation F. Strong encapsulation Answer: E QUESTION 202 Given a method that must ensure that its parameter is not null: 11. public void someMethod(Object value) { 12. // check for null value ... 20. System.out.println(value.getClass()); 21. } What, inserted at line 12, is the appropriate way to handle a null value? A. assert value == null; B. assert value != null, "value is null"; C. if (value == null) { throw new AssertionException("value is null"); } D. if (value == null) { throw new IllegalArgumentException("value is null"); }
Answer: D QUESTION 203 Given: 1. public class Target { 2. private int i = 0; 3. public int addOne() { 4. return ++i; 5. } 6. } And: 1. public class Client { 2. public static void main(String[] args){ 3. System. out .println( new Target().addOne()); 4. } 5. } Which change can you make to Target without affecting Client? A. Line 4 of class Target can be changed to return i++; B. Line 2 of class Target can be changed to private int i = 1; C. Line 3 of class Target can be changed to private int addOne(){ D. Line 2 of class Target can be changed to private Integer i = 0; Answer: D QUESTION 204 Given: class Animal { public String noise() { return "peep"; } } class Dog extends Animal { public String noise() { return "bark"; } } class Cat extends Animal { public String noise() { return "meow"; } } 30. Animal animal = new Dog(); 31. Cat cat = (Cat)animal; 32. System.out.println(cat.noise());
What is the result? A. peep B. bark C. meow D. Compilation fails. E. An exception is thrown at runtime. Answer: E Explanation/Reference: Exception in thread "main" java.lang.ClassCastException : Dog cannot be cast to Cat at Client.main(Client.java:12 ) QUESTION 205 Given: abstract class A { abstract void a1(); void a2() { } } class B extends A { void a1() { } void a2() { } } class C extends B { void c1() { } } And: A x = new B(); C y = new C(); A z = new C(); What are four valid examples of polymorphic method calls? (Choose four.) A. x.a2(); B. z.a2(); C. z.c1(); D. z.a1(); E. y.c1(); F. x.a1(); Answer: ABDF
QUESTION 206 Given: class Employee { String name; double baseSalary; Employee(String name, double baseSalary) { this .name = name; this .baseSalary = baseSalary; } } 09. public class SalesPerson extends Employee { 10. double commission; 11.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture