Implementing String Algorithms MCQ Practice

Question 1 of 10

A city name is clipped to a three-letter tag. What text does this Java code display?

String city = "denver";
System.out.print(city.substring(0, 3));
  1. A. den
  2. B. ver
  3. C. denv
  4. D. denver

Question 2 of 10

A spark code prints one interior character and the last character. What text does this Java code display?

String code = "spark";
System.out.print(code.charAt(1));
System.out.print(code.charAt(code.length() - 1));
  1. A. sk
  2. B. pk
  3. C. sr
  4. D. pa

Question 3 of 10

How many letters p are in pepper? What text does this Java code display?

String s = "pepper";
int i = 0;
int p = 0;
while (i < s.length()) {
    if (s.substring(i, i + 1).equals("p")) {
        p++;
    }
    i++;
}
System.out.print(p);
  1. A. 2
  2. B. 6
  3. C. 1
  4. D. 3

Question 4 of 10

A short word is reversed with a while loop. What text does this Java code display?

String s = "rid";
String rev = "";
int i = s.length() - 1;
while (i >= 0) {
    rev += s.charAt(i);
    i--;
}
System.out.print(rev);
  1. A. dir
  2. B. rid
  3. C. d
  4. D. rdi

Question 5 of 10

A garden word is searched, then a four-letter slice from that index is printed. What text does this Java code display?

String s = "sunflower";
int at = s.indexOf("flow");
System.out.print(at);
System.out.print(s.substring(at, at + 4));
  1. A. 3flow
  2. B. 4flow
  3. C. 3sunf
  4. D. -1flow

Question 6 of 10

Overlapping two-letter windows of thatthat are compared with th. What text does this Java code display?

String s = "thatthat";
int count = 0;
for (int i = 0; i <= s.length() - 2; i++) {
    if (s.substring(i, i + 2).equals("th")) {
        count++;
    }
}
System.out.print(count);
  1. A. 1
  2. B. 4
  3. C. 2
  4. D. 8

Question 7 of 10

Two produce labels are compared. What text does this Java code display?

String x = "pea";
String y = "pear";
if (x.compareTo(y) < 0) {
    System.out.print("first");
} else {
    System.out.print("second");
}
  1. A. first
  2. B. second
  3. C. pear
  4. D. equal

Question 8 of 10

A word is reversed and then compared with the original. What text does this Java code display?

String s = "noon";
String rev = "";
for (int i = s.length() - 1; i >= 0; i--) {
    rev += s.substring(i, i + 1);
}
if (s.equals(rev)) {
    System.out.print("mirror");
} else {
    System.out.print("plain");
}
  1. A. plain
  2. B. mirror
  3. C. noon
  4. D. noonn

Question 9 of 10

After the first c in abracadabra, remaining a characters are counted. What text does this Java code display?

String s = "abracadabra";
int start = s.indexOf("c");
int count = 0;
for (int i = start; i < s.length(); i++) {
    if (s.charAt(i) == 'a') {
        count++;
    }
}
System.out.print(count);
  1. A. 5
  2. B. 3
  3. C. 4
  4. D. 2

Question 10 of 10

A clipped piece of quartz is measured. What text does this Java code display?

String s = "quartz";
String mid = s.substring(1, 5);
System.out.print(mid.length());
  1. A. 4
  2. B. 5
  3. C. 6
  4. D. 3