Searching

AP Computer Science A· difficulty 2/5

ArrayList<String> list = new ArrayList<>();
list.add("cat");
list.add("dog");
list.add("bird");
boolean found = false;
for (String s : list) {
    if (s.equals("dog")) found = true;
}
System.out.println(found);

What is printed?

  • A

    true

    check_circle
  • B

    false

  • C

    dog

  • D

    1

Explanation

"dog" is in the list, so the if statement sets found to true.

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

Related questions