jobs4timesLogo jobs4timesLogo

Java.lang Package Certification Programs

Q:01 Given:

11. public class Person { 
12. private String name, comment; 
13. private int age; 
14. public Person(String n, int a, String c) { 
15. name = n; age = a; comment = c; 
16. } 
17. public boolean equals(Object o) { 
18. if (! (o instanceof Person)) return false; 
19, Person p = (Person)o; 
20. return age == p.age && name.equals(p.name); 
21. } 
22. }
What is the appropriate definition of the hashCode method in class Person ?
  1. return super.hashCode();
  2. return name.hashCode() + age * 7;
  3. return name.hashCode() + comment.hashCode() / 2;
  4. return name.hashCode() + comment.hashCode() / 2 - age * 3;
Answer: B

Q: 02 Given 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(); 
27. }
Which statement is true ?
  1. This code is NOT thread-safe.
  2. The programmer can replace StringBuffer with StringBuilder with no other changes.
  3. This code will perform poorly. For better performance, the code should be rewritten: return "<" + this.name + ">";
  4. This code will perform well and converting the code to use StringBuilder will not enhance the performance.
Answer: B

Q: 03 Given:

11. public void testIfA() { 
12. if (testIfB("True")) { 
13. System.out.println("True"); 
14. } else { 
15. System.out.println("Not true"); 
16. }
17. } 
18. public Boolean testIfB(String str) { 
19. return Boolean.valueOf(str); 
20. }
What is the result when method testIfA is invoked ?
  1. True
  2. Not true
  3. An exception is thrown at runtime.
  4. Compilation fails because of an error at line 12.
  5. Compilation fails because of an error at line 19.
Answer: A

Q: 04 Given:

1. public class Boxer1{ 
2. Integer i; 
3. int x; 
4. public Boxer1(int y) { 
5. x = i+y; 
6. System.out.println(x); 
7. } 
8. public static void main(String[] args) { 
9. new Boxer1(new Integer(4)); 
10. } 
11. }
What is the result ?
  1. The value "4" is printed at the command line.
  2. Compilation fails because of an error in line 5.
  3. Compilation fails because of an error in line 9.
  4. A NullPointerException occurs at runtime.
  5. A NumberFormatException occurs at runtime.
  6. An IllegalStateException occurs at runtime.
Answer: D

Q: 05 Given:

1. public class TestString3 { 
2. public static void main(String[] args) { 
3. // insert code here 

5. System.out.println(s); 
6. } 
7. }
Which two code fragments, inserted independently at line 3, generate the output 4247 ? (Choose two.)
  1. String s = "123456789"; s = (s-"123").replace(1,3,"24") - "89";
  2. StringBuffer s = new StringBuffer("123456789"); s.delete(0,3).replace(1,3,"24").delete(4,6);
  3. StringBuffer s = new StringBuffer("123456789"); s.substring(3,6).delete(1,3).insert(1, "24");
  4. StringBuilder s = new StringBuilder("123456789"); s.substring(3,6).delete(1,2).insert(1, "24");
  5. StringBuilder s = new StringBuilder("123456789"); s.delete(0,3).delete(1,3).delete(2,5).insert(1, "24");
Answer: B, E

Q: 06 Given:

11. public static void test(String str) { 
12. int check = 4; 
13. if (check = str.length()) { 
14. System.out.print(str.charAt(check -= 1) +", "); 
15. } else { 
16. System.out.print(str.charAt(0) + ", "); 
17. } 
18. } 
and the invocation: 
21. test("four"); 
22. test("tee"); 
23. test("to");
What is the result ?
  1. r, t, t,
  2. r, e, o,
  3. Compilation fails.
  4. An exception is thrown at runtime.
Answer: C

Q: 07 Given:

11. public class Person { 
12. private String name; 
13. public Person(String name) { 
14. this.name = name; 
15. } 
16. public boolean equals(Object o) { 
17. if ( ! o instanceof Person ) return false; 
18. Person p = (Person) o; 
19. return p.name.equals(this.name); 
20. } 
21. }
Which statement is true ?
  1. Compilation fails because the hashCode method is not overridden.
  2. A HashSet could contain multiple Person objects with the same name.
  3. All Person objects will have the same hash code because the hashCode method is not overridden.
  4. If a HashSet contains more than one Person object with name="Fred", then removing another Person, also with name="Fred", will remove them all.
Answer: B

Q: 08 Which two statements are true about the hashCode method? (Choose two.)

  1. The hashCode method for a given class can be used to test for object equality and object inequality for that class.
  2. The hashCode method is used by the java.util.SortedSet collection class to order the elements within that set.
  3. The hashCode method for a given class can be used to test for object inequality, but NOT object equality, for that class.
  4. The only important characteristic of the values returned by a hashCode method is that the distribution of values must follow a Gaussian distribution.
  5. The hashCode method is used by the java.util.HashSet collection class to group the elements within that set into hash buckets for swift retrieval.
Answer: C, E

Q: 09 Click the Task button.

1.<T extends Pet>
2. T
3.T
4.T

Q: 10 Given:

10. public class MyClass { 
11. 
12. public Integer startingI; 
13. public void methodA() { 
14. Integer i = new Integer(25); 
15. startingI = i; 
16. methodB(i); 
17. } 
18. private void methodB(Integer i2) { 
19. i2 = i2.intValue(); 
20. 
21. } 
22. }
If methodA is invoked, which two are true at line 20 ? (Choose two.)
  1. i2 == startingI returns true.
  2. i2 == startingI returns false.
  3. i2.equals(startingI) returns true.
  4. i2.equals(startingI) returns false.
Answer: B, C

Q : 11 Given:

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 ?
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
Answer: E

Q: 12 Given:

22. StringBuilder sb1 = new StringBuilder("123"); 
23. String s1 = "123"; 
24. // insert code here 
25. System.out.println(sb1 + " " + s1);
Which code fragment, inserted at line 24, outputs "123abc 123abc" ?
  1. sb1.append("abc"); s1.append("abc");
  2. sb1.append("abc"); s1.concat("abc");
  3. sb1.concat("abc"); s1.append("abc");
  4. sb1.concat("abc"); s1.concat("abc");
  5. sb1.append("abc"); s1 = s1.concat("abc");
  6. sb1.concat("abc"); s1 = s1.concat("abc");
  7. sb1.append("abc"); s1 = s1 + s1.concat("abc");
  8. sb1.concat("abc"); s1 = s1 + s1.concat("abc");
Answer: E

Q: 13 Given:

1. public class BuildStuff { 
2. public static void main(String[] args) { 
3. Boolean test = new Boolean(true); 
4. Integer x = 343; 
5. Integer y = new BuildStuff().go(test, x); 
6. System.out.println(y); 
7. } 
8. int go(Boolean b, int i) { 
9. if(b) return (i/7); 
10. return (i/49); 
11. } 
12. }
What is the result ?
  1. 7
  2. 49
  3. 343
  4. Compilation fails.
  5. An exception is thrown at runtime.
Answer: B

Q: 14 Given:

1. public class KungFu { 
2. public static void main(String[] args) { 
3. Integer x = 400; 
4. Integer y = x; 
5. x++; 
6. StringBuilder sb1 = new StringBuilder("123"); 
7. StringBuilder sb2 = sb1; 
8. sb1.append("5"); 
9. System.out.println((x==y) + " " + (sb1==sb2)); 
10. } 
11. } 
What is the result ?
  1. true true
  2. false true
  3. true false
  4. false false
  5. Compilation fails.
  6. An exception is thrown at runtime.
Answer: B

Q: 15 Given:

Which two scenarios are NOT safe to replace a StringBuffer object with a StringBuilder object ? (Choose two.)
  1. When using versions of Java technology earlier than 5.0.
  2. When sharing a StringBuffer among multiple threads.
  3. When using the java.io class StringBufferInputStream.
  4. When you plan to reuse the StringBuffer to build more than one string.
Answer: A, B

16.

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.)
  1. If the equals() method returns true, the hashCode() comparison == might return false.
  2. If the equals() method returns false, the hashCode() comparison == might return true.
  3. If the hashCode() comparison == returns true, the equals() method must return true.
  4. If the hashCode() comparison == returns true, the equals() method might return true.
  5. If the hashCode() comparison != returns true, the equals() method might return true.
Answer:
  • B and D. B is true because often two dissimilar objects can return the same hashcode value. D is true because if the hashCode() comparison returns ==, the two objects might or might not be equal.
  • A, C, and E are incorrect. C is incorrect because the hashCode() method is very flexible in its return values, and often two dissimilar objects can return the same hash code value. A and E are a negation of the hashCode() and equals() contract.

17. Given:

1. class Convert { 
2. public static void main(String[] args) { 
3. Long xL = new Long(456L); 
4. long x1 = Long.valueOf("123"); 
5. Long x2 = Long.valueOf("123"); 
6. long x3 = xL.longValue(); 
7. Long x4 = xL.longValue(); 
8. Long x5 = Long.parseLong("456"); 
9. long x6 = Long.parseLong("123"); 
10. } 
11. }
Which will compile using Java 5, but will NOT compile using Java 1.4 ? (Choose all that apply.)
  1. Line 4.
  2. Line 5.
  3. Line 6.
  4. Line 7.
  5. Line 8.
  6. Line 9.
Answer:
  • A, D, and E are correct. Because of the methods‘ return types, these method calls required autoboxing to compile.
  • B, C, and F are incorrect based on the above.

18. 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(s1 < 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.)
  1. .e1
  2. .e2 C. =s
  3. fly E. None of the above.
  4. Compilation fails.
  5. An exception is thrown at runtime.
Answer:
  • B , C, and D are correct. Remember, that the equals() method for the integer wrappers will only return true if the two primitive types and the two values are equal. With C, it's okay to unbox and use ==. For D, it's okay to create a wrapper object with an expression, and unbox it for comparison with a primitive.
  • A, E, F, and G are incorrect based on the above. (Remember that A is using the equals() method to try to compare two different types.)

19. Which about the three java.lang classes String, StringBuilder, and StringBuffer are true ? (Choose all that apply.)

  1. All three classes have a length() method.
  2. Objects of type StringBuffer are thread-safe.
  3. All three classes have overloaded append() methods.
  4. The "+" is an overloaded operator for all three classes.
  5. According to the API, StringBuffer will be faster than StringBuilder under most implementations.
  6. The value of an instance of any of these three types can be modified through various methods in the API.
Answer:
  • A and B are correct.
  • C is incorrect because String does not have an "append" method. D is incorrect because only String objects can be operated on using the overloaded "+" operator. E is backwards, StringBuilder is typically faster because it's not thread-safe. F is incorrect because String objects are immutable. A String reference can be altered to refer to a different String object, but the objects themselves are immutable.

20. Given:

class Polish { 
 public static void main(String[] args) { 
  int x = 4; 
  StringBuffer sb = new StringBuffer("..fedcba"); 
   sb.delete(3,6);
   sb.insert(3, "az"); 
   if(sb.length() > 6) x = sb.indexOf("b"); 
   sb.delete((x-3), (x-2)); 
   System.out.println(sb); 
 }
} 
What is the result ?
  1. .faza
  2. .fzba
  3. ..azba
  4. .fazba
  5. ..fezba
  6. Compilation fails.
  7. An exception is thrown at runtime.
Answer:
  • C is correct. Remember that StringBuffer methods use zero-based indexes, and that ending indexes are typically exclusive.
  • A, B, D, E, F, and G are incorrect based on the above.


BACK