Given the code fragment String h1 Bob String h2 new String Bob What is the best

Given the code fragment string h1 bob string h2 new

This preview shows page 12 - 15 out of 45 pages.

QUESTION 20Given the code fragment:String h1 = "Bob";String h2 = new String ("Bob");What is the best way to test that the values of h1 and h2 are the same? A.if(h1 == h2)B.if (h1.equals(h2))C.if (h1 = = h2)D.if (h1.same(h2))Correct Answer: Section: (none)ExplanationExplanation/Reference:The equals method compares values for equality. B QUESTION 21Which two are valid declarations of a two-dimensional array? QUESTION 22Given the code fragment:System.out.println ("Result:" +3+5);System.out.println ("result:" + (3+5));What is the result? Result: 8
Image of page 12
Result: 8 B. Result: 35 Result: 8 C. Result: 8 Result: 35 D. Result: 35 Result: 35 B QUESTION 23Given:public class Main {public static void main(String[] args) throws Exception { doSomething();}private static void doSomething() throws Exception { System.out.println("Before if clause");if (Math.random() > 0.5) {throw new Exception();}System.out.println ("After if clause");}}Which two are possible outputs? QUESTION 24A method doSomething () that has no exception handling code is modified to trail a method that throws achecked exception. Which two modifications, made independently, will allow the program to compile? A.Catch the exception in the method doSomething().B.Declare the exception to be thrown in the doSomething() method signature.C.Cast the exception to a RunTimeException in the doSomething() method.
Image of page 13
D.Catch the exception in the method that calls doSomething().Correct Answer: ADSection: (none)ExplanationExplanation/Reference:Valid Java programming language code must honor the Catch or Specify Requirement. This means that codethat might throw certain exceptions must be enclosed by either of the following:* A try statement that catches the exception. The try must provide a handler for the exception, as described inCatching and Handling Exceptions.* A method that specifies that it can throw the exception. The method must provide a throws clause that lists
Image of page 14
Image of page 15

You've reached the end of your free preview.

Want to read all 45 pages?

  • Summer '19

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture