Which is a true statement about the following code? (Choose all that apply.)
import java.util. *; public class Main { static class Shape { } public static void main(String[] args) { Shape c = new Shape(); ArrayList <Shape> l = new ArrayList<>(); Runnable r = new Thread(); int result = 0; if (c instanceof Shape) result += 1; if (l instanceof Shape) result += 2; if (r instanceof Shape) result += 4; System.out.println(result); } } //w ww. j a v a 2 s . c o m
E.
Code involving instanceof does not compile when there is no way for it to evaluate true.
D not only compiles but it is always true.
E does not compile because ArrayList is a concrete class that does not extend Shape.
F does compile because Runnable is an interface.
In theory, someone could subclass Shape and have the subclass implement Runnable.