String Manipulation MCQ Practice

Question 1 of 10

What text does this Java code display?

System.out.print("ket" + "ch");
  1. A. ket ch
  2. B. ketch
  3. C. chket
  4. D. ket+ch

Question 2 of 10

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

String a = "fir";
String b = a + "n";
System.out.print(a + "," + b);
  1. A. firn,firn
  2. B. fir,firn
  3. C. firn,fir
  4. D. fir,fir

Question 3 of 10

What text does this Java code display?

System.out.print("n" + 3 + 1);
  1. A. n4
  2. B. n31
  3. C. 4n
  4. D. n3+1

Question 4 of 10

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

class SlipMark {
    private int number;
    public SlipMark(int n) {
        number = n;
    }
    public String toString() {
        return "slip-" + number;
    }
}

SlipMark m = new SlipMark(14);
System.out.print("at " + m);
  1. A. at 14
  2. B. at SlipMark
  3. C. at slip-14
  4. D. at m

Question 5 of 10

What text does this Java code display?

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

Question 6 of 10

What text does this Java code display?

String w = "harbor";
System.out.print(w.substring(2));
  1. A. ha
  2. B. arb
  3. C. rbor
  4. D. bor

Question 7 of 10

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

String w = "harbor";
System.out.print("[" + w.substring(6) + "]");
  1. A. [r]
  2. B. []
  3. C. [harbor]
  4. D. The code throws an exception because 6 equals the length.

Question 8 of 10

What text does this Java code display?

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

Question 9 of 10

What text does this Java code display?

String p = new String("fjord");
String q = "fjord";
System.out.print(p.equals(q));
  1. A. false, because new String makes a different class
  2. B. true, because equals compares the character sequences
  3. C. true only if p == q
  4. D. A NullPointerException

Question 10 of 10

The table shows compareTo calls. Which row is correct? Do not assume a specific nonzero magnitude.

Expression Sign of result
"glen".compareTo("fjord") positive
"ketch".compareTo("ketch") zero
"oak".compareTo("oaken") negative
  1. A. Only the equal-string row is correct.
  2. B. glen versus fjord is actually negative, so the table is wrong.
  3. C. All three rows have the correct sign.
  4. D. oak versus oaken must be exactly -1, so the table is incomplete rather than correct.