Results 1 to 2 of 2
  1. #1
    money123 is offline Member
    Join Date
    Jul 2007
    Posts
    11
    Rep Power
    0

    Default Which statement is throwing a runtime error....

    import java.util.*;
    public void printStrings (List stringList) {
    if (stringList != null) {
    Iterator li = stringList.iterator();
    while (li.hasNext()) {
    System.out.println( (String) li.next());
    }
    }
    }

    Which one of the following statements highlights the assumed risk of potential run-time error in the above code?

    Choice 1
    System.out may have been redirected to a file rather than the terminal.

    Choice 2
    You do not know that stringList contains only String objects, so the cast could fail.

    Choice 3
    You need to check the size of the List using its size() method before accessing it so you do not read past the end of the list.

    Choice 4
    Since printStrings returns "void," you have no way to return a failure code.

    Choice 5
    The object referenced by stringList may or may not implement the iterator() method.

    I have got no idea on this question.... so tx for an explaination of answer in advance........;)

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Choice 1
    I don't think this would be a problem.

    Choice 2
    True statement.

    Choice 3
    Negative. Iterator takes care of this.

    Choice 4
    No failure code needed; we're just printing to console.

    Choice 5
    Unlikely. You are importing the java.util package so we'll assume that this is a java.util.List which has an iterator method/implements iterable. Of course you could have another, local (lame) List class in your classpath which does not have an iterator method.

Similar Threads

  1. Main method throwing specific Exception
    By bugger in forum New To Java
    Replies: 5
    Last Post: 05-13-2009, 02:34 PM
  2. throwing Exception
    By bugger in forum New To Java
    Replies: 3
    Last Post: 11-09-2007, 09:35 PM
  3. Help with if statement
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 07:53 AM
  4. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM
  5. If Statement
    By aDrizzle in forum New To Java
    Replies: 4
    Last Post: 07-08-2007, 08:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •