Given 11 class Converter 12 public static void mainString args 13 Integer i

Given 11 class converter 12 public static void

This preview shows page 39 - 42 out of 101 pages.

Question 88Given:11. class Converter {12. public static void main(String[] args) {13. Integer i = args[0];14. int j = 12;15. System.out.println(”It is “ + (j==i) + “that j==i.”);16. }17. }What is the result when the programmer attempts to compile the codeand run it with the command java Converter 12? A. It is true that j==i.B. It is false that j==i.C. An exception is thrown at runtime.D. Compilation fails because of an error in line 13.Answer: D Question 89Given this method in a class:21. public String toString() {22. StringBuffer buffer = new StringBuffer();23. buffer.append(’<’);24. buffer.append(this.name);25. buffer.append(’>’);26. return buffer.toString();
Image of page 39
27. }Which is true? Question 90Given:1. public class MyLogger {2. private StringBuilder logger = new StringBuuilder();3. public void log(String message, String user) {4. logger.append(message);5. logger.append(user);6. }7. }The programmer must guarantee that a single MyLogger object worksproperly for a multi-threaded system. How must this code be changedto be thread-safe? Question 91Given:11. public String makinStrings() {12. String s = “Fred”;13. s = s + “47”;14. s = s.substring(2, 5);15. s = s.toUpperCase();16. return s.toString();17. }How many String objects will be created when this method is invoked?
Image of page 40
Question 92Given:1. public class TestString3 {2. public static void main(String[] args) {3. // insert code here5. System.out.println(s);6. }7. }Which two code fragments, inserted independently at line 3, generatethe output 4247? (Choose two.) A. String s = “123456789”;s = (s-”123”).replace(1,3,”24”) - “89”;B. StringBuffer s = new StringBuffer(”123456789”);s.delete(0,3).replace( 1,3, “24”).delete(4,6);C. StringBuffer s = new StringBuffer(”123456789”);s.substring(3,6).delete( 1 ,3).insert( 1, “24”);D. StringBuilder s = new StringBuilder(”123456789”);s.substring(3,6).delete( 1 ,2).insert( 1, “24”);E. StringBuilder s = new StringBuilder(”123456789”);s.delete(0,3).delete( 1 ,3).delete(2,5).insert( 1, “24”);Answer: BE
Image of page 41
Image of page 42

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture