String Objects: Concatenation, Literals, and More

AP Computer Science A· difficulty 4/5

public static int run() {
    String s = "abracadabra";
    int c = 0;
    for (int i = 0; i < s.length(); i++) {
        if (s.charAt(i) == 'a') c++;
    }
    return c;
}
// Call: System.out.println(run());

What is printed?

  • A

    6

  • B

    5

    check_circle
  • C

    3

  • D

    4

Explanation

"abracadabra" contains a,a,a,a,a (positions 0,3,5,7,10) = 5.

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

Related questions