String Objects: Concatenation, Literals, and More

AP Computer Science A· difficulty 3/5

public static int run() {
    String s = "Hello World";
    return s.indexOf('o') + s.lastIndexOf('o') + s.length();
}
// Call: System.out.println(run());

What is printed?

  • A

    22

    check_circle
  • B

    16

  • C

    21

  • D

    23

Explanation

indexOf('o')=4, lastIndexOf('o')=7, length=11. 4+7+11 = 22.

Want 10 more like this — adaptive to your weak spots?

Related questions