Lists

Practice Questions

Computer Science › Lists

Questions
6
1

Which of the following is NOT a difference between the Array class and the ArrayList class in Java?

2

Consider the following code:

public static class Circle {

private double radius;

public Circle(double r) {

radius = r;

}

public double getArea() {

return radius * radius * Math.PI;

}

}

public static void main(String\[\] args) {

ArrayList circles = new ArrayList();

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

circles.add(new Circle(i + 4 * 2));

}

}

Which of the following represents code for iterating through the list circles in order to output the areas of the circles contained therein?

3

Consider the code below:

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

ArrayList names = new ArrayList();

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);

}

}

What is the bug in the code above?

4

Which of the following is NOT a valid declaration for an ArrayList?

5

Write a program that iterates through this data structure and prints the data (choose the best answer):

List<List<String>> listOflistOfStrings = new ArrayList<ArrayList<String>>();

6

Consider the code below:

ArrayList myPhilosophers = new ArrayList();

myPhilosophers.add("Frege");

myPhilosophers.add("Husserl");

myPhilosophers.add("Hegel");

myPhilosophers.add("Bill");

myPhilosophers.add("Frederick");

for(int i = 0; i < myPhilosophers.size(); i++) {

String s = myPhilosophers.get(i);

if(s.charAt(0) >= 'H' || s.charAt(3) < 'd') {

myPhilosophers.set(i, s.toUpperCase());

}

}

System.out.println(myPhilosophers);

What is the output for the code above?

Return to subject