Developing Algorithms Using Strings

AP Computer Science A· difficulty 3/5

String s = "hello";
int count = 0;
for (int i = 0; i < s.length(); i++) {
  if (s.charAt(i) == 'l') {
    count++;
  }
}
System.out.println(count);

What is printed?

  • A

    1

  • B

    0

  • C

    2

    check_circle
  • D

    3

Explanation

Iterating through "hello", the character 'l' appears at indices 2 and 3, so count = 2.

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

Related questions