String Manipulation MCQ Practice

Question 1 of 10

What text does this Java code display?

System.out.print("fjor" + "d");
  1. A. fjor d
  2. B. fjord
  3. C. d+fjor
  4. D. fjor+d

Question 2 of 10

String objects are immutable. What text does this caller display?

String a = "oak";
String b = a;
a = a + "en";
System.out.print(a + "," + b);
  1. A. oak,oak
  2. B. oaken,oaken
  3. C. oaken,oak
  4. D. oak,oaken

Question 3 of 10

What text does this Java code display?

System.out.print(3 + 1 + "ketch");
  1. A. 31ketch
  2. B. 4ketch
  3. C. ketch4
  4. D. 3+1ketch

Question 4 of 10

DockTag.toString is supplied as shown. What text does this caller display?

class DockTag {
    private String id;
    public DockTag(String code) {
        id = code;
    }
    public String toString() {
        return "[" + id + "]";
    }
}

DockTag t = new DockTag("Y7");
System.out.print("id=" + t);
  1. A. id=Y7
  2. B. id=[Y7]
  3. C. id=DockTag
  4. D. id=t

Question 5 of 10

What text does this Java code display?

String w = "glacier";
System.out.print(w.length());
  1. A. 6
  2. B. 7
  3. C. 8
  4. D. 0

Question 6 of 10

What text does this Java code display?

String w = "glacier";
System.out.print(w.substring(1, 4));
  1. A. gla
  2. B. lac
  3. C. laci
  4. D. glac

Question 7 of 10

What text does this Java code display? Brackets mark the result.

String w = "glacier";
System.out.print("[" + w.substring(3, 3) + "]");
  1. A. [a]
  2. B. [c]
  3. C. []
  4. D. The code throws an exception because the bounds are equal.

Question 8 of 10

What text does this Java code display?

String w = "harbor";
System.out.print(w.indexOf("ar"));
  1. A. 1
  2. B. 2
  3. C. 0
  4. D. -1

Question 9 of 10

What text does this Java code display?

String p = "ketch";
String q = "Ketch";
System.out.print(p.equals(q));
  1. A. true
  2. B. false
  3. C. 0
  4. D. A positive compareTo value

Question 10 of 10

Which description of "fjord".compareTo("glen") is correct?

  1. A. The result is negative because fjord comes before glen lexicographically. The exact nonzero magnitude is not specified.
  2. B. The result must be -1.
  3. C. The result is positive because f has a larger code than g.
  4. D. The result is 0 because both strings have length 4 or 5.