OCJP Preparation - Java Packages - BEHIND JAVA

OCJP Preparation - Java Packages

Share This

Given the code

What is the appropriate definition of the hashCode method in class Person?

  • A. return super.hashCode();
  • B. return name.hashCode() + age * 7;
  • C. return name.hashCode() + comment.hashCode() / 2;
  • D. return name.hashCode() + comment.hashCode() / 2 - age * 3;

Given the method in a class

Which statement is true?

  • A. This code is NOT thread-safe.
  • B. The programmer can replace StringBuffer with StringBuilder with no other changes.
  • C. This code will perform poorly. For better performance, the code should be rewritten: return "<" + this.name + ">";
  • D. This code will perform well and converting the code to use StringBuilder will not enhance the performance.

Given the program

What is the result when method testIfA is invoked?

  • A. True
  • B. Not true
  • C. An exception is thrown at runtime.
  • D. Compilation fails because of an error at line 12.
  • E. Compilation fails because of an error at line 19.

Given the program

What is the result?

  • A. The value "4" is printed at the command line.
  • B. Compilation fails because of an error in line 5.
  • C. Compilation fails because of an error in line 9.
  • D. A NullPointerException occurs at runtime.
  • E. A NumberFormatException occurs at runtime.
  • F. An IllegalStateException occurs at runtime.

Given the program

What is the result?

  • A. The value "4" is printed at the command line.
  • B. Compilation fails because of an error in line 5.
  • C. Compilation fails because of an error in line 9.
  • D. A NullPointerException occurs at runtime.
  • E. A NumberFormatException occurs at runtime.
  • F. An IllegalStateException occurs at runtime.

Given the program

Which two code fragments, inserted independently at line 3, generate the 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");

Given the program

What is the result?

  • A. r, t, t,
  • B. r, e, o,
  • C. Compilation fails.
  • D. An exception is thrown at runtime.

Given the program

What is the result?

  • A. r, t, t,
  • B. r, e, o,
  • C. Compilation fails.
  • D. An exception is thrown at runtime.

Given the program

Which statement is true?

  • A. Compilation fails because the hashCode method is not overridden.
  • B. A HashSet could contain multiple Person objects with the same name.
  • C. All Person objects will have the same hash code because the hashCode method is not overridden.
  • D. If a HashSet contains more than one Person object with name="Fred", then removing another Person, also with name="Fred", will remove them all.

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

  • A. The hashCode method for a given class can be used to test for object equality and object inequality for that class.
  • B. The hashCode method is used by the java.util.SortedSet collection class to order the elements within that set.
  • C. The hashCode method for a given class can be used to test for object inequality, but NOT object equality, for that class.
  • D. The only important characteristic of the values returned by a hashCode method is that the distribution of values must follow a Gaussian distribution.
  • E. 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.

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, the hashCode() comparison == might return false.
  • B. If the equals() method returns false, the hashCode() comparison == might return true.
  • C. If the hashCode() comparison == returns true, the equals() method must return true.
  • D. If the hashCode() comparison == returns true, the equals() method might return true.
  • E. If the hashCode() comparison != returns true, the equals() method might return true.

Given the program

How many String objects will be created when this method is invoked?

  • A. 1
  • B. 2
  • C. 3
  • D. 4
  • E. 5
  • F. 6

Given the program

Which lines will compile using Java 5, but will NOT compile using Java 1.4? (Choose all that apply.)

  • A. Line 4.
  • B. Line 5.
  • C. Line 6.
  • D. Line 7.
  • E. Line 8.
  • F. Line 9.

Given the following program

Which of the following will be included in the output String s? (Choose all that apply.)

  • A. .e1
  • B. .e2
  • C. =s
  • D. fly
  • E. None of the above.
  • F. Compilation fails.
  • G. An exception is thrown at runtime.

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

  • A. All three classes have a length() method.
  • B. Objects of type StringBuffer are thread-safe.
  • C. All three classes have overloaded append() methods.
  • D. The "+" is an overloaded operator for all three classes.
  • E. According to the API, StringBuffer will be faster than StringBuilder under most implementations.
  • F. The value of an instance of any of these three types can be modified through various methods in the API.

Given the program

What is the output?

  • A. .faza
  • B. .fzba
  • C. ..azba
  • D. .fazba
  • E. ..fezba
  • F. Compilation fails.
  • G. An exception is thrown at runtime.

No comments:

Post a Comment

Pages