Explore what CodeHS has to offer for districts, schools, and teachers.
Click on one of our programs below to get started coding in the sandbox!
View All
Given:
Armor.java
public class Armor extends Item { public void use() { System.out.println("Armor equipped."); } }
Key.java
public class Key extends Item { public void use() { System.out.println("Key turned."); } }
Potion.java
public class Potion extends Item { public void use() { System.out.println("Potion consumed."); } }
Item.java
public class Item { public Item() {/* Implementation not shown */} public void use() { System.out.println("Item used."); } }
Main.java
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Item> items = new ArrayList<Item>(); items.add(new Armor()); items.add(new Key()); items.add(new Potion()); for (Item item : items) { item.use(); } } }
What will this program do?
Prints to the console:
Item used. Item used. Item used.
Infinite loop
Runtime error
Armor equipped. Key turned. Potion consumed.
Compile error