Test: Computer Science

Consider the code below:

String[] db = {"Harvey","Plutarch","Frege","Radulphus"};

ArrayList<String> names = new ArrayList<String>();

for(int i = 0; i < 12; i++) {

        names.add(db[i % db.length]);

for(int i = 0; i < 12; i++) {

        if(names.get(i).equals("Frege")) {

                names.remove(i);

        }

}

1.

What is the bug in the code above?

The list will overrun its indices in the second loop.

There are no bugs.

The list will use up all of the memory on the machine.

There is an infinite loop in one of the two loops.

The .get method is buggy and likely to crash.  It is better to use an Iterator.

1/3 questions

0%

Access results and powerful study features!

Take 15 seconds to create an account.
Start now! Create your free account and get access to features like:
  • Full length diagnostic tests
  • Invite your friends
  • Access hundreds of practice tests
  • Monitor your progress over time
  • Manage your tests and results
  • Monitor the progress of your class & students
By clicking Create Account you agree that you are at least 13 years old and you agree to the Varsity Tutors LLC Terms of Use and Privacy Policy.
Learning Tools by Varsity Tutors