Implementing String Algorithms MCQ Practice

Question 1 of 10

A label is clipped from a planet name. What text does this Java code display?

String word = "planet";
System.out.print(word.substring(1, 4));
  1. A. lan
  2. B. pla
  3. C. lane
  4. D. plan

Question 2 of 10

A comet tag is shortened, then its original length is printed. What text does this Java code display?

String tag = "comet";
System.out.print(tag.substring(2));
System.out.print(tag.length());
  1. A. met5
  2. B. comet5
  3. C. me5
  4. D. met2

Question 3 of 10

How many times does the letter a appear in banana? What text does this Java code display?

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

Question 4 of 10

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

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

Question 5 of 10

A tree name is rebuilt from last character to first. What text does this Java code display?

String s = "oak";
String rev = "";
for (int i = s.length() - 1; i >= 0; i--) {
    rev += s.substring(i, i + 1);
}
System.out.print(rev);
  1. A. kao
  2. B. oak
  3. C. k
  4. D. kaoak

Question 6 of 10

A search reports where te occurs in notebook, then where z occurs. What text does this Java code display?

String title = "notebook";
System.out.print(title.indexOf("te"));
System.out.print(" ");
System.out.print(title.indexOf("z"));
  1. A. 2 -1
  2. B. 3 -1
  3. C. 2 0
  4. D. -1 2

Question 7 of 10

Two kite-shop labels are compared. What text does this Java code display?

String a = "kite";
String b = "kit";
if (a.compareTo(b) > 0) {
    System.out.print("after");
} else {
    System.out.print("not after");
}
  1. A. not after
  2. B. after
  3. C. equal
  4. D. kit

Question 8 of 10

A word is reversed by prepending one character at a time, then a slice is printed. What text does this Java code display?

String s = "trace";
String rev = "";
int i = 0;
while (i < s.length()) {
    rev = s.substring(i, i + 1) + rev;
    i++;
}
System.out.print(rev.substring(1, 4));
  1. A. car
  2. B. eca
  3. C. trac
  4. D. cart

Question 9 of 10

Vowels in audio are counted by asking whether each one-character piece appears in aeiou. What text does this Java code display?

String s = "audio";
int count = 0;
for (int i = 0; i < s.length(); i++) {
    String ch = s.substring(i, i + 1);
    if ("aeiou".indexOf(ch) >= 0) {
        count++;
    }
}
System.out.print(count);
  1. A. 5
  2. B. 2
  3. C. 0
  4. D. 4

Question 10 of 10

A maple tag is tested with equals after a prefix clip. What text does this Java code display?

String s = "maple";
if (s.substring(0, 3).equals("map")) {
    System.out.print("yes");
} else {
    System.out.print("no");
}
  1. A. yes
  2. B. no
  3. C. map
  4. D. maple