Question: Exercise 3 . 1 . What happens when you run the following code? Choose all that apply. ` ` ` public class Duck { private

Exercise 3.1. What happens when you run the following code? Choose all that apply.
```
public class Duck {
private String name;
public Duck(String name){ this.name = name; }
public static void main(String[] args){
List ducks = new ArrayList>();
ducks.add(new Duck("Quack"));
ducks.add(new Duck("Puddles"));
Collections.sort(ducks); // sort by name
System.out.println(ducks);
}
public String toString(){ return name; }
public int compareTo(Duck d){
return name.compareTo(d.name); // call String's compareTo
}
}
```
(a) The code does not compile.
(b) The error is on line 11.
(c) The errors are on lines 8 and 9.
(d) The errors are on lines 4 and 17.
(e) This code runs just fine. It prints [Puddles, Quack].
(f) This code runs just fine. It prints [Quack, Puddles].
\((g)\) The error is on line 7.
Exercise 3 . 1 . What happens when you run the

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!