Review Questions

graphics/rq_icon.gif

10.16

Which of the following operators cannot be used in conjunction with a String object?

Select the two correct answers.

  1. +

  2. -

  3. +=

  4. .

  5. &

10.17

Which expression will extract the substring "kap" from a string defined by String str = "kakapo"?

Select the one correct answer.

  1. str.substring(2, 2)

  2. str.substring(2, 3)

  3. str.substring(2, 4)

  4. str.substring(2, 5)

  5. str.substring(3, 3)

10.18

What will be the result of attempting to compile and run the following code?

class MyClass {
    public static void main(String[] args) {
        String str1 = "str1";
        String str2 = "str2";
        String str3 = "str3";

        str1.concat(str2);
        System.out.println(str3.concat(str1));
    }
}

Select the one correct answer.

  1. The code will fail to compile since the expression str3.concat(str1) will not result in a valid argument for the println() method.

  2. The program will print str3str1str2 when run.

  3. The program will print str3 when run.

  4. The program will print str3str1 when run.

  5. The program will print str3str2 when run.

10.19

What function does the trim() method of the String class perform?

Select the one correct answer.

  1. It returns a string where the leading white space of the original string has been removed.

  2. It returns a string where the trailing white space of the original string has been removed.

  3. It returns a string where both the leading and trailing white space of the original string has been removed.

  4. It returns a string where all the white space of the original string has been removed.

  5. None of the above.

10.20

Which statements are true?

Select the two correct answers.

  1. String objects are immutable.

  2. Subclasses of the String class can be mutable.

  3. All wrapper classes are declared final.

  4. All objects have a public method named clone().

  5. The expression ((new StringBuffer()) instanceof String) is always true.

10.21

Which of these expressions are legal?

Select the four correct answers.

  1. "co".concat("ol")

  2. ("co" + "ol")

  3. ('c' + 'o' + 'o' + 'l')

  4. ("co" + new String('o' + 'l'))

  5. ("co" + new String("co"))

10.22

What will be the result of attempting to compile and run the following code?

public class RefEq {
    public static void main(String[] args) {
        String s = "ab" + "12";
        String t = "ab" + 12;
        String u = new String("ab12");
        System.out.println((s==t) + " " + (s==u));
    }
}

Select the one correct answer.

  1. The code will fail to compile.

  2. The program will print false false when run.

  3. The program will print false true when run.

  4. The program will print true false when run.

  5. The program will print true true when run.

10.23

Which of these parameter lists have a corresponding constructor in the String class?

Select the three correct answers.

  1. ()

  2. (int capacity)

  3. (char[] data)

  4. (String str)

10.24

Which method is not defined in the String class?

Select the one correct answer.

  1. trim()

  2. length()

  3. concat(String)

  4. hashCode()

  5. reverse()

10.25

Which statement concerning the charAt() method of the String class is true?

Select the one correct answer.

  1. The charAt() method takes a char value as an argument.

  2. The charAt() method returns a Character object.

  3. The expression ("abcdef").charAt(3) is illegal.

  4. The expression "abcdef".charAt(3) evaluates to the character 'd'.

  5. The index of the first character is 1.

10.26

Which expression will evaluate to true?

Select the one correct answer.

  1. "hello: there!".equals("hello there")

  2. "HELLO THERE".equals("hello there")

  3. ("hello".concat("there")).equals("hello there")

  4. "Hello There".compareTo("hello there") == 0

  5. "Hello there".toLowerCase().equals("hello there")

10.27

What will the following program print when run?

public class Search {
    public static void main(String[] args) {
        String s = "Contentment!";
        int middle = s.length()/2;
        String nt = s.substring(middle-1, middle+1);
        System.out.println(s.lastIndexOf(nt, middle));
    }
};

Select the one correct answer.

  1. 2

  2. 4

  3. 5

  4. 7

  5. 9

  6. 11